2026附中交互专题
__JCY__
·
·
个人记录
题单
CF727C Guess the Array
::::info[code]
```cpp
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1e5 + 5;
int a[kMaxN], ans[kMaxN], n, sum;
signed main() {
cin >> n;
if (n & 1) {
for (int i = 1; i < n; i++) {
cout << "? " << i << ' ' << i + 1 << endl;
cin >> a[i];
sum += a[i];
}
cout << "? " << 1 << ' ' << n << endl;
cin >> a[n];
sum += a[n], sum /= 2;
for (int i = 1; i < n; i += 2) sum -= a[i];
ans[n] = sum;
for (int i = n - 1; i >= 1; i--) ans[i] = a[i] - ans[i + 1];
cout << "! ";
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
} else {
cout << "? 1 2" << endl;
cin >> a[1];
cout << "? 2 3" << endl;
cin >> a[2];
cout << "? 3 1" << endl;
cin >> a[3];
int sum = (a[1] + a[2] + a[3]) / 2;
ans[1] = sum - a[2], ans[2] = a[1] - ans[1], ans[3] = a[3] - ans[1];
for (int i = 4; i <= n; i++) {
cout << "? " << i << ' ' << 1 << endl;
int x; cin >> x; ans[i] = x - ans[1];
}
cout << "! ";
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
}
return 0;
}
```
::::
# [CF809B Glad to see you!](https://www.luogu.com.cn/problem/CF809B)
二分出任意一个,再二分左右。若左边没有则右边一定有。
::::info[code]
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> Ans;
int C(int x, int y) {
cout << 1 << ' ' << x << ' ' << y << endl;
string s; cin >> s;
return s == "NIE";
}
signed main() {
cin >> n >> k;
int l = 1, r = n;
for (int mid; l < r; ) {
mid = l + r >> 1;
if (C(mid, mid + 1)) l = mid + 1;
else r = mid;
}
Ans.push_back(l);
l = 1, r = Ans[0] - 1;
for (int mid; l < r; ) {
mid = l + r >> 1;
if (C(mid, mid + 1)) l = mid + 1;
else r = mid;
}
if (l != Ans[0] && C(l, l + 1) == 0) {
cout << 2 << ' ' << Ans[0] << ' ' << l << endl;
return 0;
}
l = Ans[0] + 1, r = n;
for (int mid; l < r; ) {
mid = l + r >> 1;
if (C(mid, mid + 1)) l = mid + 1;
else r = mid;
}
cout << 2 << ' ' << Ans[0] << ' ' << l << '\n';
return 0;
}
```
::::
# [CF835E The penguin's game](https://www.luogu.com.cn/problem/CF835E)
按照位置的二进制分类,一定有一种方案使得 $x$, $y$ 分来,通过过查询可以查询出包含的特殊位置个数的奇偶性。计算另一个答案是,可以统计哪些位 $x$, $y$ 不同,来直接计算。
::::info[code]
```cpp
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1005, kMaxM = 15;
int n, X, Y, Ans[2];
bool vis[kMaxM], flag;
int C(vector<int> G) {
cout << "? ";
cout << G.size() << ' ';
for (auto v : G) cout << v << ' ';
cout << endl;
int x; cin >> x;
if (G.size() & 1) return x == Y;
else return x == (X ^ Y);
}
vector<int> M(vector<int> G, int l, int r) {
vector<int> P;
for (int i = l; i <= r; i++) P.push_back(G[i]);
return P;
}
signed main() {
cin >> n >> X >> Y;
for (int j = 0; j <= 10; j++) {
vector<int> G;
for (int i = 1; i <= n; i++) {
if (i & (1 << j)) {
G.push_back(i);
}
}
if (G.size() && C(G)) {
vis[j] = 1;
if (flag) continue;
flag = 1;
int l = 0, r = G.size() - 1;
for (int mid; l < r; ) {
mid = l + r >> 1;
if (C(M(G, l, mid))) r = mid;
else l = mid + 1;
}
Ans[0] = G[l];
}
}
Ans[1] = Ans[0];
for (int i = 0; i <= 10; i++) if (vis[i]) Ans[1] ^= (1 << i);
if (Ans[0] > Ans[1]) swap(Ans[0], Ans[1]);
cout << "! " << Ans[0] << ' ' << Ans[1] << endl;
return 0;
}
```
::::
# [CF811D Vladik and Favorite Game](https://www.luogu.com.cn/problem/CF811D)
先判断是不是反转,可以通过在绝对安全的地方左右/上下移动,再搜索位置。
::::info[code]
```cpp
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 105;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dd[] = {'R', 'L', 'D', 'U'};
int n, m, x, y, X, Y, flag;
char c[kMaxN][kMaxN], t[kMaxN * kMaxN];
bool vis[kMaxN][kMaxN];
int f1 = -1, f2 = -1;
vector<pair<int, int>> G;
void dfs(int x, int y, int X, int Y, int &flag) {
if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || c[x][y] == '*') {
return;
}
vis[x][y] = 1;
if (x == X && y == Y) {
G.push_back({x, y});
return flag = 1, void();
}
for (int i = 0; i <= 3; i++) {
int xx = x + dx[i], yy = y + dy[i];
dfs(xx, yy, X, Y, flag);
if (flag == 1) return G.push_back({x, y}), void();
}
}
void T(char c) {
cout << c << endl;
cin >> x >> y;
if (x == X && y == Y) exit(0);
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
if (c[i][j] == 'F') X = i, Y = j;
}
}
if (c[1][2] == '.') {
T('R');
if (x == 1 && y == 1) f1 = 1;
else f1 = 0, T('L');
}
if (c[2][1] == '.') {
T('D');
if (x == 1 && y == 1) f2 = 1;
else f2 = 0, T('U');
}
if (f1 == -1) {
int E = 0;
for (int i = 1; i <= n; i++) if (c[i][2] == '.' || c[i][2] == 'F') {
E = i; break;
}
for (; x != E; T(f2 == 1 ? 'U' : 'D'));
T('R');
if (y == 1) f1 = 1;
else f1 = 0;
}
if (f2 == -1) {
int E = 0;
for (int i = 1; i <= m; i++) if (c[2][i] == '.' || c[2][i] == 'F') {
E = i; break;
}
for (; y != E; T(f1 == 1 ? 'L' : 'R'));
T('D');
if (x == 1) f2 = 1;
else f2 = 0;
}
dfs(x, y, X, Y, flag);
// for (auto v : G) cout << v.first << ' ' << v.second << '\n';
for (int i = G.size() - 1; i >= 1; i--) {
char p = 0;
for (int j = 0; j <= 3; j++) {
int x1 = G[i].first, y1 = G[i].second;
int x2 = G[i - 1].first, y2 = G[i - 1].second;
if (x1 + dx[j] == x2 && y1 + dy[j] == y2) {
p = dd[j];
}
}
if (p == 'L' && f1 == 1) p = 'R';
else if (p == 'R' && f1 == 1) p = 'L';
if (p == 'U' && f2 == 1) p = 'D';
else if (p == 'D' && f2 == 1) p = 'U';
T(p);
}
return 0;
}
```
::::
# [Madhouse (Easy version)](https://www.luogu.com.cn/problem/CF1286C1)
查询 $\left [ 1,n \right ] $ 和 $\left [ 1, n-1 \right ] $,通过去重可以直接计算。
::::info[code]
```cpp
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1e4 + 5;
string s[kMaxN];
int n;
map<string, int> mp;
vector<string> G;
string M(string x) {
vector<int> a(26);
string y = "";
for (int i = 0; i < x.size(); i++) a[x[i] - 'a']++;
for (int i = 0; i < 26; i++) for (int j = 1; j <= a[i]; j++) y += (char)(i + 'a');
return y;
}
vector<int> MM(string x) {
vector<int> a(26);
string y = "";
for (int i = 0; i < x.size(); i++) a[x[i] - 'a']++;
for (int i = 0; i < 26; i++) for (int j = 1; j <= a[i]; j++) y += (char)(i + 'a');
return a;
}
void C(int l, int r, int d) {
cout << "? " << l << ' ' << r << endl;
int len = r - l + 1;
for (int i = 1; i <= (len + 1) * len / 2; i++) {
string s; cin >> s; s = M(s);
mp[s] += d; if (mp[s] == 0) mp.erase(s);
}
}
bool cmp(string A, string B) {
return A.size() < B.size();
}
signed main() {
cin >> n;
if (n == 1) {
C(1, 1, 1);
cout << "! " << (*mp.begin()).first << endl;
return 0;
}
C(1, n, 1);
C(2, n, -1);
vector<int> pr(26);
cout << "! ";
for (auto v : mp) G.push_back(v.first);
sort(G.begin(), G.end(), cmp);
for (auto v : G) {
vector<int> now = MM(v);
for (int i = 0; i < 26; i++) if (now[i] - pr[i] == 1) cout << char(i + 'a');
pr = now;
}
return 0;
}
```
::::
# [CF2001C Guess The Tree](https://www.luogu.com.cn/problem/CF2001C)
当一次询问的结果是两个点中间的一个,说明两个点相邻。一次将得到的中心加入下一次查询。
::::info[code]
```cpp
#include <bits/stdc++.h>
using namespace std;
int T, n;
vector<pair<int, int>> G;
signed main() {
for (cin >> T; T; T--) {
cin >> n, G.clear();
for (int i = 2; i <= n; i++) {
int t;
cout << "? " << 1 << ' ' << i << endl;
cin >> t;
int x = 1, ret;
for (; 1; x = ret) {
cout << "? " << i << ' ' << x << endl;
cin >> ret;
if (ret == x || ret == i) break;
}
G.push_back({x, i});
}
cout << "! ";
for (auto v : G) cout << v.first << ' ' << v.second << ' ';
cout << '\n';
}
return 0;
}
```
::::