Codeforces Round #780 (Div. 3)

By xiaruize

My Performance

水调歌头
苏轼

丙辰中秋,欢饮达旦,大醉,作此篇,兼怀子由。
明月几时有?把酒问青天。
不知天上宫阙,今夕是何年?
我欲乘风归去,又恐琼楼玉宇,高处不胜寒。
起舞弄清影,何似在人间?

转朱阁,低绮户,照无眠。
不应有恨,何事长向别时圆?
人有悲欢离合,月有阴晴圆缺,此事古难全。
但愿人长久,千里共婵娟。

Codeforces rating涨了 qwq

A. Vasya and Coins

Method

分两种情况:

情况一

他有1\geq1个1-buble的硬币

ans=a+2bans=a+2b

情况二

他没有1-buble的硬币

则他一定付不出1-buble这个金额

ans=1\therefore ans=1

Code

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
// Problem: A. Vasya and Coins
// Contest: Codeforces Round #780 (Div. 3)
// URL: https://codeforces.com/contest/1660/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 1e5 + 10;

// bool st;
int t;
// bool en;

void solve()
{
int a, b;
cin >> a >> b;
if (a == 0)
cout << "1" << endl;
else
cout << a + 2 * b + 1 << endl;
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
cin >> t;
while (t--)
solve();
return 0;
}

B. Vlad and Candies

Method

每次操作,我们优先降低最大值,只要最大值和次大值的差1\leq1就一定可以完成

Code

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
// Problem: B. Vlad and Candies
// Contest: Codeforces Round #780 (Div. 3)
// URL: https://codeforces.com/contest/1660/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int t;
int n;
int a[N];
int mx, smx;
// bool en;

void solve()
{
mx = smx = 0;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
{
if (a[i] > mx)
{
smx = mx;
mx = a[i];
}
else if (a[i] > smx)
smx = a[i];
}
if (mx - smx <= 1)
YES;
else
NO;
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".vout","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
cin >> t;
while (t--)
solve();
return 0;
}

C. Get an Even String

Feeling

赛时在这题上卡了很久。。。

Method

贪心策略:

按顺序遍历,如果存在配对则一定优先选取。

Code

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
// Problem: C. Get an Even String
// Contest: Codeforces Round #780 (Div. 3)
// URL: https://codeforces.com/contest/1660/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int t;
string s;
int la[30];
int now = 0;
int ans = 0;
// bool en;

void solve()
{
ans = 0;
now = 0;
s = "";
memset(la, 0, sizeof(la));
cin >> s;
s = ' ' + s;
for (int i = 1; i < s.size(); i++)
{
if (la[s[i] - 'a'] && now < la[s[i] - 'a'])
{
for (int j = max(1, now); j < i; j++)
la[s[j] - 'a'] = 0;
ans += i - now - 2;
now = i;
la[s[i] - 'a'] = 0;
}
else
{
la[s[i] - 'a'] = i;
}
// cerr << i << ' ' << ans << endl;
}
for (int i = 0; i < 26; i++)
if (la[i])
ans++;
cout << ans << endl;
return;
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
cin >> t;
while (t--)
solve();
return 0;
}

D. Maximum Product Strikes Back

Method

并不是很难,思路很简单

可以用每个00为界线,把数组分成几段,下面我们讨论对每一段求最大值:

因为全部删去时的值为11,所以任意一段的极值一定为正。

可以用前缀和维护负数的个数。(设到第ii位为sumisum_i)

对于每一段,可以O(n)O(n)找到包含区间最大的一对(i,j)(i,j)使得sumisumj(mod2)sum_i\equiv sum_j(mod 2)

可以证明区间[i+1,j]为当前段中的最优答案

2100longlongqwq2^{100}大于long long的范围,统计一定不要统计值qwq

Code

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
// Problem: D. Maximum Product Strikes Back
// Contest: Codeforces Round #780 (Div. 3)
// URL: https://codeforces.com/contest/1660/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int t;
int n;
int a[N];
int cnt[N];
int sum[N];
int fo, fe;
int lo, le;
// bool en;

void solve()
{
memset(sum, 0, sizeof(sum));
le = lo = fe = fo = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum[i] = sum[i - 1] + (a[i] == 2 || a[i] == -2);
}
int mx = 0;
pii ans;
for (int i = 1; i <= n; i++)
{
if (a[i] != 0)
{
cnt[i] = cnt[i - 1] + (a[i] < 0);
if (cnt[i] & 1 && !fo)
fo = i;
if (cnt[i] & 1)
lo = i;
else
le = i;
}
if (a[i] == 0)
{
if (fo && (sum[lo] - sum[fo]) > mx)
{
mx = (sum[lo] - sum[fo]);
ans = {fo, lo};
}
if ((sum[le] - sum[fe]) > mx)
{
mx = (sum[le] - sum[fe]);
ans = {fe, le};
}
fe = le = i;
fo = lo = 0;
cnt[i] = 0;
}
}
if (fo && (sum[lo] - sum[fo]) > mx)
{
mx = (sum[lo] - sum[fo]);
ans = {fo, lo};
}
if ((sum[le] - sum[fe]) > mx)
{
mx = (sum[le] - sum[fe]);
ans = {fe, le};
}
cout << ans.fir << ' ' << n - ans.sec << endl;
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
cin >> t;
while (t--)
solve();
return 0;
}

Happy-April-Fool’s-Day!