2019-2020 ACM-ICPC Pacific Northwest Regional Contest

8题。B有傻逼做法但是没出来,不应该。反而最后搞出G出乎意料,lzhnb!


A:

solver:lzh

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 typedef long long ll;
 5 #define ff first
 6 #define ss second
 7 #define mp make_pair
 8 
 9 ll dp1[100010], dp2[100010], ans[100010];
10 int t[100010], sz[100010], sumt[100010], n, T;
11 vector<pii> v[100010];
12 void dfs0(int x, int pre) {
13     for (auto i : v[x])
14         if (i.ff != pre) {
15             dfs0(i.ff, x);
16             sz[x] += sz[i.ff];
17             sumt[x] += sumt[i.ff];
18             dp1[x] += 1ll * sz[i.ff] * i.ss + dp1[i.ff];
19             dp2[x] += 1ll * sumt[i.ff] * i.ss + dp2[i.ff];
20         }
21     sumt[x] += t[x];
22     sz[x]++;
23 }
24 void dfs(int x, int pre) {
25     ans[x] = 1ll * t[x] * dp1[x] + dp2[x];
26     for (auto i : v[x])
27         if (i.ff != pre) {
28             dp1[i.ff] = dp1[x] + 1ll * i.ss * (n - sz[i.ff]) - 1ll * sz[i.ff] * i.ss;
29             dp2[i.ff] = dp2[x] + 1ll * (T - sumt[i.ff]) * i.ss - 1ll * sumt[i.ff] * i.ss;
30             dfs(i.ff, x);
31         }
32 }
33 int main() {
34     scanf("%d", &n);
35     T = 0;
36     for (int i = 1; i <= n; i++)
37         scanf("%d", &t[i]), T += t[i];
38     for (int i = 1; i <= n - 1; i++) {
39         int x, y, z;
40         scanf("%d%d%d", &x, &y, &z);
41         v[x].push_back(mp(y, z));
42         v[y].push_back(mp(x, z));
43     }
44     dfs0(1, -1);
45     dfs(1, -1);
46     for (int i = 1; i <= n; i++)
47         printf("%lld\n", ans[i]);
48 }
View Code

B:

有很好的单调栈sb做法,可惜我最后想到了但是没想清楚,崩了。赛后听了bobby的想法才知道我的想法差在哪里。

 1 /* basic header */
 2 #include <bits/stdc++.h>
 3 /* define */
 4 #define ll long long
 5 #define dou double
 6 #define pb emplace_back
 7 #define mp make_pair
 8 #define sot(a,b) sort(a+1,a+1+b)
 9 #define rep1(i,a,b) for(int i=a;i<=b;++i)
10 #define rep0(i,a,b) for(int i=a;i<b;++i)
11 #define eps 1e-8
12 #define int_inf 0x3f3f3f3f
13 #define ll_inf 0x7f7f7f7f7f7f7f7f
14 #define lson (curpos<<1)
15 #define rson (curpos<<1|1)
16 /* namespace */
17 using namespace std;
18 /* header end */
19 
20 const int maxn = 2e5 + 10;
21 int n, k, cnt[maxn], a[maxn], in[maxn], ans[maxn];
22 
23 int main() {
24     for (int i = 0; i < maxn; i++) cnt[i] = in[i] = 0;
25     scanf("%d%d", &n, &k);
26     for (int i = 1; i <= n; i++) {
27         scanf("%d", &a[i]);
28         cnt[a[i]]++;
29     }
30     stack<int>s;
31     s.push(a[1]); cnt[a[1]]--; in[a[1]] = 1;
32     for (int i = 2; i <= n; i++) {
33         cnt[a[i]]--;
34         if (in[a[i]]) continue;
35         for (; !s.empty() && a[i] < s.top() && cnt[s.top()];) {
36             in[s.top()] = 0;
37             s.pop();
38         }
39         s.push(a[i]);
40         in[a[i]] = 1;
41     }
42     for (int i = k; i >= 1; i--) ans[i] = s.top(), s.pop();
43     for (int i = 1; i <= k; i++) printf("%d ", ans[i]);
44     puts("");
45     return 0;
46 }
View Code

C:

solver:lzh、czq

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 typedef long long ll;
 5 #define ff first
 6 #define ss second
 7 #define mp make_pair
 8 
 9 vector<int>v[100010];
10 int vis[100010];
11 int main() {
12     int n, m; scanf("%d%d", &n, &m);
13     for (int i = 1; i <= m; i++) {
14         int x, y; scanf("%d%d", &x, &y);
15         v[x].push_back(y);
16         v[y].push_back(x);
17     }
18     queue<pii>q;
19     q.push(mp(0, 1));
20     while (!q.empty()) {
21         pii x = q.front(); q.pop();
22         if (vis[x.ss])continue;
23         vis[x.ss]++;
24         for (auto i : v[x.ss])
25             if (!vis[i]) {
26                 if (i == n) {
27                     printf("%d\n", x.ff); return 0;
28                 }
29                 q.push(mp(x.ff + 1, i));
30             }
31     }
32 }
View Code

D:

solver:lzh、czq

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 typedef long long ll;
 5 #define ff first
 6 #define ss second
 7 
 8 int main() {
 9     int a, b, ans = 0; scanf("%d%d", &a, &b);
10     while (a != b) {
11         if (a % 2)a++;
12         else if (a < b) {
13             ans += b - a;
14             break;
15         } else a = a / 2;
16         ans++;
17     }
18     printf("%d\n", ans);
19 }
View Code

E:

solver:lzh、zyh

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 typedef long long ll;
 5 #define ff first
 6 #define ss second
 7  
 8 char s[100010];
 9 int a[30];
10 int main(){
11     scanf("%s",s+1);
12     int n=strlen(s+1);
13     for(int i=1;i<=n;i++)a[s[i]-'a'+1]++;
14     ll ans=1;
15     ll mod=11092019;
16     for(int i=1;i<=26;i++)ans=ans*(a[i]+1)%mod;
17     printf("%d\n",ans);
18 }
View Code

G:

solver:lzh、czq

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 typedef long long ll;
 5 #define ff first
 6 #define ss second
 7 
 8 pii h[200010];
 9 int v1[200010], v2[200010];
10 int main() {
11     int n;
12     scanf("%d", &n);
13     int cnth = 0, cntv = 0;
14     for (int i = 1; i <= n; i++) {
15         char s[10];
16         int x, y, z;
17         scanf("%s%d%d%d", s + 1, &x, &y, &z);
18         if (s[1] == 'h')
19             h[++cnth] = make_pair(x - z, x - z + y - 1);
20         else {
21             v1[++cntv] = x - z;
22             v2[cntv] = x - z + y - 1;
23         }
24     }
25     sort(v1 + 1, v1 + 1 + cntv);
26     sort(v2 + 1, v2 + 1 + cntv);
27     ll ans = 0;
28     for (int i = 1; i <= cnth; i++) {
29         int l, r;
30         l = lower_bound(v2 + 1, v2 + 1 + cntv, h[i].ff) - v2;
31         r = upper_bound(v1 + 1, v1 + 1 + cntv, h[i].ss) - v1;
32         ans += r - l;
33     }
34     printf("%lld\n", ans);
35 }
View Code

I:

solver:czq

  1 /* basic header */
  2 #include <bits/stdc++.h>
  3 /* define */
  4 #define ll long long
  5 #define dou double
  6 #define pb emplace_back
  7 #define mp make_pair
  8 #define sot(a,b) sort(a+1,a+1+b)
  9 #define rep1(i,a,b) for(int i=a;i<=b;++i)
 10 #define rep0(i,a,b) for(int i=a;i<b;++i)
 11 #define eps 1e-8
 12 #define int_inf 0x3f3f3f3f
 13 #define ll_inf 0x7f7f7f7f7f7f7f7f
 14 #define lson (curpos<<1)
 15 #define rson (curpos<<1|1)
 16 /* namespace */
 17 using namespace std;
 18 /* header end */
 19 
 20 map<int, int> front, back, dist;
 21 
 22 bool parity(const string &s) {
 23     bool ret = false;
 24     for (auto i = s.begin(); i != s.end(); ++i)
 25         for (auto j = i + 1; j != s.end(); ++j)
 26             if (*i > *j) ret = !ret;
 27     return ret;
 28 }
 29 
 30 string swap(const string &s, int i, int j) {
 31     string ret(s);
 32     ret[i] = s[j]; ret[j] = s[i];
 33     return ret;
 34 }
 35 
 36 bool bfs(const vector<vector<int>> &edges) {
 37     queue<int> q;
 38     for (auto i : front) {
 39         if (i.second == -1) {
 40             dist[i.first] = 0;
 41             q.push(i.first);
 42         } else {
 43             dist[i.first] = int_inf;
 44         }
 45     }
 46     dist[-1] = int_inf;
 47     while ((int)q.size()) {
 48         int u = q.front();
 49         q.pop();
 50         if (dist[u] < dist[-1]) {
 51             for (auto v : edges[u]) {
 52                 auto curr = back.find(v);
 53                 if (dist[curr->second] == int_inf) {
 54                     dist[curr->second] = dist[u] + 1;
 55                     q.push(curr->second);
 56                 }
 57             }
 58         }
 59     }
 60 
 61     return dist[-1] != int_inf;
 62 }
 63 
 64 bool dfs(const vector<vector<int>> &edges, int u, map<int, int> *front, map<int, int> *back, map<int, int> &dist) {
 65     if (u == -1) return true;
 66     for (auto v : edges[u]) {
 67         if (dist.find((*back)[v])->second == dist.find(u)->second + 1) {
 68             if (dfs(edges, (*back)[v], front, back, dist)) {
 69                 (*front)[u] = v; (*back)[v] = u;
 70                 return true;
 71             }
 72         }
 73     }
 74     return false;
 75 }
 76 
 77 int main() {
 78     int n; cin >> n;
 79     vector<string> words(n);
 80     unordered_map<string, int> index;
 81     vector<vector<int>> edges(n), pwords(2);
 82     for (int i = 0; i < n; ++i) {
 83         cin >> words[i];
 84         index[words[i]] = i;
 85     }
 86     int __size = words[0].size();
 87     for (int i = 0; i < n; ++i) {
 88         int p = (int)parity(words[i]);
 89         pwords[p].push_back(i);
 90         if (!p) {
 91             for (int a = 0; a < __size; ++a) {
 92                 for (int b = a + 1; b < __size; ++b) {
 93                     auto s = swap(words[i], a, b);
 94                     auto si = index.find(s);
 95                     if (si != index.end())
 96                         edges[i].push_back(si->second);
 97                 }
 98             }
 99         }
100     }
101     for (auto i : pwords[0]) front[i] = -1;
102     for (auto i : pwords[1]) back[i] = -1;
103     for (int i = 0; i < n; ++i) dist[i] = int_inf;
104     // flow
105     while (bfs(edges))
106         for (auto i : front)
107             if (i.second == -1) dfs(edges, i.first, &front, &back, dist);
108     int total_flow = 0;
109     for (auto i : front) total_flow += (i.second != -1);
110     cout << n - total_flow << endl;
111     return 0;
112 }
View Code

L:

solver:lzh、zyh

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 typedef long long ll;
 5 #define ff first
 6 #define ss second
 7 
 8 char s[30];
 9 ll ans = 1e18;
10 const ll ansinf = 1e18;
11 int a[30], cal[15][15], len;
12 vector<pii> f[30];
13 void dfs(int x) {
14     if (ans != ansinf)
15         return;
16     if (x == len - 1) {
17         for (int i = 1; i <= len - 1; i++) {
18             int tmp = 0;
19             for (auto j : f[i])
20                 tmp += cal[a[j.ff]][a[j.ss]];
21             if (tmp % 10 != s[i] - '0')
22                 return;
23         }
24         ans = 0;
25         for (int i = len; i >= 1; i--)
26             ans = ans * 10 + a[i];
27         return;
28     }
29     for (int i = 0; i <= 9; i++) {
30         int tmp = 0;
31         a[x - len + 1] = i;
32         for (auto j : f[x])
33             tmp += cal[a[j.ff]][a[j.ss]];
34         if (tmp % 10 == s[x] - '0') {
35             dfs(x - 1);
36         }
37     }
38 }
39 int main() {
40     for (int i = 0; i <= 9; i++)
41         for (int j = 0; j <= 9; j++)
42             cal[i][j] = i * j % 10;
43 
44     scanf("%s", s + 1);
45     int n = strlen(s + 1);
46     if (n % 2 == 0) {
47         printf("-1\n");
48         return 0;
49     }
50 
51     len = (n + 1) / 2;
52     for (int i = 1; i <= len; i++)
53         for (int j = 1; j <= len; j++)
54             f[i + j - 1].push_back(make_pair(i, j));
55 
56     char tmpc[30];
57     for (int i = 1; i <= n; i++)
58         tmpc[i] = s[n - i + 1];
59     for (int i = 1; i <= n; i++)
60         s[i] = tmpc[i];
61 
62     dfs(n);
63     if (ans == 1e18)
64         printf("-1\n");
65     else
66         printf("%lld\n", ans);
67 }
View Code

M:

solver:zyh

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char Map[2001][2001];
 4 bool vis[2001][2001];
 5 int c[2001][2001];
 6 int cnt = 0;
 7 int mx[] = {0, 0, 1, -1};
 8 int my[] = {1, -1, 0, 0};
 9 void dfs(int x, int y) {
10     vis[x][y] = true;
11     c[x][y] = cnt;
12     for (int i = 0; i < 4; ++i)
13         if (!vis[x + mx[i]][y + my[i]] && Map[x + mx[i]][y + my[i]] == '.') {
14             dfs(x + mx[i], y + my[i]);
15         }
16     if (!vis[x - 1][y + 1] && Map[x - 1][y + 1] == '.' && ((Map[x][y + 1] == '.' || Map[x - 1][y] == '.') || (Map[x][y + 1] == '/' && Map[x - 1][y] == '/')) ) dfs(x - 1, y + 1);
17     if (!vis[x - 1][y - 1] && Map[x - 1][y - 1] == '.' && ((Map[x][y - 1] == '.' || Map[x - 1][y] == '.') || (Map[x][y - 1] == '\\' && Map[x - 1][y] == '\\'))) dfs(x - 1, y - 1);
18     if (!vis[x + 1][y - 1] && Map[x + 1][y - 1] == '.' && ((Map[x][y - 1] == '.' || Map[x + 1][y] == '.') || (Map[x][y - 1] == '/' && Map[x + 1][y] == '/'))) dfs(x + 1, y - 1);
19     if (!vis[x + 1][y + 1] && Map[x + 1][y + 1] == '.' && ((Map[x][y + 1] == '.' || Map[x + 1][y] == '.') || (Map[x][y + 1] == '\\' && Map[x + 1][y] == '\\'))) dfs(x + 1, y + 1);
20 }
21 int main() {
22     int n, m;
23     scanf("%d%d", &n, &m);
24     for (int i = 2; i <= n + 1; ++i) {
25         scanf("%s", Map[i] + 2);
26     }
27     for (int i = 0; i <= n + 3; ++i) {
28         Map[i][0] = Map[i][m + 3] = '$';
29     }
30     for (int i = 0; i <= m + 3; ++i) {
31         Map[0][i] = Map[n + 3][i] = '$';
32     }
33     for (int i = 1; i <= n + 2; ++i) Map[i][1] = Map[i][m + 2] = '.';
34     for (int i = 1; i <= m + 2; ++i) Map[1][i] = Map[n + 2][i] = '.';
35 
36     int f1 = 0, f2 = 0;
37     for (int i = 1; i <= n + 2; ++i)
38         for (int j = 1; j <= m + 2; ++j) {
39             if (Map[i][j] == '/' && Map[i][j + 1] == '\\' && Map[i + 1][j] == '\\' && Map[i + 1][j + 1] == '/') ++f1;
40             if (Map[i][j] == '.' && !vis[i][j]) {
41                 cnt++;
42                 dfs(i, j);
43                 f2++;
44             }
45         }
46     printf("%d", f1 + f2 - 1);
47 }
View Code
posted @ 2020-01-15 20:33  JHSeng  阅读(1185)  评论(0编辑  收藏  举报