2019牛客暑期多校训练营 第三场

F题没出,J题没出,锅++

题目链接:https://ac.nowcoder.com/acm/contest/883#question


A:

神仙题。

B:

签到题。一眼可以二分长度。

 1 /* basic header */
 2 #include 
 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;i11 #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 = 1e5 + 10;
21 int n, a[maxn], b[maxn], cnt1 = 0, cnt2 = 0;
22 
23 int check(int x) {
24     x*=2;
25     rep1(i, x, n)
26     if (a[i] - a[i - x] == b[i] - b[i - x]) return 1;
27     return 0;
28 }
29 
30 int main() {
31     a[0] = b[0] = 0;
32     scanf("%d", &n);
33     rep1(i, 1, n) {
34         int x; scanf("%1d", &x);
35         a[i] = a[i - 1], b[i] = b[i - 1];
36         if (x) cnt1++, a[i]++;
37         else cnt2++, b[i]++;
38     }
39     int l = 1, r = n / 2;
40     while (l <= r) {
41         int mid = l + r >> 1;
42         if (check(mid)) l = mid + 1;
43         else r = mid - 1;
44     }
45     printf("%d %d\n", 2 * r, 2 * min(cnt1, cnt2));
46     return 0;
47 }
View Code

不过队友O(n)前缀和过了。

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 #include<string>
 8 using namespace std;
 9 map<int, int> M;
10 int main() {
11     int Sum = 0;
12     int n;
13     cin >> n;
14     string s;
15     cin >> s;
16     int n0, n1;
17     n0 = 0; n1 = 0;
18     int ans = 0;
19     M[0] = -1;
20     for (int i = 0; i < n; i++) {
21         if (s[i] == '0') {
22             n0++;
23             Sum--;
24         } else {
25             n1++;
26             Sum++;
27         }
28         if (M.count(Sum)) {
29             M[Sum] = min(M[Sum], i);
30         } else {
31             M[Sum] = i;
32         }
33         ans = max(ans, i - M[Sum]);
34     }
35     cout << ans << " " << min(n0, n1) * 2 << endl;
36 
37     return 0;
38 }
Code via. rsqppp

C:

题解说是很有趣的构造题。

D:

数论。

E:

kruskal重构树神仙题。

F:

数据结构乱搞题。手写队列ac,优先队列似乎会TLE。

 1 /* basic header */
 2 #include 
 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;i11 #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 = 5e2 + 10;
21 int n, m, a[maxn][maxn], maxx[maxn], minn[maxn], q[maxn][2];
22 
23 int main() {
24     int t; scanf("%d", &t);
25     while (t--) {
26         int ans = 1;
27         scanf("%d%d", &n, &m);
28         rep1(i, 1, n) {
29             rep1(j, 1, n) scanf("%d", &a[i][j]);
30         }
31         rep1(i, 1, n) {
32             rep1(j, 1, n) maxx[j] = -int_inf, minn[j] = int_inf;
33             rep1(j, i, n) {
34                 // 维护第p列的极值
35                 rep1(p, 1, n) maxx[p] = max(maxx[p], a[j][p]);
36                 rep1(p, 1, n) minn[p] = min(minn[p], a[j][p]);
37                 int l = 1, h0 = 0, h1 = 0, t0 = 1, t1 = 1;
38                 rep1(r, 1, n) {
39                     while (t0 <= h0 && maxx[r] >= maxx[q[h0][0]]) h0--;
40                     while (t1 <= h1 && minn[r] <= minn[q[h1][1]]) h1--;
41                     q[++h0][0] = q[++h1][1] = r;
42                     while (l <= r && maxx[q[t0][0]] - minn[q[t1][1]] > m) {
43                         l++;
44                         if (q[t0][0] < l) t0++;
45                         if (q[t1][1] < l) t1++;
46                     }
47                     ans = max(ans, (r - l + 1) * (j - i + 1));
48                 }
49             }
50         }
51         printf("%d\n", ans);
52     }
53     return 0;
54 }
View Code

G:

分治题。

H:

选一个超远的点,然后二分斜率求叉积判断即可。

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 using namespace std;
 8 
 9 
10 typedef long long ll;
11 const int maxn = 2e6 + 10;
12 struct Point {
13     ll x, y;
14 } A[maxn];
15 
16 ll Cal(Point a, Point b) {
17     return a.x * b.y - a.y * b.x;
18 }
19 int n;
20 Point ans1;
21 Point ans2;
22 
23 ll check(ll x) {
24     ll num1 = 0; ll num2 = 0;
25     bool flag = false;
26     Point now;
27     now.x = x;
28     now.y = ans2.y;
29     Point Or = now;
30     Or.x -= ans1.x;
31     Or.y -= ans1.y;
32     for (int i = 1; i <= n; i++) {
33         Point phi = A[i];
34         phi.x -= ans1.x;
35         phi.y -= ans1.y;
36         if (Cal(Or, phi) > 0) {
37             num1++;
38         } else if (Cal(Or, phi) < 0) {
39             num2++;
40         }
41     }
42     if (num1 == num2) {
43         return 0;
44     } else if (num1 > num2) {
45         return 1;
46     } else if (num1 < num2) {
47         return -1;
48     }
49 }
50 
51 int main() {
52     ios::sync_with_stdio(false);
53     cin.tie(0);
54     int T;
55     cin >> T;
56     ans1.x = -172337ll;
57     ans1.y = -1001ll;
58     ans2.y =  199997ll;
59     while (T--) {
60         cin >> n;
61         for (int i = 1; i <= n; i++) {
62             ll x, y;
63             cin >> x >> y;
64             A[i].x = x;
65             A[i].y = y;
66         }
67         ll l = 0; ll r = 1000000000ll;
68         while (l <= r) {
69             ll m = (l + r ) / 2;
70             if (check(m) == 0) {
71                 ans2.x = m;
72                 break;
73             } else if (check(m) < 0) {
74                 l = m + 1;
75             } else if (check(m) > 0) {
76                 r = m - 1;
77             }
78         }
79         cout << ans1.x << " " << ans1.y << " " << ans2.x << " " << ans2.y << endl;
80     }
81     return 0;
82 }
Code via. rsqppp

I:

dp。

J:

数据结构大暴力。

 1 /* basic header */
 2 #include 
 3 /* define */
 4 #define ll long long
 5 #define dou double
 6 #define pb emplace_back
 7 #define sot(a,b) sort(a+1,a+1+b)
 8 #define rep1(i,a,b) for(int i=a;i<=b;++i)
 9 #define rep0(i,a,b) for(int i=a;i10 #define eps 1e-8
11 #define int_inf 0x3f3f3f3f
12 #define ll_inf 0x7f7f7f7f7f7f7f7f
13 #define lson (curpos<<1)
14 #define rson (curpos<<1|1)
15 /* namespace */
16 using namespace std;
17 /* header end */
18 
19 const int maxn = 1e6 + 10;
20 unordered_map<string, int>mp;
21 int q, m;
22 char rs[maxn];
23 
24 struct _List {
25     string s[maxn];
26     int pre[maxn], nxt[maxn], v[maxn];
27     int sz, csz, head, tail;
28 
29     void init() {
30         sz = 2, csz = 0, head = 1, tail = 2;
31         pre[head] = 0, nxt[head] = 2;
32         pre[tail] = 1, nxt[tail] = 0;
33     }
34     int app(string _s, int _v) {
35         csz++;
36         if (csz == m + 1) del(nxt[head]);
37         s[++sz] = _s, v[sz] = _v;
38         nxt[pre[tail]] = sz;
39         pre[sz] = pre[tail];
40         nxt[sz] = tail;
41         pre[tail] = sz;
42         return sz;
43     }
44     void del(int pos) {
45         --csz;
46         mp.erase(s[pos]);
47         pre[nxt[pos]] = pre[pos];
48         nxt[pre[pos]] = nxt[pos];
49     }
50 } _list;
51 
52 int main() {
53     int t; scanf("%d", &t);
54     while (t--) {
55         scanf("%d%d", &q, &m);
56         _list.init();
57         mp.clear();
58         rep1(i, 1, q) {
59             int op, v, pos; scanf("%d%s%d", &op, rs, &v);
60             string s(rs);
61             if (!op) {
62                 if (mp.find(s) != mp.end()) {
63                     pos = mp[s];
64                     v = _list.v[pos];
65                     _list.del(pos);
66                 }
67                 mp[s] = _list.app(s, v);
68                 printf("%d\n", v);
69             } else {
70                 int flag = 0;
71                 if (mp.find(s) != mp.end()) {
72                     pos = mp[s];
73                     flag = 1;
74                     if (v == -1) {
75                         pos = _list.pre[pos];
76                         if (pos == _list.head) flag = 0;
77                     } else if (v == 1) {
78                         pos = _list.nxt[pos];
79                         if (pos == _list.tail) flag = 0;
80                     }
81                 }
82                 if (flag) printf("%d\n", _list.v[pos]);
83                 else puts("Invalid");
84             }
85         }
86     }
87     return 0;
88 }
View Code

 

转载于:https://www.cnblogs.com/JHSeng/p/11247583.html

你可能感兴趣的:(2019牛客暑期多校训练营 第三场)