Skip to content

Commit 0b3645b

Browse files
committed
update Codechef Staters46 div4.md
1 parent 384c451 commit 0b3645b

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

codechef/Codechef Staters46 div4.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
### A
2+
```c++
3+
void solve() {
4+
ll a, b;
5+
cin >> a >> b;
6+
cout << a * b << endl;
7+
}
8+
9+
```
10+
### B
11+
```c++
12+
void solve() {
13+
ll a, b;
14+
cin >> a >> b;
15+
cout << max(0LL, b - a) << endl;
16+
}
17+
```
18+
19+
### C
20+
```c++
21+
void solve() {
22+
int n;
23+
cin >> n;
24+
map <int ,int> cnt;
25+
for (int i = 0; i < n; i++) {
26+
int a; cin >> a;
27+
cnt[a]++;
28+
}
29+
bool ok = 1;
30+
int N = 1e5;
31+
for (auto && [k, v]: cnt){
32+
if(v % k != 0){
33+
ok = 0;
34+
break;
35+
}
36+
}
37+
cout << (ok?"YES":"NO") << endl;
38+
}
39+
```
40+
41+
### D
42+
贪心,每从反转是将1的序列反转到最后面,如果是一片1可以在一次操作中完成
43+
```c++
44+
void solve() {
45+
int n;
46+
cin >> n;
47+
string s;
48+
cin >> s;
49+
s.erase(unique(all(s)), end(s));
50+
cout << count(all(s), '1') - (s.back() == '1') << endl;
51+
}
52+
```
53+
54+
### E
55+
这个特殊的交换,首先我们需要去考虑交换的距离,接着存下来这些操作的情况,然后求最大公约数,就能满足题意
56+
```c++
57+
void solve() {
58+
int n;
59+
cin >> n;
60+
vector <int> a(n);
61+
for (int i = 0; i < n; i++) {
62+
cin >> a[i];
63+
}
64+
auto b = a;
65+
sort(all(b));
66+
map <int, int> m;
67+
for (int i = 0; i < n; i++) {
68+
m[b[i]] = i;
69+
}
70+
vector <int> c;
71+
for (int i = 0; i < n; i++) {
72+
if(i != m[a[i]]){
73+
c.push_back(abs(m[a[i]] - i));
74+
}
75+
}
76+
int k = c[0];
77+
for (int i = 0; i < c.size(); i++) {
78+
k = gcd(k, c[i]);
79+
}
80+
cout << k << endl;
81+
}
82+
```
83+
### F
84+
需要观察一下性质,因为X, Y, Z存在一个至少一个数为奇数,由于A, B, C为质数,这意味着必须会有一个数是偶数,因此其中一个数只能为2.
85+
至此只需要分类讨论即可
86+
```c++
87+
void solve() {
88+
int x, y;
89+
cin >> x >> y;
90+
//A is odd and B is even
91+
if (x % 2 == 0 && y % 2 == 1) swap(x, y);
92+
if (x % 2 == 1 and y % 2 == 0){
93+
cout << 2 << " " << min(x ^ 2, y ^ (x ^ 2)) << " " << max(x ^ 2, y ^ (x ^ 2)) << endl;
94+
return;
95+
}
96+
//A is odd and B is odd
97+
cout << 2 << " " << min(x ^ 2, y ^ 2) << " " << max(x ^ 2, y ^ 2) << endl;
98+
}
99+
```
100+
101+
### G
102+
蒙的,不太理解为什么可以排序然后分段取最大值。
103+
我一开始觉得是个NP-hard问题,但是数据规模太大了,于是卡了很长时间
104+
```c++
105+
void solve(){
106+
int n;
107+
cin >> n;
108+
vector <int> a(n);
109+
//if we use mask DP, it will TLE
110+
ll tot = 0;
111+
for (int i = 0; i < n; i++) {
112+
cin >> a[i];
113+
tot += 1000 - a[i];
114+
}
115+
ll b = 0, c = 0;
116+
sort(all(a));
117+
ll ans = 0;
118+
for (int i = 0; i < n; i++) {
119+
b += a[i];
120+
tot -= 1000 - a[i];
121+
chkmax(ans, tot * b);
122+
}
123+
b = 0, c = 0;
124+
tot = accumulate(all(a), 0LL);
125+
for (int i = 0; i < n; i++) {
126+
b += 1000 - a[i];
127+
tot -= a[i];
128+
chkmax(ans, tot * b);
129+
}
130+
cout << ans << endl;
131+
}
132+
```
133+
134+
135+
### F
136+
这种序列操作为了避免后效性问题,一般都是从后面开始操作
137+
```c++
138+
void solve(){
139+
int n;
140+
cin >> n;
141+
string s;
142+
cin >> s;
143+
vector <int> id;
144+
auto chk = [&](char c)->bool{
145+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
146+
};
147+
bool rev = 1;
148+
string ans(n, ' ');
149+
int l = 0, r = n - 1;
150+
for (int i = n - 1; i >= 0; i--) {
151+
if(rev) ans[r--] = s[i];
152+
else ans[l++] = s[i];
153+
if(chk(s[i])){
154+
rev ^= 1;
155+
}
156+
}
157+
cout << ans << endl;
158+
}
159+
```

0 commit comments

Comments
 (0)