Comet OJ Contest #4

边开着Android Studio边打,结果电脑死机了一次……题目还蛮有趣的。

题目链接:https://cometoj.com/contest/39


A:

超级温暖人心的手速题。

 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 int a[6];
21 
22 int main() {
23     int t;
24     scanf("%d", &t);
25     while (t--) {
26         int ans = 1;
27         for (int i = 1; i <= 5; i++) a[i] = 0;
28         for (int i = 1; i <= 5; i++) {
29             int x; scanf("%d", &x); a[x]++;
30         }
31         for (int i = 1; i <= 5; i++)
32             if (a[i] > a[ans]) ans = i;
33         printf("%d\n", ans);
34     }
35     return 0;
36 }
View Code

B:

打表找规律之后发现k为奇数的时候答案就是r-l+1,k为偶数的时候答案以k+1为周期找规律。

 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 int t;
21 
22 int main() {
23     scanf("%d", &t);
24     while (t--) {
25         ll l, r, k, L, R;
26         scanf("%lld%lld%lld", &l, &r, &k);
27         if (k % 2) {
28             printf("%lld\n", r - l + 1);
29             continue;
30         }
31         r++;
32         if (l < k) L = l;
33         else L = l % (k + 1) + l / (k + 1) * k;
34         if (r < k) R = r;
35         else R = r % (k + 1) + r / (k + 1) * k;
36         printf("%lld\n", R - L);
37     }
38     return 0;
39 }
View Code

C:

不会,搬运题解好了。

 1 #include <cstdio>
 2 #include <vector>
 3 
 4 const int N = 1000 + 10;
 5 
 6 char s[N][N];
 7 int g[N][N];
 8 
 9 int main() {
10   int T;
11   scanf("%d", &T);
12   for (int cas = 1; cas <= T; ++cas) {
13     int n, m, k;
14     scanf("%d%d%d", &n, &m, &k);
15     for (int i = 1; i <= n; ++i) {
16       scanf("%s", s[i] + 1);
17     }
18     for (int i = 1; i <= n; ++i) {
19       for (int j = 1; j <= m; ++j) {
20         g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1] + (s[i][j] == '0');
21       }
22     }
23     if (g[n][m] == 0) {
24       for (int i = 1; i <= k; ++i) {
25         printf("%d%c", i, " \n"[i == k]);
26       }
27       continue;
28     }
29     std::vector<int> ret;
30     for (int a = 0; a <= k && a <= n - 1; ++a) {
31       int b = k - a;
32       if (b < 0 || b > m - 1) continue;
33       if (g[n][m] % ((a + 1) * (b + 1))) continue;
34       std::vector<int> xs, ys;
35       int col_sum = g[n][m] / (b + 1);
36       int row_sum = g[n][m] / (a + 1);
37       int avg = g[n][m] / ((a + 1) * (b + 1));
38       for (int i = 0, j = 0; i <= m; ++i) {
39         if (g[n][i] % col_sum == 0 && g[n][i] / col_sum == j) {
40           ys.push_back(i);
41           ++j;
42         }
43       }
44       if (ys.size() != b + 2) continue;
45       ys.pop_back(); ys.push_back(m);
46       for (int i = 0, j = 0; i <= n; ++i) {
47         if (g[i][m] % row_sum == 0 && g[i][m] / row_sum == j) {
48           xs.push_back(i);
49           ++j;
50         }
51       }
52       if (xs.size() != a + 2) continue;
53       xs.pop_back(); xs.push_back(n);
54       bool valid = true;
55       for (size_t i = 1; i < xs.size() && valid; ++i) {
56         for (size_t j = 1; j < ys.size() && valid; ++j) {
57           int sum = g[xs[i]][ys[j]] - g[xs[i]][ys[j - 1]] - g[xs[i - 1]][ys[j]] + g[xs[i - 1]][ys[j - 1]];
58           valid &= (sum == avg);
59         }
60       }
61       if (valid) {
62         std::vector<int> tmp;
63         for (int i = 1; i <= a; ++i) tmp.push_back(xs[i]);
64         for (int i = 1; i <= b; ++i) tmp.push_back(ys[i] + n - 1);
65         if (ret.empty() || ret > tmp) ret = tmp;
66       }
67     }
68     if (ret.empty()) puts("Impossible");
69     else {
70       for (int i = 0; i < k; ++i) {
71         printf("%d%c", ret[i], " \n"[i == k - 1]);
72       }
73     }
74   }
75   return 0;
76 }
STD

D:

又是个找规律的题……code via. lzh12139

 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 int calc(ll x) {
21     if (x < 10) return x;
22     int a[20]; a[0] = 0;
23     while (x) a[++a[0]] = x % 10, x /= 10;
24     ll tmp = 0;
25     for (int i = a[0]; i >= 2; i--) tmp = tmp * 10 + (a[i] + a[i - 1]) % 10;
26     return calc(tmp);
27 }
28 
29 ll solve(ll x) {
30     ll ret = (x + 1) / 10 * 45;
31     if (x % 10 == 9) return ret;
32     int y = calc(x / 10 * 10);
33     rep1(i, 0, x % 10) ret += (y + i) % 10;
34     return ret;
35 }
36 int main() {
37     int t; scanf("%d", &t);
38     while (t--) {
39         ll l, r; scanf("%lld%lld", &l, &r);
40         printf("%lld\n", solve(r) - solve(l - 1));
41     }
42 }
View Code

E:

  1 #include <cstdio>
  2 #include <vector>
  3 #include <algorithm>
  4 
  5 int solve1(const std::vector<int> &a, const std::vector<int> &b, int e) {
  6   int ca = 0, cb = 0;
  7   for (auto &&x: a) ca += x == e;
  8   for (auto &&x: b) cb += x == e;
  9   return std::min(ca, cb);
 10 }
 11 
 12 int solve2(const std::vector<int> &a, const std::vector<int> &b, int x, int y) {
 13   int say = 0, sby = 0;
 14   for (auto &v: a) say += v == y;
 15   for (auto &v: b) sby += v == y;
 16   int ret = 0;
 17   for (size_t i = 0, j = 0, cx = 0; i < a.size() && j < b.size(); ++i, ++j) {
 18     while (i < a.size() && a[i] != x) {
 19       say -= a[i] == y;
 20       ++i;
 21     }
 22     while (j < b.size() && b[j] != x) {
 23       sby -= b[j] == y;
 24       ++j;
 25     }
 26     if (i < a.size() && a[i] == x && j < b.size() && b[j] == x) {
 27       ++cx;
 28       ret = std::max<int>(ret, cx + std::min(say, sby));
 29     }
 30   }
 31   return ret;
 32 }
 33 
 34 int solve(const std::vector<int> &a, const std::vector<int> &b) {
 35   std::vector<int> sa1(a.size() + 1), sb1(b.size() + 1);
 36   for (int i = a.size() - 1; i >= 0; --i) {
 37     sa1[i] = sa1[i + 1] + (a[i] == 1);
 38   }
 39   for (int i = b.size() - 1; i >= 0; --i) {
 40     sb1[i] = sb1[i + 1] + (b[i] == 1);
 41   }
 42   std::vector<int> pa0, pa2, pb0, pb2;
 43   for (size_t i = 0; i < a.size(); ++i) {
 44     if (a[i] == 0) pa0.push_back(i);
 45     if (a[i] == 2) pa2.push_back(i);
 46   }
 47   for (size_t i = 0; i < b.size(); ++i) {
 48     if (b[i] == 0) pb0.push_back(i);
 49     if (b[i] == 2) pb2.push_back(i);
 50   }
 51   pa2.push_back(a.size());
 52   pb2.push_back(b.size());
 53   std::reverse(pa2.begin(), pa2.end());
 54   std::reverse(pb2.begin(), pb2.end());
 55   std::vector<int> xs;
 56   for (size_t i = 0; i < pa2.size() && i < pb2.size(); ++i) {
 57     xs.push_back(sa1[pa2[i]] - sb1[pb2[i]]);
 58   }
 59   std::sort(xs.begin(), xs.end());
 60   xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
 61 
 62   std::vector<int> bit(xs.size(), -1e9);
 63 
 64   auto add = [](std::vector<int> &bit, int x, int v) {
 65     for (; x >= 0; x -= ~x & x + 1) {
 66       bit[x] = std::max(bit[x], v);
 67     }
 68   };
 69 
 70   auto get = [](std::vector<int> &bit, int x, int r = -1e9) {
 71     for (; x < bit.size(); x += ~x & x + 1) {
 72       r = std::max(bit[x], r);
 73     }
 74     return r;
 75   };
 76 
 77   int ret = 0;
 78   for (int i = std::min(pa0.size(), pb0.size()) - 1, j = 0; i >= 0; --i) {
 79     while (j < pa2.size() && j < pb2.size() && pa2[j] > pa0[i] && pb2[j] > pb0[i]) {
 80       int p = std::lower_bound(xs.begin(), xs.end(), sa1[pa2[j]] - sb1[pb2[j]]) - xs.begin();
 81       add(bit, p, j - sa1[pa2[j]]);
 82       ++j;
 83     }
 84     auto p = std::lower_bound(xs.begin(), xs.end(), sa1[pa0[i]] - sb1[pb0[i]]) - xs.begin();
 85     ret = std::max(ret, sa1[pa0[i]] + get(bit, p) + i + 1);
 86   }
 87   return ret;
 88 }
 89 
 90 int main() {
 91   int T;
 92   scanf("%d", &T);
 93   for (int cas = 1; cas <= T; ++cas) {
 94     int n, m;
 95     scanf("%d%d", &n, &m);
 96     std::vector<int> a(n), b(m);
 97     for (int i = 0; i < n; ++i) {
 98       scanf("%d", &a[i]);
 99       --a[i];
100     }
101     for (int i = 0; i < m; ++i) {
102       scanf("%d", &b[i]);
103       --b[i];
104     }
105     int res = std::max(std::max(solve1(a, b, 0), solve1(a, b, 1)), solve1(a, b, 2));
106     res = std::max(res, solve2(a, b, 0, 1));
107     res = std::max(res, solve2(a, b, 0, 2));
108     res = std::max(res, solve2(a, b, 1, 2));
109     res = std::max(res, solve(a, b));
110     res = std::max(res, solve(b, a));
111     printf("%d\n", res);
112   }
113   return 0;
114 }
STD

F:

  1 #include <cstdio>
  2 #include <cassert>
  3 #include <vector>
  4 #include <map>
  5 #include <algorithm>
  6 
  7 using int64 = long long;
  8 using uint64 = unsigned long long;
  9 
 10 const int mod = 1e9 + 7;
 11 const int N = 100;
 12 
 13 std::vector<std::pair<uint64, uint64>> count[N];
 14 
 15 inline uint64 rev(uint64 ns) {
 16   ns = ((ns & 0xaaaaaaaaaaaaaaaaull) >>  1) | ((ns & 0x5555555555555555ull) <<  1);
 17   ns = ((ns & 0xccccccccccccccccull) >>  2) | ((ns & 0x3333333333333333ull) <<  2);
 18   ns = ((ns & 0xf0f0f0f0f0f0f0f0ull) >>  4) | ((ns & 0x0f0f0f0f0f0f0f0full) <<  4);
 19   ns = ((ns & 0xff00ff00ff00ff00ull) >>  8) | ((ns & 0x00ff00ff00ff00ffull) <<  8);
 20   ns = ((ns & 0xffff0000ffff0000ull) >> 16) | ((ns & 0x0000ffff0000ffffull) << 16);
 21   ns = ((ns & 0xffffffff00000000ull) >> 32) | ((ns & 0x00000000ffffffffull) << 32);
 22   return ns;
 23 }
 24 
 25 int main() {
 26   int T;
 27   scanf("%d", &T);
 28   for (int cas = 1; cas <= T; ++cas) {
 29     int n;
 30     scanf("%d", &n);
 31     for (int i = 0; i < n; ++i) count[i].clear();
 32     std::vector<int> p(n);
 33     uint64 candidate = (uint64(1) << n) - 1;
 34     uint64 mask = candidate;
 35     int cnt = 0;
 36     for (int i = 0; i < n; ++i) {
 37       scanf("%d", &p[i]);
 38       --p[i];
 39       if (p[i] == -1) {
 40         ++cnt;
 41       } else {
 42         candidate ^= uint64(1) << p[i];
 43       }
 44     }
 45     if (p[0] == -1) {
 46       for (int i = 0; i < n; ++i) if (candidate >> i & 1) {
 47         count[0].emplace_back(uint64(1) << i, uint64(1));
 48       }
 49     } else {
 50       count[0].emplace_back(uint64(1) << p[0], uint64(1));
 51     }
 52 
 53     int64 ret = 1;
 54     for (int i = 1; i <= cnt; ++i) {
 55       ret = ret * i % mod;
 56     }
 57 
 58     for (int i = 1; i < n; ++i) {
 59       if (p[i] == -1) {
 60         for (auto &&e: count[i - 1]) {
 61           auto s = e.first, ns = rev(s);
 62           for (uint64 msk = candidate ^ (candidate & s); msk; ) {
 63             int j = __builtin_ctzll(msk);
 64             assert(~s >> j & 1);
 65             uint64 z = (j <= 31) ? (ns >> (63 - 2 * j)) : (ns << (2 * j - 63));
 66             z &= mask;
 67             if ((z | s) == s) {
 68               count[i].emplace_back(s ^ (uint64(1) << j), e.second);
 69             }
 70             msk ^= uint64(1) << j;
 71           }
 72         }
 73       } else {
 74         for (auto &&e: count[i - 1]) {
 75           auto s = e.first, ns = rev(s);
 76           if (s >> p[i] & 1) continue;
 77           ns = (p[i] <= 31) ? (ns >> (63 - 2 * p[i])) : (ns << (2 * p[i] - 63));
 78           ns &= mask;
 79           if ((ns | s) == s) {
 80             count[i].emplace_back(s | (uint64(1) << p[i]), e.second);
 81           }
 82         }
 83       }
 84       if (count[i].size()) {
 85         std::sort(count[i].begin(), count[i].end());
 86         int m = 1;
 87         for (size_t j = 1; j < count[i].size(); ++j) {
 88           if (count[i][j].first != count[i][j - 1].first) count[i][m++] = count[i][j];
 89           else count[i][m - 1].second += count[i][j].second;
 90         }
 91         count[i].resize(m);
 92       }
 93     }
 94 
 95     for (auto &&e: count[n - 1]) {
 96       ret -= e.second;
 97       ret = (ret % mod + mod) % mod;
 98     }
 99     printf("%lld\n", ret);
100   }
101   return 0;
102 }
STD

 

posted @ 2019-05-24 22:55  JHSeng  阅读(333)  评论(0编辑  收藏  举报