Atcoder Beginner Contest 260 Tutorial

By xiaruize

A - A Unique Letter

签到题

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
// Problem: A - A Unique Letter
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_a
// Memory Limit: 1024 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 ll long long
#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 multitest(t) \
while (t--) \
solve();
#define double long double
const int INF = 0x3f3f3f3f;
const int Mod = 1000000007;
const int N = 1e5 + 10;

inline int rd()
{
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
inline void wt(int x)
{
static int sta[35];
int top = 0;
do
{
sta[top++] = x % 10, x /= 10;
} while (x);
while (top)
putchar(sta[--top] + 48);
}

// bool st;
string s;
// bool en;

signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
cin >> s;
if (s[0] != s[1] && s[0] != s[2])
cout << s[0] << endl;
else if (s[1] != s[0] && s[1] != s[2])
cout << s[1] << endl;
else if (s[2] != s[0] && s[1] != s[2])
cout << s[2] << endl;
else
cout << "-1" << endl;
return 0;
}

B - Better Students Are Needed!

排序基础题,写三个cmp即可

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
// Problem: B - Better Students Are Needed!
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_b
// Memory Limit: 1024 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 debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n, x, y, z;
struct node
{
int m, e;
int sum;
int id;
bool st;
} s[N];
// bool en;

bool cmp(node a, node b)
{
if (a.m != b.m)
return a.m > b.m;
else
return a.id < b.id;
}

bool cmpp(node a, node b)
{
if (a.e != b.e)
return a.e > b.e;
else
return a.id < b.id;
}

bool cmppp(node a, node b)
{
if (a.sum != b.sum)
return a.sum > b.sum;
else
return a.id < b.id;
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n >> x >> y >> z;
for (int i = 1; i <= n; i++)
cin >> s[i].m;
for (int i = 1; i <= n; i++)
{
cin >> s[i].e;
s[i].sum = s[i].e + s[i].m;
s[i].id = i;
s[i].st = false;
}
vector<int> res;
sort(s + 1, s + n + 1, cmp);
int cnt = 0, p = 1;
while (cnt < x && p <= n)
{
while (s[p].st)
p++;
s[p].st = 1;
cnt++;
res.pb(s[p].id);
p++;
}
sort(s + 1, s + n + 1, cmpp);
cnt = 0, p = 1;
while (cnt < y && p <= n)
{
while (s[p].st)
p++;
s[p].st = 1;
cnt++;
res.pb(s[p].id);
p++;
}
sort(s + 1, s + n + 1, cmppp);
cnt = 0, p = 1;
while (cnt < z && p <= n)
{
while (s[p].st)
p++;
s[p].st = 1;
cnt++;
res.pb(s[p].id);
p++;
}
sort(ALL(res));
for (auto v : res)
cout << v << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

C - Changing Jewels

模拟题,显然最大值就是一直操作到无法操作的值

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
// Problem: C - Changing Jewels
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_c
// Memory Limit: 1024 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 debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n, x, y;
// bool en;

int dfs(bool st, int lv)
{
// cerr << st << ' ' << lv << endl;
if (lv == 1)
{
if (st)
return 0;
else
return 1;
}
if (st)
{
int res = 0;
res += dfs(st, lv - 1);
res += x * dfs(0, lv);
return res;
}
else
{
int res = 0;
res += dfs(1, lv - 1);
res += y * dfs(0, lv - 1);
return res;
}
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n >> x >> y;
cout << dfs(1, n) << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

D - Draw Your Cards

明显可以使用set完成加减和查询比x小的最大元素的操作,模拟即可

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
// Problem: D - Draw Your Cards
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_d
// Memory Limit: 1024 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 debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n, k, p[200005], res[200005];
int tp[200005], ys[200005];
set<int> s;
vector<int> st[200005];
// bool en;

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n >> k;
memset(res, -1, sizeof(res));
for (int i = 1; i <= n; i++)
{
cin >> p[i];
if (s.empty())
{
tp[i] = p[i];
ys[p[i]] = i;
st[i].pb(p[i]);
s.insert(p[i]);
}
else
{
auto it = s.lower_bound(p[i]);
if (it == s.end())
{
tp[i] = p[i];
ys[p[i]] = i;
st[i].pb(p[i]);
s.insert(p[i]);
}
else
{
ys[p[i]] = ys[*it];
tp[ys[p[i]]] = p[i];
st[ys[p[i]]].pb(p[i]);
s.erase(it);
s.insert(p[i]);
}
}
if (st[ys[p[i]]].size() == k)
{
for (auto x : st[ys[p[i]]])
res[x] = i;
st[ys[p[i]]].clear();
s.erase(tp[ys[p[i]]]);
}
}
for (int i = 1; i <= n; i++)
cout << res[i] << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

E - At Least One

处理一个数组CiC_i表示如果不选择第i位及以前的数,右边界至少为多少。
可以O(n)处理,CA[i]=max(CA[i],B[i])C_{A[i]}=max(C_{A[i]},B[i])
然后可以枚举区间的起点,差分处理答案

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
// Problem: E - At Least One
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_e
// Memory Limit: 1024 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 debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n, m;
pii a[N];
int p[N];
int c[N];
int mi = INF;
// bool en;

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i].fir >> a[i].sec;
int j = 0;
for (int i = 1; i <= n; i++)
{
c[a[i].fir] = max(c[a[i].fir], a[i].sec);
j = max(j, a[i].fir);
mi = min(a[i].sec, mi);
}
for (int i = 1; i <= mi; i++)
{
p[j - i + 1]++;
p[m - i + 2]--;
j = max(j, c[i]);
}
int res = 0;
for (int i = 1; i <= m; i++)
{
res += p[i];
cout << res << ' ';
}
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

F - Find 4-cycle

似乎暴力就能过,这是枚举的时候可以记录一下。。。

wtcl
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
// Problem: F - Find 4-cycle
// Contest: AtCoder - AtCoder Beginner Contest 260
// URL: https://atcoder.jp/contests/abc260/tasks/abc260_f
// Memory Limit: 1024 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 debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 3e5 + 10;

// bool st;
int s, t, m;
vector<int> g[N];
int mp[3005][3005];
// bool en;

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> s >> t >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].pb(v - s);
}
for (int i = 1; i <= s; i++)
{
for (auto &j : g[i])
{
for (auto &l : g[i])
{
if (j == l)
continue;
if (mp[j][l])
{
cout << mp[j][l] << ' ' << j + s << ' ' << i << ' ' << l + s << endl;
return 0;
}
else
mp[j][l] = i;
}
}
}
cout << "-1" << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

理论上,时间复杂度有可能达到O(st2)O(s \cdot t^2),但是实际上跑的飞快qwq

未完待更