Codeforces Round 809(Div.2)

By xiaruize

A. Another String Minimization Problem

贪心,考虑优先改左侧的,如果左侧已经为A,则修改右侧的

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
// Problem: A. Another String Minimization Problem
// Contest: Codeforces Round #809 (Div. 2)
// URL: https://codeforces.com/contest/1706/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 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;
int a[55];
string res;
int t;
// bool en;

void solve()
{
res = "";
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++)
res += "B";
for (int i = 1; i <= n; i++)
{
if (a[i] < m - a[i] + 1)
{
if (res[a[i] - 1] == 'B')
res[a[i] - 1] = 'A';
else
res[m - a[i]] = 'A';
}
else
{
if (res[m - a[i]] == 'B')
res[m - a[i]] = 'A';
else
res[a[i] - 1] = 'A';
}
}
cout << res << endl;
}

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 >> t;
while (t--)
solve();
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

B. Making Towers

对于同样颜色的两块,如果两者之间隔偶数块(即坐标只差为奇数),两块就可以满足上下堆叠,这需要统计奇偶性即可

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
// Problem: B. Making Towers
// Contest: Codeforces Round #809 (Div. 2)
// URL: https://codeforces.com/contest/1706/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 debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 1e5 + 10;

// bool st;
int t;
int n;
int c[N];
int cnt[N];
int b[N];
// bool en;

void solve()
{
cin >> n;
memset(cnt, 0, sizeof(cnt));
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++)
{
cin >> c[i];
if (!cnt[c[i]] || i % 2 != b[c[i]] % 2)
cnt[c[i]]++, b[c[i]] = i;
}
for (int i = 1; i <= n; i++)
cout << cnt[i] << ' ';
cout << endl;
}

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 >> t;
while (t--)
solve();
// 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. Qpwoeirut And The City

nn的奇偶性分情况考虑

  1. n为奇数时 答案为把h2,h4,...,hn1h_2,h_4,...,h_{n-1}调整到合适高度的最小代价

  2. n为偶数时 有且仅有一对相邻的两栋楼均不是cool的,且这一对一定出现在(2k1,2k)(2k-1,2k)(k为正整数)

    枚举每一对可能的间隔,统计答案

    查询的过程可以通过前缀和,后缀和实现

    复杂度O(n)O(n)

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
// Problem: C. Qpwoeirut And The City
// Contest: Codeforces Round #809 (Div. 2)
// URL: https://codeforces.com/contest/1706/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;
#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 = 1e5 + 10;

// bool st;
int t;
int n;
int a[N];
int pre[N];
int suf[N];
int r[N];
// bool en;

void solve()
{
memset(pre, 0, sizeof(pre));
memset(suf, 0, sizeof(suf));
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 2; i <= n; i++)
{
r[i] = max(max(a[i - 1], a[i + 1]) - a[i] + 1, 0ll);
pre[i] = pre[i - 2] + r[i];
}
for (int i = n - 1; i >= 0; i--)
suf[i] = suf[i + 2] + r[i];
int res = INF;
res = min(pre[n - 1], suf[2]);
for (int i = 1; i <= n - 2; i += 2)
res = min(res, pre[i - 1] + suf[i + 2]);
if (n % 2 == 0)
cout << res << endl;
else
cout << pre[n - 1] << endl;
}

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 >> t;
while (t--)
solve();
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

D1. Chopping Carrots (Easy Version)

首先,可以考虑枚举最小值,此时要使得最大值最小,可以使用set维护每个数的商,此时可以lower_bound查询
时间复杂度: O(nk+na1logk)O(nk+na_1logk)

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
// Problem: D1. Chopping Carrots (Easy Version)
// Contest: Codeforces Round #809 (Div. 2)
// URL: https://codeforces.com/contest/1706/problem/D1
// Memory Limit: 64 MB
// Time Limit: 4000 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;
int a[3005];
set<int> s[3005];
int minn = INF;
int t;
// bool en;

void solve()
{
cin >> n >> k;
minn = INF;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
s[i].clear();
minn = min(minn, a[i]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
s[i].insert(a[i] / j);
}
int res = INF;
for (int i = 0; i <= minn; i++)
{
int mx = 0, mi = INF;
for (int j = 1; j <= n; j++)
{
auto x = s[j].lower_bound(i);
mx = max(mx, *x);
mi = min(mi, *x);
}
res = min(res, mx - mi);
}
cout << res << endl;
}

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 >> t;
while (t--)
solve();
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

D2. Chopping Carrots (Hard Version)

考虑优化上面的程序

处理m数组表示最小值为i时的最大值的最小值

时间复杂度: O(n)O(n)

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
// Problem: D2. Chopping Carrots (Hard Version)
// Contest: Codeforces Round #809 (Div. 2)
// URL: https://codeforces.com/contest/1706/problem/D2
// Memory Limit: 64 MB
// Time Limit: 4000 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 = 1e5 + 10;

// bool st;
int n, k;
int a[N];
int s[N];
// bool en;

void solve()
{
memset(s, 0, sizeof(s));
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
int tmp = INF;
for (int j = 1; j <= min(a[i], k); j = (a[i] / (a[i] / j) + 1))
{
s[a[i] / j + 1] = max(s[a[i] / j + 1], tmp);
tmp = a[i] / j;
}
s[0] = max(s[0], tmp);
}
int res = INF, now = 0;
for (int i = 0; i <= a[1]; i++)
{
now = max(now, s[i]);
res = min(res, now - i);
}
cout << res << endl;
}

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();
int t;
cin >> t;
while (t--)
solve();
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

未完待更