Atcoder Reguler Contest 146

By xiaruize

A - Three Cards

简单分析一下,我们发现最优解为在总长度最长的情况下使高位尽可能大

那么可以先 O(nlogn)O(nlogn) 找出长度最大的 33 个数

显然,如果有两个数的位数相同,应该优先选择更大的

此时,我们已经确定最终组成答案的数,可以从高位至低位做关键字排序

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
// Problem: A - Three Cards
// Contest: AtCoder - AtCoder Regular Contest 146
// URL: https://atcoder.jp/contests/arc146/tasks/arc146_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 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;
string s[N];
// bool en;

bool cmp2(string a, string b)
{
if (a.size() != b.size())
return a.size() < b.size();
for (int i = 0; i < min(a.size(), b.size()); i++)
{
if (a[i] < b[i])
return 1;
else if (a[i] > b[i])
return 0;
}
}
bool cmp(string a, string b)
{
for (int i = 0; i < min(a.size(), b.size()); i++)
{
if (a[i] < b[i])
return 1;
else if (a[i] > b[i])
return 0;
}
return a.size() > b.size();
}

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;
for (int i = 1; i <= n; i++)
cin >> s[i];
sort(s + 1, s + n + 1, cmp2);
sort(s + n - 2, s + n + 1, cmp);
for (int i = n; i >= n - 2; i--)
cout << s[i];
// 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 - Plus and AND

从高位到低位贪心,如果当前位可以满足,那么一定满足当前位(因为更低的位不可能带来更优解)

对于每一位,最优的选择顺序为先选当前位二进制下为11的,再选其余中只考虑更低位的数值最大者

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
// Problem: B - Plus and AND
// Contest: AtCoder - AtCoder Regular Contest 146
// URL: https://atcoder.jp/contests/arc146/tasks/arc146_b
// Memory Limit: 1024 MB
// Time Limit: 5000 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, k;
pii a[N];
bool vis[N];
// bool en;

bool check(int x)
{
int cnt = 0;
for (int i = 1; i <= n; i++)
a[i].fir &= (1 << (x + 1)) - 1;
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
{
if (vis[a[i].sec])
continue;
if (a[i].fir & (1 << x))
{
cnt++;
}
}
int sum = 0;
for (int i = n; i >= 1; i--)
{
if (vis[a[i].sec])
continue;
if (cnt < k && (!(a[i].fir & (1 << x))))
{
sum += (1 << x) - (a[i].fir & ((1 << x) - 1));
if (sum > m)
return false;
cnt++;
}
else if ((!(a[i].fir & (1 << x))))
{
vis[a[i].sec] = true;
}
}
if (sum > m)
return false;
m -= sum;
return true;
}

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 >> k;
for (int i = 1; i <= n; i++)
cin >> a[i].fir, a[i].sec = i;
int res = 0;
for (int i = 30; i >= 0; i--)
{
if (check(i))
{
for (int j = 1; j <= n; j++)
{
if (!vis[a[j].sec] && (((a[j].fir >> i) ^ 1) & 1))
a[j].fir = 0;
}
res += (1 << i);
}
}
cout << res << 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 - Even XOR

往一个已有满足条件的集合SS中加入一个元素的可能性为2N2S22^{N}-2^{|S|-2}

及有ii个元素的集合方案数fn=fi1×(2N2i2)if_n=\frac{f_{i-1}\times (2^N-2^{i-2})}{i}

其中ii为考虑这个数有ii个位置可以插入

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
/*
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 = 998244353;
const int N = 2e5 + 10;

// bool st;
int n;
// bool en;

int qpow(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1)
(res *= a) %= MOD;
a = a * a % MOD;
b >>= 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;
int tmp = 1, tot = qpow(2, n);
int now = tot, res = tot + 1;
for (int i = 1; i <= n; i++)
{
now = now * (tot - tmp) % MOD * qpow(i + 1, MOD - 2) % MOD;
now = (now % MOD + MOD) % MOD;
tmp = tmp * 2 % MOD;
res = (res + now) % MOD;
}
cout << res << 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 - >=<

很容易想到一个最优但不一定成立的状态,及所有数值均为11

那么我们来考虑如何使这个最优状态合法

发现题目可以转化为APi<XAQi<YA_{P_i}<X \lrArr A_{Q_i}<YAPi<X+1AQi<Y+1A_{P_i}<X+1 \lrArr A_{Q_i}<Y+1

这时,会发现,当出现>>的情况时,最优的操作一定是将较小的数变大至合法的最小值

可以对边按照XX排序,保证每一条边只会被用一次,所以复杂度为O(N+K)O(N+K)

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
/*
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, k;
struct node
{
int x, q, y;
};
vector<node> g[N];
int a[N];
int st[N];
queue<int> que;
// bool en;

bool cmp(node a, node b)
{
return a.x < b.x;
}

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 >> k;
for (int i = 1; i <= k; i++)
{
int p, x, q, y;
cin >> p >> x >> q >> y;
g[p].pb({x, q, y});
g[q].pb({y, p, x});
g[p].pb({x + 1, q, y + 1});
g[q].pb({y + 1, p, x + 1});
}
for (int i = 1; i <= n; i++)
{
a[i] = 1;
que.push(i);
sort(ALL(g[i]), cmp);
}
while (!que.empty())
{
int x = que.front();
que.pop();
for (int &i = st[x]; i < g[x].size(); i++)
{
node v = g[x][i];
if (a[x] < v.x)
break;
if (a[v.q] < v.y)
que.push(v.q);
a[v.q] = max(a[v.q], v.y);
if (a[v.q] > m)
{
cout << "-1" << endl;
return 0;
}
}
}
int res = 0;
for (int i = 1; i <= n; i++)
res += a[i];
cout << res << 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;
}