Bronze
A
从后往前扫一遍即可。
char rev(char c) {
if(c == 'M') return 'O';
if(c == 'O') return 'M';
}
void solve() {
int n = rd();
string s; cin >> s; s = " " + s;
puts("YES");
if(!K) return;
string ans;
for(int i = n, cnt = 0; i; i--) {
if(cnt & 1) ans += rev(s[i]);
else ans += s[i];
if(ans[ans.size() - 1] == 'O') cnt++;
}
reverse(ans.begin(), ans.end());
cout << ans << '\n';
}B
看到 考虑枚举。
首先想到一个 的暴力,然后考虑优化。我在这里卡了 1h+。
发现三元组的个数最多只有 个,但是还是会超时。
但是如果先枚举三元组,计算这些三元组在那些状态中有贡献,就可以少枚举三个位置,复杂度 ,可以通过。
int n = rd(), m = rd(), N = (1 << n) - 1, tot = 0, ans = 0;
for(int i = 1; i <= m; i++) {
a[i] = rd(), b[i] = rd(), c[i] = rd();
if(b[i] > c[i]) swap(b[i], c[i]);
cx[a[i]][b[i]][c[i]]++;
}
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++)
for(int k = 1; k <= n; k++) if(cx[i][j][k]) {
int t = N ^ (1 << (i - 1)) ^ (1 << (j - 1)) ^ (1 << (k - 1));
for(int s = t; ; s = t & (s - 1)) {
f[s | (1 << (i - 1))] += cx[i][j][k];
if(s == 0) break;
}
}
for(int s = 1; s <= N; s++) {
if(f[s] > ans) ans = f[s], tot = 0;
if(f[s] == ans) tot++;
}
cout << ans << ' ' << tot << '\n';C
首先可以让 。
从低位往高位考虑。
- 如果已经没有可以花费的,需要将最后的全买;
- 如果这位是 ,需要买;
- 若当前位多买一个比买以前的便宜,就多买一个。
ll n = rd() - 1, Q = rd();
for(ll i = 0; i <= n; i++) {
a[i] = rd();
if(i) a[i] = min(a[i], a[i - 1] * 2);
}
while(Q--) {
ll x = rd(), ans = 0;
for(ll i = 0; i <= min(40ll, n); i++) {
ans = min(ans, a[i]);
if(i == n && x) ans += x * a[i];
else if(x & 1) ans += a[i];
x >>= 1;
}
cout << ans << '\n';
}Silver
A
不难发现若相邻两位相同,相互的回答是 JJ;若相邻两位不同,答案是 NN。将原序列重排后,要满足现:
- 右上和左下的字母是相同的;
NJ和JN是配对的,所以数量一定相同;- 若同时存在
JJ和NN,则NJ和NJ一定存在; NN的数量是偶数。每NN一个会使相邻两个位置不同,由于这是一个环,最后要变回原来的,所以数量是偶数。
一种合法的方案为:
JJJJJNNNNNJNJNJN
JJJJNNNNNJNJNJNJ然后又这个序列推出原序列即可。
int n = rd();
string s, t; cin >> s >> t; s = " " + s, t = " " + t;
for(int i = 0; i < 4; i++) cx[i].clear();
ans.clear();
for(int i = 1; i <= n; i++) {
if(s[i] == 'J' && t[i] == 'J') cx[0].push_back(i);
if(s[i] == 'N' && t[i] == 'N') cx[1].push_back(i);
if(s[i] == 'J' && t[i] == 'N') cx[2].push_back(i);
if(s[i] == 'N' && t[i] == 'J') cx[3].push_back(i);
}
int len = cx[2].size();
if(cx[2].size() != cx[3].size()) return 1;
if(cx[0].size() && cx[1].size() && !cx[2].size()) return 1;
string res;
for(int v : cx[0]) ans.push_back(v), res += 'J';
if(len) ans.push_back(cx[2][0]), res += 'J';
int now = 1;
for(int v : cx[1]) ans.push_back(v), now ^= 1, res += (now ? 'J' : 'N');
if(len) ans.push_back(cx[3][0]), now ^= 1, res += (now ? 'J' : 'N');
for(int i = 1; i < len; i++) {
ans.push_back(cx[2][i]);
res += (now ? 'J' : 'N');
now ^= 1;
ans.push_back(cx[3][i]);
res += (now ? 'J' : 'N');
}
if(res[0] == res[n - 1] && res[0] == 'N') return 1;
if(res[0] != res[n - 1] && res[0] == 'J') return 1;
puts("YES");
if(!K) return 0;
for(int i = 0; i < n; i++) cout << ans[i] << " \n"[i == n - 1];
cout << res << '\n';
return 0;B
按题意模拟即可,不难发现每个点最多被删一次,所以复杂度是正确的。
ll n = rd(), c = rd(), ans = 0;
for(ll i = 1; i <= c; i++) a[i] = rd();
for(ll i = 1; i <= n; i++) p[i] = rd();
for(ll i = 1; i <= n; i++)
for(ll Q = rd(), x; Q--; ) x = rd(), cx[x].push_back(i);
for(ll i = 1; i <= c; i++) {
while(now[i] < cx[i].size() && a[i]) {
ll x = cx[i][now[i]];
if(!f[x]) f[x] = i, a[i]--, ans += x;
now[i]++;
}
}
cout << ans << '\n';
for(ll i = 1; i < n; i++) {
ll t = p[i], id = f[t];
if(f[t]) ans -= t;
vis[t] = 1;
while(now[id] < cx[id].size()) {
ll x = cx[id][now[id]];
now[id]++;
if(vis[x]) continue;
if(!f[x]) { f[x] = id, ans += x; break; }
if(f[x] > id) swap(f[x], id);
}
cout << ans << '\n';
}C
先复制 遍,防止边界问题。
其实只有 种情况,往左、往右、先左后右、先右后左。
先求出每个点 往左往右,最短要多少,设为 。
点 往右,在 处折返的步数为 ,放进单调队列里求即可。
int n = rd(), cnt = 0;
for(int i = 1; i <= n; i++) {
a[i] = a[i + n] = a[i + n + n] = rd();
if(!c[a[i]]) cnt++;
c[a[i]]++;
}
int tot = cnt;
for(int i = 1; i <= n; i++) c[a[i]] = 0;
for(int i = 1, j = 0; i <= n * 2; i++) {
while(cnt) {
if(!c[a[++j]]) cnt--;
c[a[j]]++;
}
f[i] = j;
c[a[i]]--;
if(c[a[i]] == 0) cnt++;
}
cnt = tot;
for(int i = 1; i <= n; i++) c[a[i]] = 0;
for(int i = n * 2, j = n * 2 + 1; i > n; i--) {
while(cnt) {
if(!c[a[--j]]) cnt--;
c[a[j]]++;
}
g[i - n] = j;
c[a[i]]--;
if(c[a[i]] == 0) cnt++;
}
f[0] = 1e9;
for(int i = 1, l = 1, r = 0; i <= n * 2; i++) {
while(l <= r && f[q[l]] < i) l++;
h[(i - 1) % n + 1] = 2 * f[q[l]] - q[l] - i;
while(l <= r && 2 * f[q[r]] - q[r] > 2 * f[i] - i) r--;
q[++r] = i;
}
q[1] = 0;
for(int i = 1, l = 1, r = 0; i <= n * 2; i++) {
while(l <= r && f[q[l]] < i) l++;
p[(i - 1) % n + 1] = f[q[l]] - 2 * q[l] + i;
while(l <= r && f[q[r]] - 2 * q[r] > f[i] - 2 * i) r--;
q[++r] = i;
}
for(int i = 1; i <= n; i++)
cout << min(min(f[i] - i, n + i - g[i]), min(h[i], p[i])) << " \n"[i == n];