Codeforces Round 897 (Div. 2) 考试总结
前言#
这次打得很好,相较于 div3 的脑残题和签到题来说,div2 的思维难度更加的大。同时还有除传统题外,其他的题型出现。比如交互题等。这次能在考场上想出三道较于之前是有很大的进步的。
赛时实况:
A | B | C | D | E1 | E2 | F |
---|---|---|---|---|---|---|
√ | √ | √ | × | × | × | × |
赛后改题情况:
A | B | C | D | E1 | E2 | F |
---|---|---|---|---|---|---|
√ | √ | √ | × | √ | √ | × |
感觉 D、F 不是很会。
A. green_gold_dog, array and permutation#
Problem#
给定一个长度为
Solve#
首先,
现在要使
可以用反证法证明:假如
时间复杂度
Code#
#include <bits/stdc++.h>
#define int long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
namespace Read {
template <typename T>
inline void read(T &x) {
x=0;T f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x*=f;
}
template <typename T, typename... Args>
inline void read(T &t, Args&... args) {
read(t), read(args...);
}
}
using namespace Read;
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
const int N = 4e4 + 10;
int T, n, a[N], p[N], Ans[N];
bool cmp(int x, int y) {
return a[x] > a[y];
}
void solve() {
read(n);
For(i,1,n) read(a[i]), p[i] = i;
sort(p + 1, p + n + 1, cmp);
For(i,1,n) Ans[p[i]] = i;
For(i,1,n) cout << Ans[i] << ' ';
cout << '\n';
}
signed main() {
read(T);
while(T--) {
solve();
}
return 0;
}
B. XOR Palindromes#
Problem#
给定一个
现有长度为
Solve#
首先,肯定是从小到大找答案。考虑最小的,合法(“好的”)的数
做法是从串的中点向左右扩展。若两数不同,则耗费一步数更改其中的一位。具体是哪一位不需要管。因为我们只需要将其变为回文串,不需要关心其形态。
然后知道最小的
证明很简单,当
最后模拟即可。
时间复杂度
Code#
#include <bits/stdc++.h>
#define int long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
namespace Read {
template <typename T>
inline void read(T &x) {
x=0;T f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x*=f;
}
template <typename T, typename... Args>
inline void read(T &t, Args&... args) {
read(t), read(args...);
}
}
using namespace Read;
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
const int N = 1e5 + 10;
int T, n;
char a[N];
void solve() {
read(n);
For(i,1,n) cin >> a[i];
if(n & 1) {
int l = (1 + n) >> 1, r = l, ans1 = 0;
while(l >= 1) {
if(a[l] != a[r]) ans1++;
l--, r++;
}
For(i,0,n) {
if(i >= ans1 && i <= n - ans1) cout << 1;
else cout << 0;
}
cout << '\n';
} else {
int l = n >> 1, r = l + 1, ans1 = 0;
while(l >= 1) {
if(a[l] != a[r]) ans1++;
l--, r++;
}
For(i,0,n) {
if(i >= ans1 && i <= n - ans1 && (i - ans1) % 2 == 0) cout << 1;
else cout << 0;
}
cout << '\n';
}
}
signed main() {
read(T);
while(T--) {
solve();
}
return 0;
}
C. Salyg1n and the MEX Game#
Problem#
交互题。给定一个
游戏最后的答案为 MEX,你要最大化这个结果,交互机要最小化这个结果。设
Solve#
脑瘫交互。
注意到每次交互机会选择一个
你先选择并插入的的数肯定是数列的 MAX,因为你要最大化 MEX。然后交互机删什么,你就填什么即可。
时间复杂度
Code#
#include <bits/stdc++.h>
#define int long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
namespace Read {
template <typename T>
inline void read(T &x) {
x=0;T f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x*=f;
}
template <typename T, typename... Args>
inline void read(T &t, Args&... args) {
read(t), read(args...);
}
}
using namespace Read;
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
const int N = 1e5 + 10;
int T, n, y, a[N];
void solve() {
read(n);
For(i,1,n) cin >> a[i];
sort(a + 1, a + n + 1);
int MEX = 0;
For(i,1,n) {
if(a[i] == MEX) MEX++;
}
cout << MEX << '\n';
fflush(stdout);
while(cin >> y) {
if(y == -1) return ;
cout << y << '\n';
fflush(stdout);
}
}
signed main() {
read(T);
while(T--) {
solve();
}
return 0;
}
这可能是我人生第一次自己做的交互题了。
D. Cyclic Operations#
Problem#
(咕咕咕...)
Solve#
(咕咕咕...)
Code#
(咕咕咕...)
E1. Salyg1n and Array (simple version)#
Problem#
交互题。交互库有一个长度为
Solve#
当
当
证明:原数组记为
区间覆盖询问时,操作数为
时间复杂度
Code#
#include <bits/stdc++.h>
#define ll long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
namespace Read {
template <typename T>
inline void read(T &x) {
x=0;T f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x*=f;
}
template <typename T, typename... Args>
inline void read(T &t, Args&... args) {
read(t), read(args...);
}
}
using namespace Read;
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
int n, T, k, res;
int ask(int i) {
cout << "? " << i << '\n';
fflush(stdout);
int x; cin >> x;
return x;
}
void solve() {
res = 0;
cin >> n >> k;
int i;
for (i = 1; i + k - 1 <= n; i += k) {
res ^= ask(i);
}
if(n % k == 0) {
cout << "! " << res << '\n';
fflush(stdout);
} else {
res ^= ask(i - k + (n % k) / 2);
res ^= ask(n - k + 1);
cout << "! " << res << '\n';
fflush(stdout);
}
return ;
}
signed main() {
cin >> T;
while(T--) {
solve();
}
return 0;
}
E2. Salyg1n and Array (hard version)#
Problem#
交互题。交互库有一个长度为
Solve#
我们发现 E1 的程序不高效率的原因在于它有很多的冗余运算。比如,最后的剩余段会被询问
我们考虑将
这样最多只需要询问
Code#
#include <bits/stdc++.h>
#define ll long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
namespace Read {
template <typename T>
inline void read(T &x) {
x=0;T f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x*=f;
}
template <typename T, typename... Args>
inline void read(T &t, Args&... args) {
read(t), read(args...);
}
}
using namespace Read;
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
int n, T, k, res;
int ask(int i) {
cout << "? " << i << '\n';
fflush(stdout);
int x; cin >> x;
return x;
}
void solve() {
res = 0;
cin >> n >> k;
int i;
for (i = 1; i + k - 1 <= n; i += k) {
res ^= ask(i);
}
if(n % k == 0) {
cout << "! " << res << '\n';
fflush(stdout);
} else {
res ^= ask(i - k + (n % k) / 2);
res ^= ask(n - k + 1);
cout << "! " << res << '\n';
fflush(stdout);
}
return ;
}
signed main() {
cin >> T;
while(T--) {
solve();
}
return 0;
}
F. Most Different Tree#
Problem#
(咕咕咕...)
Solve#
(咕咕咕...)
Code#
(咕咕咕...)
作者:Daniel-yao
出处:https://www.cnblogs.com/Daniel-yao/p/17702654.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效