0%

nowcoder多校(第九场)

A. Groundhog and 2-Power Representation

题目描述:

给出一个例子,就是计算后面那种表达式的值。

数据范围:
$ 10 \le|ans| \le 10^{180} \\ |s| \le 20000 $

题解:

大整数类居然有问题,重载输出 $ << $ 参数需要是 $ const $ 才能够使用临时变量输出。而且比较大小的居然错了,我晚上 $ debug $ 半天,现在这两个错误修好了,后期还需要增加一些新功能。

可以使用栈模拟,也可以使用递归

忘了使用 $ python $ ,使用 $ eval $ 简单的一批

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
const double eqs = 1e-5;
const int maxn = 1e3 + 10;
const int maxm = 1e5 + 10;
int t, n, m, k;

#define MAX_L 1005 //最大长度,可以修改
class bign
{
public:
int len, s[MAX_L]; //数的长度,记录数组
//构造函数
bign();
bign(const char *);
bign(int);
bool sign; //符号 1正数 0负数
string toStr() const; //转化为字符串,主要是便于输出
friend istream &operator>>(istream &, bign &); //重载输入流
friend ostream &operator<<(ostream &, const bign &); //重载输出流
//重载复制
bign operator=(const char *);
bign operator=(int);
bign operator=(const string);
//重载各种比较
bool operator>(const bign &) const;
bool operator>=(const bign &) const;
bool operator<(const bign &) const;
bool operator<=(const bign &) const;
bool operator==(const bign &) const;
bool operator!=(const bign &) const;
//重载四则运算
bign operator+(const bign &) const;
bign operator++();
bign operator++(int);
bign operator+=(const bign &);
bign operator-(const bign &) const;
bign operator--();
bign operator--(int);
bign operator-=(const bign &);
bign operator*(const bign &)const;
bign operator*(const int num) const;
bign operator*=(const bign &);
bign operator/(const bign &) const;
bign operator/=(const bign &);
//四则运算的衍生运算
bign operator%(const bign &) const; //取模(余数)
bign factorial() const; //阶乘
bign Sqrt() const; //整数开根(向下取整)
bign pow(const bign &) const; //次方
//一些乱乱的函数
void clean();
~bign();
};
#define max(a, b) a > b ? a : b
#define min(a, b) a < b ? a : b
bign::bign()
{
memset(s, 0, sizeof(s));
len = 1;
sign = 1;
}
bign::bign(const char *num) { *this = num; }
bign::bign(int num) { *this = num; }
string bign::toStr() const
{
string res;
res = "";
for (int i = 0; i < len; i++)
res = (char)(s[i] + '0') + res;
if (res == "")
res = "0";
if (!sign && res != "0")
res = "-" + res;
return res;
}
istream &operator>>(istream &in, bign &num)
{
string str;
in >> str;
num = str;
return in;
}
ostream &operator<<(ostream &out, const bign &num)
{
out << num.toStr();
return out;
}
bign bign::operator=(const char *num)
{
memset(s, 0, sizeof(s));
char a[MAX_L] = "";
if (num[0] != '-')
strcpy(a, num);
else
for (int i = 1; i < strlen(num); i++)
a[i - 1] = num[i];
sign = !(num[0] == '-');
len = strlen(a);
for (int i = 0; i < strlen(a); i++)
s[i] = a[len - i - 1] - 48;
return *this;
}
bign bign::operator=(int num)
{
char temp[MAX_L];
sprintf(temp, "%d", num);
*this = temp;
return *this;
}
bign bign::operator=(const string num)
{
const char *tmp;
tmp = num.c_str();
*this = tmp;
return *this;
}
bool bign::operator<(const bign &num) const
{
if (sign ^ num.sign) // 符号不同
return num.sign;
if (len != num.len) // 符号同
return sign ? (len < num.len) : (!(len < num.len));

for (int i = len - 1; i >= 0; i--)
if (s[i] != num.s[i])
return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
return false;
}
bool bign::operator>(const bign &num) const { return num < *this; }
bool bign::operator<=(const bign &num) const { return !(*this > num); }
bool bign::operator>=(const bign &num) const { return !(*this < num); }
bool bign::operator!=(const bign &num) const { return *this > num || *this < num; }
bool bign::operator==(const bign &num) const { return !(num != *this); }
bign bign::operator+(const bign &num) const
{
if (sign ^ num.sign)
{
bign tmp = sign ? num : *this;
tmp.sign = 1;
return sign ? *this - tmp : num - tmp;
}
bign result;
result.len = 0;
int temp = 0;
for (int i = 0; temp || i < (max(len, num.len)); i++)
{
int t = s[i] + num.s[i] + temp;
result.s[result.len++] = t % 10;
temp = t / 10;
}
result.sign = sign;
return result;
}
bign bign::operator++()
{
*this = *this + 1;
return *this;
}
bign bign::operator++(int)
{
bign old = *this;
++(*this);
return old;
}
bign bign::operator+=(const bign &num)
{
*this = *this + num;
return *this;
}
bign bign::operator-(const bign &num) const
{
bign b = num, a = *this;
if (!num.sign && !sign)
{
b.sign = 1;
a.sign = 1;
return b - a;
}
if (!b.sign)
{
b.sign = 1;
return a + b;
}
if (!a.sign)
{
a.sign = 1;
b = bign(0) - (a + b);
return b;
}
if (a < b)
{
bign c = (b - a);
c.sign = false;
return c;
}
bign result;
result.len = 0;
for (int i = 0, g = 0; i < a.len; i++)
{
int x = a.s[i] - g;
if (i < b.len)
x -= b.s[i];
if (x >= 0)
g = 0;
else
{
g = 1;
x += 10;
}
result.s[result.len++] = x;
}
result.clean();
return result;
}
bign bign::operator*(const bign &num) const
{
bign result;
result.len = len + num.len;

for (int i = 0; i < len; i++)
for (int j = 0; j < num.len; j++)
result.s[i + j] += s[i] * num.s[j];

for (int i = 0; i < result.len; i++)
{
result.s[i + 1] += result.s[i] / 10;
result.s[i] %= 10;
}
result.clean();
result.sign = !(sign ^ num.sign);
return result;
}
bign bign::operator*(const int num) const
{
bign x = num;
bign z = *this;
return x * z;
}
bign bign::operator*=(const bign &num)
{
*this = *this * num;
return *this;
}
bign bign::operator/(const bign &num) const
{
bign ans;
ans.len = len - num.len + 1;
if (ans.len < 0)
{
ans.len = 1;
return ans;
}

bign divisor = *this, divid = num;
divisor.sign = divid.sign = 1;
int k = ans.len - 1;
int j = len - 1;
while (k >= 0)
{
while (divisor.s[j] == 0)
j--;
if (k > j)
k = j;
char z[MAX_L];
memset(z, 0, sizeof(z));
for (int i = j; i >= k; i--)
z[j - i] = divisor.s[i] + '0';
bign dividend = z;
if (dividend < divid)
{
k--;
continue;
}
int key = 0;
while (divid * key <= dividend)
key++;
key--;
ans.s[k] = key;
bign temp = divid * key;
for (int i = 0; i < k; i++)
temp = temp * 10;
divisor = divisor - temp;
k--;
}
ans.clean();
ans.sign = !(sign ^ num.sign);
return ans;
}
bign bign::operator/=(const bign &num)
{
*this = *this / num;
return *this;
}
bign bign::operator%(const bign &num) const
{
bign a = *this, b = num;
a.sign = b.sign = 1;
bign result, temp = a / b * b;
result = a - temp;
result.sign = sign;
return result;
}
bign bign::pow(const bign &num) const
{
bign result = 1;
for (bign i = 0; i < num; i++)
result = result * (*this);
return result;
}
bign bign::factorial() const
{
bign result = 1;
for (bign i = 1; i <= *this; i++)
result *= i;
return result;
}
void bign::clean()
{
if (len == 0)
len++;
while (len > 1 && s[len - 1] == '\0')
len--;
}
bign bign::Sqrt() const
{
if (*this < 0)
return -1;
if (*this <= 1)
return *this;
bign l = 0, r = *this, mid;
while (r - l > 1)
{
mid = (l + r) / 2;
if (mid * mid > *this)
r = mid;
else
l = mid;
}
return l;
}
bign::~bign() {}

inline bign quickmi(ll xx, ll n)
{
bign x = xx, res = 1;
for (; n; n >>= 1)
{
if (n & 1)
res *= x;
x *= x;
}
return res;
}

stack<bign> s;
string str;

bign solve()
{
bign ans = 0, sum = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == ')')
{
sum = 0;
while (s.top() != -1)
{
sum += s.top();
s.pop();
}
s.pop();
s.pop();
bign tmp = 1;
while (sum > 0)
{
sum = sum - 1;
tmp = tmp * 2;
}

s.push(tmp);
}
else if (str[i] == '(')
{
s.push(-1);
}
else if (str[i] != '+')
{
s.push(str[i] - '0');
}
}

while (!s.empty())
{
ans = ans + s.top();
s.pop();
}
return ans;
}
int main()
{

#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
cin >> str;
cout << solve() << endl;

return 0;
}
1
print(eval(input().replace('(', '**(')))

B. Groundhog and Apple Tree

题目描述:

一棵树, $ n $ 个点,每个点有一个苹果。每经过一条边有一个花费 $ w $ 。初始在 $ 1 $ 号点, $ hp = 0 $ ,然后可以休息回血,去采苹果,每得到一个苹果回复 $ a_i $ 血量。每条边最多经过两次,问采完所有的苹果回到起点的最小休息时间。

数据范围:
$ 1\le T \le 1000 \\ 1\le n \le 10^5 , \sum n \le 10^6 \\ 0\le a_i, w_i \lt 2 ^ {31} $

题解:

image-20200809123808408

访问一个子树返回的 $ hp $ 是一个定值,所以跟子树的访问顺序有关。对于每一棵子树,可以维护每一棵子树的得到的 $ hp $ 和花费的 $ hp $ ,根据这两个值排序。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
const double eqs = 1e-5;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
int read()
{
int x = 0, flag = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
flag = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
if (flag)
return -x;
return x;
}
int t, n, m, k;
int a[maxn];

struct Edge
{
int v, w, next;
} edge[maxm << 1];
int head[maxn], cnt;
bool vis[maxn];
inline void addEdge(int u, int v, int w)
{
++cnt;
edge[cnt] = {v, w, head[u]};
head[u] = cnt;
}

struct Node
{
int id;
ll c, g;
} dp[maxn];

bool cmp(Node a, Node b)
{
if (a.g > a.c)
if (b.g > b.c)
return a.c < b.c;
else
return true;
else if (b.g > b.c)
return false;
else
return a.g > b.g;
}

void dfs(int u, int p)
{
vector<Node> comp;
for(int i = head[u]; i; i = edge[i].next)
{
int v = edge[i].v;
if(v == p)
continue;
dfs(v, u);
dp[v].id = v;
dp[v].c += edge[i].w;
dp[v].g -= edge[i].w;
if(dp[v].g < 0)
dp[v].c -= dp[v].g, dp[v].g = 0;
comp.push_back(dp[v]);
}
sort(comp.begin(), comp.end(), cmp);

dp[u].c = 0;
ll now = a[u];
for(int i = 0; i < comp.size(); i++)
{
auto v = comp[i];
now -= v.c;
if(now < 0)
{
dp[u].c -= now;
now = 0;
}
now += v.g;
}
dp[u].g = now;
}

int main()
{
// #define COMP_DATA
#ifndef ONLINE_JUDGE
#ifdef COMP_DATA
freopen("/mnt/f/文档/课件/ACM比赛/2020hdu多校/第四场/data/E.in", "r", stdin);
freopen("out.txt", "w", stdout);
#else
freopen("in.txt", "r", stdin);
#endif
#endif
t = read();
while (t--)
{
cnt = 0;
n = read();
for (int i = 1; i <= n; i++)
a[i] = read(), head[i] = 0;

for (int i = 1, u, v, w; i < n; i++)
{
u = read(), v = read(), w = read();
addEdge(u, v, w), addEdge(v, u, w);
}
dfs(1, 0);
printf("%lld\n", dp[1].c);
}
return 0;
}

C.

题目描述:

数据范围:

题解:

代码:

1
2


D.

题目描述:

数据范围:

题解:

代码:

1
2


E. Groundhog Chasing Death

题目描述:

计算

数据范围:
$ 0\le a, b, c, d \le 3 \times 10^6 \\ 0 \lt x, y \le 10^9 \\ a\le b, c \le d $

题解:

肯定是需要分解质因数,然后只计算每个公因数的幂次的和,最后再快速幂。

打表发现公共质因数非常少 $ 10 $ 个左右

循环这些公共质因数,然后循环 $ a-b $ 或 $ c - d $ ,求另一个,根据幂次关系比较容易求,最后记得使用欧拉降幂,因为模数为质数,所以 $ \% (mod - 1) $ ,最后求快速幂,否则的话会暴 $ long \ long $ ,会 $ TLE $

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
ll read()
{
ll x = 0; char ch = getchar(); bool f = 0;
for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') f = 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
return f? -x : x;
}

int x, y, a, b, c, d;
int p[30], q[30], r[30];
int f[30], g[30], s[30], t[30];
ll ans;

ll pow(int x, ll y)
{
ll res = 1, tmp = x;
for(; y; y >>= 1)
{
if(y & 1) (res *= tmp) %= mod;
(tmp *= tmp) %= mod;
}
return res;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
a = read(); b = read(); c = read(); d = read();
x = read(); y = read();
int k = 0;
for(int i = 2, lim = sqrt(x); i <= lim; ++i)
{
if(x % i == 0) p[++k] = i;
while(x % i == 0) x /= i, ++f[k];
}
if(x > 1) {p[++k] = x; f[k] = 1;}
p[0] = k; k = 0;
for(int i = 2, lim = sqrt(y); i <= lim; ++i)
{
if(y % i == 0) q[++k] = i;
while(y % i == 0) y /= i, ++g[k];
}
if(y > 1) {q[++k] = y; g[k] = 1;}
q[0] = k; k = 0;
for(int i = 1, j = 1; i <= p[0]; ++i)
{
while(j <= q[0] && p[i] > q[j]) ++j;
if(j > q[0]) break;
if(p[i] == q[j])
{
r[++k] = p[i];
s[k] = i;
t[k] = j;
}
}
r[0] = k;
ans = 1;
ll ans = 1;
for(int i = 1, u, v; i <= k; ++i)
{
u = s[i]; v = t[i];
ll sum = 0;
for(int j = c, l; j <= d; ++j)
{
l = j*g[v]/f[u];
l = max(l, a-1);
l = min(l, b);
sum = (sum + (1ll * j * g[v] *(b-l) + 1ll*f[u]*(a+l) * (l-a+1) / 2 )) % (mod - 1); // 降幂
}
ans = (ans * pow(r[i], sum)) % mod;
}
printf("%lld", ans);
return 0;
}

F. Groundhog Looking Dowdy

题目描述:

给出 $ n $ 天,每天有 $ k_i $ 个数,从 $ n $ 天中选出 $ m $ 天,这 $ m $ 天每天选出一个数,使这组数的最大值和最小值差值最小

数据范围:
$ 1\le a_{i,j} \le 10^9 \\ 1 \lt n \le 10^6 \\ 1 \le m \le n \\ \sum clothes \le 2 \times 10^6 \\ k_i \ge 1 $

题解:

吐槽题面,难懂的一批

最大值最小值差值最小,可以直接将所有的数字排个序。则合法的为区间至少有 $ m $ 个不同的天。答案为 $ a[r] - a[l] $ ,区间不断滑动就行了

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
const double eqs = 1e-5;
const int maxn = 2e6 + 10;
const int maxm = 1e5 + 10;
int read()
{
int x = 0, flag = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
flag = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
if (flag)
return -x;
return x;
}

int t, n, m, k;

struct Node
{
int a, id;
bool operator<(const Node& x) const
{
return a < x.a;
}
}node[maxn];
int tot[maxn];
int main()
{
// #define COMP_DATA
#ifndef ONLINE_JUDGE
#ifdef COMP_DATA
freopen("/mnt/f/文档/课件/ACM比赛/2020hdu多校/第四场/data/E.in", "r", stdin);
freopen("out.txt", "w", stdout);
#else
freopen("in.txt", "r", stdin);
#endif
#endif
n = read();
m = read();
int k;
int cnt = 0;
for (int i = 1; i <= n; i++)
{
k = read();
while(k--)
{
node[++cnt].a = read();
node[cnt].id = i;
}
}
sort(node + 1, node + 1 + cnt);
int l = 1, r = 1;
int p = 0;
int ans = 1e9;
while (r <= cnt && l <= r)
{
while(p < m && r <= cnt)
{
if(!tot[node[r].id])
{
p++;
}
tot[node[r].id]++;
r++;
}
while (p == m && l <= r)
{

if (--tot[node[l].id] == 0)
{
p--;
}
l++;
}
ans = min(ans, node[r - 1].a - node[l - 1].a);
}
printf("%d\n", ans);
return 0;
}

G.

题目描述:

数据范围:

题解:

代码:

1
2


H.

题目描述:

数据范围:

题解:

代码:

1
2


I. The Crime-solving Plan of Groundhog

题目描述:

给出一串只有 $ 0-9 $ 的字符,构造两个数乘积最小

数据范围:
$ 1\le T \le 1000 \\ 2\le n \le 100000 \\ 1\le \sum n \le 1000000 $

题解:

拆成一个数和其他的这样最小

image-20200808223058079

最小的数一个,第二小的作大数的第一位,然后将零全放在第二位,剩下的是从小到大排列的数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>

using namespace std;
using longs = long long;
using uint = unsigned;

inline int nextInt()
{
int x = 0, f = 0, ch = getchar();
while (!isdigit(ch)) ch == '-' && (f = 1), ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
return f ? -x : x;
}

const int N = 1e5 + 5;
int cnt[10];

int main()
{
ios::sync_with_stdio(false);

int t = nextInt();
while (t --)
{
memset(cnt, 0, sizeof cnt);
int n = nextInt(), xx = 0, yy = 0;
while (n --) ++ cnt[nextInt()];
for (int i = 1; i < 10; ++ i)
if (cnt[i]) {-- cnt[i], xx = i; break;}
for (int i = 1; i < 10; ++ i)
if (cnt[i]) {-- cnt[i], yy = i; break;}
string s;
s.append(1, yy + '0');
s.append(cnt[0], '0');
for (int i = 1; i < 10; ++ i)
s.append(cnt[i], i + '0');
int top = 0, len = s.length();
while (-- len >= 0)
{
auto _this = s[len] - '0';
auto _tmp = _this * xx + top;
top = _tmp / 10;
s[len] = _tmp % 10 + '0';
}
if (top) printf("%d", top);
printf("%s\n", s.c_str());
}

return 0;
}

J. The Escape Plan of Groundhog

题目描述:

给出一个 $ 0,1 $ 矩阵。然后需要找出四周全是 $ 1 $ ,中间部分 $ 0 $ 和 $ 1 $ 的数量相差不超过 $ 1 $ 的子矩阵个数。

数据范围:
$ 1\le n,m \le 500 $

题解:

枚举上下边界,然后对列扫一遍。扫的时候用一个前缀和维护前面的所有的值。如果遇到前面存在前缀和和他相差 $ 1 $ 或 $ 0 $ 直接计入答案。

注意将 $ 0 $ 看作 $ -1 $

image-20200809120601569

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
const double eqs = 1e-5;
const int maxn = 25e4 + 10;
const int maxm = 5e2 + 10;
int read()
{
int x = 0, flag = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
flag = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
if (flag)
return -x;
return x;
}
int t, n, m, k;
int a[maxm][maxm];
int sum[maxm][maxm];
int tot[maxn << 1];
int s[maxn];
int main()
{
// #define COMP_DATA
#ifndef ONLINE_JUDGE
#ifdef COMP_DATA
freopen("/mnt/f/文档/课件/ACM比赛/2020hdu多校/第四场/data/E.in", "r", stdin);
freopen("out.txt", "w", stdout);
#else
freopen("in.txt", "r", stdin);
#endif
#endif
n = read(), m = read();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
a[i][j] = read();
if(!a[i][j])
a[i][j] = -1;
sum[i][j] = sum[i - 1][j] + a[i][j];
}
ll ans = 0;
s[0] = maxn;
for (int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
int pre = 1;
for(int k = 1; k <= m; k++)
{
if(a[i][k] != 1 || a[j][k] != 1)
{
for(int l = pre; l <= k; l++)
{
if(sum[j][l] - sum[i-1][l] == j - i + 1)
{
tot[s[l]]--;
}
}
pre = k + 1;
s[k] = maxn;
continue;
}
if(sum[j][k] - sum[i-1][k] == j - i + 1)
{
ans += tot[s[k - 1]] + tot[s[k - 1] + 1] + tot[s[k - 1] - 1];
}
s[k] = s[k - 1] + sum[j - 1][k] - sum[i][k];
if (sum[j][k] - sum[i - 1][k] == j - i + 1)
tot[s[k]]++;
}

for (int l = pre; l <= m; l++)
{
if (sum[j][l] - sum[i - 1][l] == j - i + 1)
{
tot[s[l]]--;
}
}
}
}

printf("%lld\n", ans);
return 0;
}

k. The Flee Plan of Groundhog

题目描述:

一棵无根树 $ n $ 个点, $ n-1 $ 条边。所有的边都是 $ 1 $

$ A $ 在 $ 1 $ 号点, $ B $ 在 $ n $ 号点, $ A $ 的速度为 $ 1m/s $ , $ B $ 的速度为 $ 2m/s $ 。然后 $ A $ 先出发 $ ts $ 向着 $ n $ 运动,然后 $ t $ 后, $ B $ 出发追赶 $ A $ ,问最晚追上的时间。

数据范围:
$ 1\le n \le 10^5 \\ 1 \le t \le n - 1 \\ 1\le x, y \le n $

题解:

首先求出 $ A $ 在 $ t $ 后的位置,然后在该位置求一遍最短路, $ B $ 也求一遍最短路,枚举所有的点判断时间即可。

最短路可以使用 $ bfs $ 比较快,但是我直接拿的板子交的 $ dijkstra $

这里再补一个 $ O(n) $ 的,使用 $ dfs $ 就行了。

向上取整 $ x/2 + x \& 1 $

向下取整 $ x/2 $

四舍五入 $ x * 1.0 / 2 + 0.5 $

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
const double eqs = 1e-5;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
int read()
{
int x = 0, flag = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
flag = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
if (flag)
return -x;
return x;
}
int t, n, m, k;

int head[maxn], cnt;
struct Edge
{
int v, next;
} edge[maxm << 1];
inline void addEdge(int u, int v)
{
++cnt;
edge[cnt] = {v, head[u]};
head[u] = cnt;
}
int dis[2][maxn];
int path[maxn];
void dfs(int u, int p, int id)
{
for (int i = head[u]; i; i = edge[i].next)
{
int v = edge[i].v;
if(v == p) continue;
dis[id][v] = dis[id][u] + 1;
path[v] = u;
dfs(v, u, id);
}
}
int main()
{
// #define COMP_DATA
#ifndef ONLINE_JUDGE
#ifdef COMP_DATA
freopen("/mnt/f/文档/课件/ACM比赛/2020hdu多校/第四场/data/E.in", "r", stdin);
freopen("out.txt", "w", stdout);
#else
freopen("in.txt", "r", stdin);
#endif
#endif
n = read(), t = read();
int u, v;
for (int i = 1; i < n; i++)
{
u = read(), v = read();
addEdge(u, v);
addEdge(v, u);
}
memset(dis[1], 0x3f, sizeof(dis[1]));
dis[1][n] = 0;
dfs(n, -1, 1);
if(dis[1][1] <= t)
{
printf("0\n");
}
else
{
int now = 1;
while(t--)
{
now = path[now];
}
memset(dis[0], 0x3f, sizeof(dis[0]));
dis[0][now] = 0;
dfs(now, -1, 0);

int maxx = 0;
for (int i = 1; i <= n; i++)
{
if (dis[0][i] * 2 <= dis[1][i])
{
maxx = max(maxx, dis[1][i] / 2 + (dis[1][i] & 1));
}
}
printf("%d\n", maxx);
}
return 0;
}