CF573E Bear and Bowling *3200(fake)
从长度为 的数组 中挑选子序列 ,使得 最大。
,。
s MB
CF1624F Interacdive Problem *2000
给定 ,存在 。
你可以使用的操作:+ c,交互库会使 并在此之后返回 。
求 最后的值。
, 次操作。
s MB
CF128D Numbers *2000
给定长度为 的序列 。现在问是否能将序列 排成一个环, 使得环上任意相邻两数之差都是 。
,。
s MB
CF843B Interactive LowerBound *2000
2x bonus: CF844D
给定单调递增链表的长度 ,链表的开头 和 。
你每次可以询问一个位置,交互库会返回这个位置的 和 指针。
求链表中最小的不小于 的数字。
, 次询问。
s MB
CF1918E ace5 and Task Order *2200
交互库生成一个长度为 的排列和一个数字 。
你每次可以向交互库询问一个位置 :
- 如果 ,返回
>,此后 增加 。 - 如果 ,返回
<,此后 减少 。 - 如果 ,返回
=, 不变。
你需要求出整个排列。
,你可以询问 次。
s MB
CF1848F Vika and Wiki *2400
给定长度为 的环 ,每次 将变为 。求需要多少次操作使 归零。
,。
s MB
CF1773I Interactive Factorial Guessing *2500
交互库隐藏了一个数字 ,你每次可以询问 的第 位(从 开始编号)是多少,请求出 。
, 次询问。
s MB
CF1354G Find a Gift *2600
个盒子排成一排,有 个盒子里面是礼物,剩下的盒子里面是石头,石头都一样重且严格重于所有礼物,礼物重量不一定相等。你每次可以选择两个子序列去称重,会返回哪个更重或一样重。求最左边的礼物的位置。
,, 次询问。
s MB
CF2022D2 Asesino (Hard Version) *2700
每局游戏有若干骑士、骗子但只有一个冒名顶替者。
骑士是好人阵营,骗子和冒名顶替者是坏人阵营。
如果询问一个人和自己相同阵营的人会回答 Yes,否则会回答 No。
冒名顶替者很调皮,会回答相反的答案。
| \ | 骑士 | 骗子 | 冒名顶替者 |
|---|---|---|---|
| 骑士 | Yes | No | Yes |
| 骗子 | No | Yes | No |
| 冒名顶替者 | No | Yes | — |
请你使用最少的询问次数来确定谁是冒名顶替者。
交互库自适应!
。
s MB
CF1838F Stuck Conveyor *3000
一个 的正方形区域,有 个传送带方块,你每次可以指定每个方块的方向,并且在其中任意一个位置投放一个物品,物品会沿方块所指的方向运动,你最后会得知方块出来的位置 或方块进入了死循环 。但是有一个方块不会受到你的控制,而是会固定朝向一个方向,请你在 次分配内找到这个方块以及它的朝向。
CF573E Bear and Bowling *3200(fake)
从长度为 的数组 中挑选子序列 ,使得 最大。
,。
s MB
题解
考虑 dp。
设 表示到第 位截止,选择 个位置的最大值情况。
发现数组开不下,考虑状压,因为每次转移都是 转移到 ,所以省略第一维。记得倒序更新(原理与 01 背包相同)。
虽然 ,但是 s,足够 通过。
代码
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int n;
long long a[100005] , dp[100005] , ans;
int main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++)
{
cin >> a[i];
}
for(int i = 1 ; i <= n ; i++)
{
dp[i] = -1000000000000000018ll;
}
for(int i = 1 ; i <= n ; i++)
{
for(int j = i ; j >= 1 ; j--)
{
dp[j] = max(dp[j] , dp[j - 1] + j * a[i]);
}
}
for(int i = 1 ; i <= n ; i++)
{
ans = max(ans , dp[i]);
}
cout << ans;
return 0;
}CF1624F Interacdive Problem *2000
给定 ,存在 。
你可以使用的操作:+ c,交互库会使 并在此之后返回 。
求 最后的值。
, 次操作。
s MB
题解
假设没有增加操作,则可直接二分。
否则记录已经增加的值 ,二分 的大小能够使 。
最终输出 即可。
代码
#include<bits/stdc++.h>
using namespace std;
int n , k , kk , l , r , mid , ans , sum , tmp;
int main()
{
cin >> n;
l = 1;
r = n - 1;
while(l <= r)
{
mid = (l + r) / 2;
k = (mid + sum) / n;
tmp = (k + 1) * n - mid - sum;
cout << "+ " << tmp << endl;
cin >> kk;
sum += tmp;
if(kk > k)
{
ans = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
cout << "! " << ans + sum << endl;
return 0;
}CF128D Numbers *2000
给定长度为 的序列 。现在问是否能将序列 排成一个环, 使得环上任意相邻两数之差都是 。
,。
s MB
题解
首先,我们可以减去一些明显不可能的情况,也就是排序后任意两个相邻的数差 的情况。
接下来,我们可以缩小数据范围,也就是将所有值都减去最小值。
以上两步可得:。证明也很简单,这里可以感性理解一下。若最大的 ,且经第二步操作,最小的 总等于 。此时,若想保证排序后的任意两个相邻的数的差 ,则必须有至少 个元素,所以可以证明。
其实,前面所有的转化都是为了能够将所有的数装进桶里面,方便后续运算。
这时就可以推出一个类似转移的贪心。将现在的位置记为 后,初始时 ,则整个转移可表示为:
- 若 位置仍有数字,则 。
- 若 位置的桶为空时,可以分以下情况:
- 若 位置仍有数字,则 。
- 若 位置的桶为空且 时:跳出转移。
- 若 位置的桶为空且 时:说明没有可行的数列。
最后,检查桶里面还有没有剩余的元素,如果有,则代表没有可行的数列,可以理解为在以上所有构造中构造的可行数列丢下了这几个元素。
如果一个数列闯过了以上所有判断,那么就证明这个数列是可行的,输出 YES 结束程序。
代码
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int n , a[100005] , b[100005] , cnt;
int main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++)
{
cin >> a[i];
}
sort(a + 1 , a + n + 1);
for(int i = 2 ; i <= n ; i++)
{
if(a[i] - 1 > a[i - 1])
{
cout << "NO";
return 0;
}
}
for(int i = n ; i >= 1 ; i--)
{
a[i] -= a[1];
b[a[i]]++;
}
while(1)
{
b[cnt]--;
if(b[cnt + 1])
{
cnt++;
}
else if(cnt == 1 && !b[0])
{
break;
}
else if(b[cnt - 1])
{
cnt--;
}
else
{
cout << "NO";
return 0;
}
}
for(int i = 1 ; i <= 100000 ; i++)
{
if(b[i])
{
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}CF843B Interactive LowerBound *2000
2x bonus: CF844D
给定单调递增链表的长度 ,链表的开头 和 。
你每次可以询问一个位置,交互库会返回这个位置的 和 指针。
求链表中最小的不小于 的数字。
, 次询问。
s MB
题解
显然,我们如果询问到了一个比目标大的数字和一个比目标小的数字,则目标一定在两个元素之间。
我们尝试去随机找点,缩小不必要的枚举范围,若找了 个点,则总点数的消耗是 。画出函数图像:
将找点数定位 ,此时期望 次询问解决问题。小于题目要求的 次,只要不是运气特别差即可通过。
代码
#include<bits/stdc++.h>
using namespace std;
struct p
{
int val , nxt;
}a[230] , ans;
int n , s , x , ask;
bool vis[50005];
void solve()
{
int tmp[2005] , v , nxt;
for(int i = 1 ; i <= n ; i++)
{
cout << "? " << i << endl;
cin >> v >> nxt;
tmp[i] = v;
}
sort(tmp + 1 , tmp + n + 1);
for(int i = 1 ; i <= n ; i++)
{
if(tmp[i] >= x)
{
cout << "! " << tmp[i];
return;
}
}
cout << "! -1";
return;
}
bool cmp(p l , p r)
{
return l.val < r.val;
}
int main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
srand(time(0));
cin >> n >> s >> x;
if(n < 2000)
{
solve();
return 0;
}
vis[0] = 1;
for(int i = 1 ; i <= 224 ; i++)
{
while(vis[ask])
{
ask = rand() * rand() % n + 1;
}
vis[ask] = 1;
cout << "? " << ask << endl;
cin >> a[i].val >> a[i].nxt;
if(a[i].val == x)
{
cout << "! " << x;
return 0;
}
}
sort(a + 1 , a + 225 , cmp);
if(a[1].val > x)
{
ans.nxt = s;
while(ans.val < a[1].val)
{
cout << "? " << ans.nxt << endl;
cin >> ans.val >> ans.nxt;
if(ans.val >= x)
{
cout << "! " << ans.val;
return 0;
}
}
cout << "! -1";
return 0;
}
for(int i = 1 ; i <= 224 ; i++)
{
if(a[i].val >= x)
{
ans.nxt = a[i - 1].nxt;
break;
}
else if(i == 224)
{
ans.nxt = a[224].nxt;
}
}
while(1)
{
cout << "? " << ans.nxt << endl;
cin >> ans.val >> ans.nxt;
if(ans.val >= x)
{
cout << "! " << ans.val;
return 0;
}
if(ans.nxt == -1)
{
cout << "! -1";
return 0;
}
}
return 0;
}CF1918E ace5 and Task Order *2200
交互库生成一个长度为 的排列和一个数字 。
你每次可以向交互库询问一个位置 :
- 如果 ,返回
>,此后 增加 。 - 如果 ,返回
<,此后 减少 。 - 如果 ,返回
=, 不变。
你需要求出整个排列。
,你可以询问 次。
s MB
题解
要求找到整个排列所有位置的值,考虑排序。
则我们可以模拟快排,首先随机出一个中心点,对中心点持续询问直到返回 =。
然后就可以将这个点和整个序列内部的所有点比较,比较后还要重新询问一次这个点来消除增减造成的影响。
若有 个数比它小,则它的值就是 。
其余点分治处理即可(断句不是“其余/点分治/处理”不要读错了)。
随机出中心点并使 对齐的期望次数是每次对齐 下 个位置。
判断大小的次数期望是 次,具体参照快排复杂度分析。每次判断后要复原所以要乘以 。
总期望次数为 ,大约 ,小于题目要求的 。卡的比较紧,如果实在是运气太差一遍没过可以多交几遍。
代码
#include<bits/stdc++.h>
using namespace std;
int t , n , ans[2005];
vector<int> p;
char cc;
inline void qsort(int l , vector<int> a)
{
int x = rand() % a.size();
cout << "? " << a[x] << endl;
cin >> cc;
while(cc != '=')
{
cout << "? " << a[x] << endl;
cin >> cc;
}
vector<int> b , c;
b.clear();
c.clear();
for(int i = 0 ; i < a.size() ; i++)
{
if(i == x)
{
continue;
}
cout << "? " << a[i] << endl;
cin >> cc;
if(cc == '<')
{
b.push_back(a[i]);
}
else
{
c.push_back(a[i]);
}
cout << "? " << a[x] << endl;
cin >> cc;
}
ans[a[x]] = l + b.size();
if(b.size())
{
qsort(l , b);
}
if(c.size())
{
qsort(l + b.size() + 1 , c);
}
return;
}
int main()
{
auto now = std::chrono::system_clock::now();
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto t = ms.time_since_epoch().count();
srand(t);
cin >> t;
while(t--)
{
cin >> n;
p.clear();
for(int i = 1 ; i <= n ; i++)
{
p.push_back(i);
}
qsort(1 , p);
cout << "! ";
for(int i = 1 ; i <= n ; i++)
{
cout << ans[i] << ' ';
}
cout << endl;
}
return 0;
}CF1848F Vika and Wiki *2400
给定长度为 的环 ,每次 将变为 。求需要多少次操作使 归零。
,。
s MB
题解
- 操作 :
- 操作 :
- 操作 :
- 操作 :
- 操作 :
因此,不存在无解( 时一定清空)。
从大到小对于每个二进制位上的值,都更新处理即可。如果会变成全 ,则说明答案大了,否则说明还能继续更新。这里直接枚举即可。
复杂度 。
代码
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int n , k , a[2000005] , b[2000005] , ans;
int makek()
{
int cnt = 0 , tmp = n;
while(tmp > 1)
{
tmp /= 2;
cnt++;
}
return cnt;
}
bool all0()
{
for(int i = 1 ; i <= n ; i++)
{
if(b[i])
{
return 0;
}
}
return 1;
}
int main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++)
{
cin >> a[i];
}
k = makek();
for(int i = k - 1 ; i >= 0 ; i--)
{
for(int j = 1 ; j <= n ; j++)
{
b[j] = a[j] ^ a[(j + (1 << i) - 1) % n + 1];
}
if(all0())
{
continue;
}
ans += (1 << i);
for(int j = 1 ; j <= n ; j++)
{
a[j] = b[j];
}
}
for(int i = 1 ; i <= n ; i++)
{
b[i] = a[i];
}
if(all0())
{
cout << ans;
}
else
{
cout << ans + 1;
}
return 0;
}CF1773I Interactive Factorial Guessing *2500
交互库隐藏了一个数字 ,你每次可以询问 的第 位(从 开始编号)是多少,请求出 。
, 次询问。
s MB
CF1354G Find a Gift *2600
个盒子排成一排,有 个盒子里面是礼物,剩下的盒子里面是石头,石头都一样重且严格重于所有礼物,礼物重量不一定相等。你每次可以选择两个子序列去称重,会返回哪个更重或一样重。求最左边的礼物的位置。
,, 次询问。
s MB
题解
我们先假设第一个箱子是石头,尝试找到一些可行的解法。可以使用倍增的思想快速跳过所有第一个礼物前的石头,如图所示:
只要前 个数和第 到 的重量相等,那么就可以证明前 都相等。反之则可以证明答案在第 到 之间。此时,我们可以进行标准的二分查找,在前 个之中选择和 到 数量相等的箱子(因为前面的都是石头,后面的有可能有礼物)。如果重量相等:,否则 ,直到 即为答案。我们到目前总共用了 最多 次操作。可是,我们目前的所有做法都基于第一个箱子不是礼物。为了保证这个做法成立,我们需要先判断第一个礼盒是不是礼物(如果是,直接输出 ! 1 结束)。方式也很简单,暴力找 个位置,和第一个点比较,如果出现有大于第一个点的数,那么第一个点就是礼物,否则,就认为第一个点是石头(因为如果第一个点是礼物,结合题面给出礼物数量小于 ,所以错误率也小于 )。
代码
#include<bits/stdc++.h>
using namespace std;
int t , n , k , p , l , r , mid , ans;
string s;
void seed()
{
auto now = std::chrono::system_clock::now();
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto t = ms.time_since_epoch().count();
srand(t);
return;
}
void ask(int as , int ae , int bs , int be)
{
cout << "? " << ae - as + 1 << ' ' << be - bs + 1 << endl;
for(int i = as ; i <= ae ; i++)
{
cout << i << ' ';
}
cout << endl;
for(int i = bs ; i <= be ; i++)
{
cout << i << ' ';
}
cout << endl;
cin >> s;
if(s == "WASTED")
{
cout << "ERROR" << endl;
exit(0);
}
return;
}
bool first()
{
for(int i = 1 ; i <= 20 ; i++)
{
int tmp = rand() % (n - 1) + 2;
ask(1 , 1 , tmp , tmp);
if(s == "SECOND")
{
return 1;
}
}
return 0;
}
bool check(int q)
{
ask(1 , q - p , p + 1 , q);
if(s == "FIRST")
{
return 1;
}
return 0;
}
int main()
{
seed();
cin >> t;
while(t--)
{
cin >> n >> k;
if(first())
{
cout << "! 1" << endl;
continue;
}
p = 1;
while(p < n)
{
if(p * 2 >= n)
{
l = p + 1;
r = n;
break;
}
ask(1 , p , p + 1 , p * 2);
if(s == "FIRST")
{
l = p + 1;
r = p * 2;
break;
}
p *= 2;
}
while(l <= r)
{
mid = (l + r) / 2;
if(check(mid))
{
ans = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
cout << "! " << ans << endl;
}
return 0;
}CF2022D2 Asesino (Hard Version) *2700
每局游戏有若干骑士、骗子但只有一个冒名顶替者。
骑士是好人阵营,骗子和冒名顶替者是坏人阵营。
如果询问一个人和自己相同阵营的人会回答 Yes,否则会回答 No。
冒名顶替者很调皮,会回答相反的答案。
| \ | 骑士 | 骗子 | 冒名顶替者 |
|---|---|---|---|
| 骑士 | Yes | No | Yes |
| 骗子 | No | Yes | No |
| 冒名顶替者 | No | Yes | — |
请你使用最少的询问次数来确定谁是冒名顶替者。
交互库自适应!
。
s MB
题解
2x Bonus CF2022D1.
神仙题。
观察发现若 中有一人是冒名顶替者当且仅当询问 ? x y 和 ? y x 的答案不同。
于是若 为偶数可以通过在 至 个元素两两排除,在最后两个元素中再判断其中一个做到 次询问。
若 为奇数则在最后的判断中是留下 个数字,做到 次询问。
此时已经可以通过简单版本。
偶数显然已经是最优。
我们考虑手摸 。发现 次已经是最优,不可能减少。
我们发现若 ,则可先用 次判断前 个中是否有冒名顶替者,若有,通过 次判断是哪个;若无,按照偶数时的方法解决剩下的位置。到达 次操作,显然最优。
只有 有些难办。可以使用惊人的观察力发现,若询问 ? 1 2、? 2 3、? 3 1 且其中存在内鬼,则在后续判断中会有相同询问被使用,于是我们可以记录以上询问获得的值,并且在后续需要的时候直接调用。
代码
#include<bits/stdc++.h>
using namespace std;
int t , n , a , b , c;
bool flag;
int main()
{
cin >> t;
while(t--)
{
cin >> n;
if(n == 3)
{
cout << "? 1 2" << endl;
cin >> a;
cout << "? 2 1" << endl;
cin >> b;
if(a == b)
{
cout << "! 3" << endl;
continue;
}
cout << "? 1 3" << endl;
cin >> a;
cout << "? 3 1" << endl;
cin >> b;
if(a == b)
{
cout << "! 2" << endl;
}
else
{
cout << "! 1" << endl;
}
continue;
}
if(n == 4)
{
cout << "? 1 2" << endl;
cin >> a;
cout << "? 2 1" << endl;
cin >> b;
if(a == b)
{
cout << "? 1 3" << endl;
cin >> a;
cout << "? 3 1" << endl;
cin >> b;
if(a == b)
{
cout << "! 4" <<endl;
}
else
{
cout << "! 3" << endl;
}
continue;
}
cout << "? 1 3" << endl;
cin >> a;
cout << "? 3 1" << endl;
cin >> b;
if(a == b)
{
cout << "! 2" << endl;
}
else
{
cout << "! 1" << endl;
}
continue;
}
if(n == 5)
{
int a12 , a23 , a31;
cout << "? 1 2" << endl;
cin >> a12;
cout << "? 2 3" << endl;
cin >> a23;
cout << "? 3 1" << endl;
cin >> a31;
if((a12 + a23 + a31) % 2 == 1)
{
cout << "? 1 4" << endl;
cin >> a;
cout << "? 4 1" << endl;
cin >> b;
if(a == b)
{
cout << "! 5" << endl;
}
else
{
cout << "! 4" << endl;
}
}
else
{
cout << "? 2 1" << endl;
cin >> a;
if(a12 == a)
{
cout << "! 3" << endl;
}
else
{
cout << "? 3 2" << endl;
cin >> a;
if(a23 == a)
{
cout << "! 1" << endl;
}
else
{
cout << "! 2" << endl;
}
}
}
continue;
}
if(n % 2 == 1)
{
cout << "? 1 2" << endl;
cin >> a;
cout << "? 2 3" << endl;
cin >> b;
cout << "? 3 1" << endl;
cin >> c;
if((a + b + c) % 2 == 1)
{
flag = 1;
for(int i = 4 ; i + 2 <= n ; i += 2)
{
cout << "? " << i << ' ' << i + 1 << endl;
cin >> a;
cout << "? " << i + 1 << ' ' << i << endl;
cin >> b;
if(a != b)
{
cout << "? 1 " << i << endl;
cin >> a;
cout << "? " << i << " 1" << endl;
cin >> b;
if(a == b)
{
cout << "! " << i + 1 << endl;
}
else
{
cout << "! " << i << endl;
}
flag = 0;
break;
}
}
if(flag)
{
cout << "? 1 " << n << endl;
cin >> a;
cout << "? " << n << " 1" << endl;
cin >> b;
if(a == b)
{
cout << "! " << n - 1 << endl;
}
else
{
cout << "! " << n << endl;
}
}
}
else
{
cout << "? 2 1" << endl;
cin >> c;
if(a == c)
{
cout << "! 3" << endl;
}
else
{
cout << "? 3 2" << endl;
cin >> a;
if(a == b)
{
cout << "! 1" << endl;
}
else
{
cout << "! 2" << endl;
}
}
}
continue;
}
else
{
cout << "? 1 2" << endl;
cin >> a;
cout << "? 2 1" << endl;
cin >> b;
if(a == b)
{
flag = 1;
for(int i = 3 ; i + 2 <= n ; i += 2)
{
cout << "? " << i << ' ' << i + 1 << endl;
cin >> a;
cout << "? " << i + 1 << ' ' << i << endl;
cin >> b;
if(a != b)
{
cout << "? 1 " << i << endl;
cin >> a;
cout << "? " << i << " 1" << endl;
cin >> b;
if(a == b)
{
cout << "! " << i + 1 << endl;
}
else
{
cout << "! " << i << endl;
}
flag = 0;
break;
}
}
if(flag)
{
cout << "? 1 " << n << endl;
cin >> a;
cout << "? " << n << " 1" << endl;
cin >> b;
if(a == b)
{
cout << "! " << n - 1 << endl;
}
else
{
cout << "! " << n << endl;
}
}
}
else
{
cout << "? 1 3" << endl;
cin >> a;
cout << "? 3 1" << endl;
cin >> b;
if(a == b)
{
cout << "! 2" << endl;
}
else
{
cout << "! 1" << endl;
}
}
}
}
return 0;
}CF1838F Stuck Conveyor *3000
一个 的正方形区域,有 个传送带方块,你每次可以指定每个方块的方向,并且在其中任意一个位置投放一个物品,物品会沿方块所指的方向运动,你最后会得知方块出来的位置 或方块进入了死循环 。但是有一个方块不会受到你的控制,而是会固定朝向一个方向,请你在 次分配内找到这个方块以及它的朝向。
题解
打表找规律,可以发现:
- 如果出现了死循环,那么意味着你设计的路线比较靠后的一个点卡住了,跳到了比较靠前的点。
- 如果在错误的位置走出循环,那么说明紧挨着的点就是卡住的点。
- 如果正确,那么说明要么方向和你设计的路线相同,要么从一个比较靠前的点跳到了比较靠后的点。
所以我们可以设计出类似蛇形的地图:
>>>>v
v<<<<
>>>>v
v<<<<
>>>>v
^<<<<
>>>>^
^<<<<
>>>>^
^<<<<此时可以发现这两张地图非常巧妙的让所有正确的情况在两张图里面没有交集,即:如果一个点在第一张图成功,那么一定是:
- 第一张图左、下成功对应第二张图右、上成功
- 第一张图右、下成功对应第二张图左、上成功
换句话说,无论如何也有一张图会让它死循环或出界。
此时,问题就被划分为了两个子问题:出界、死循环。
出界问题相对简单,直接判掉就可以了。
死循环的情况相对复杂,需要先确定哪张图能够导致死循环,然后再做下一步处理。确定之后就最坏情况还剩 次询问,恰好我们需要在最多 个数里面找,联想 级别的算法,联想二分算法。
我们可以把矩阵转换成一条链,在链上二分,找到到底是哪个点出现了问题。此时,答案即为那个点的 坐标。
可是丧心病狂的出题人还要我们输出方向。
我们可以使用“冲浪”矩阵:
对于第一组地图构造:
vvvvv
vvvvv
vvvvv
vvvvv
vvvvv对于第二组地图构造:
^^^^^
^^^^^
^^^^^
^^^^^
^^^^^- 如果物品的 坐标等于答案的 坐标,那么答案就是顺着矩阵的方向。
- 如果物品的 坐标小于答案的 坐标,答案为
<。 - 如果物品的 坐标大于答案的 坐标,答案为
>。
这题就这么用特判堆过了。
期望最大询问次数:。
(询问第一组地图构造)(询问第二组地图构造)(二分答案)。
代码
#include<bits/stdc++.h>
using namespace std;
struct p
{
int x , y;
};
int n , x , y , l , r , mid , ans;
vector<p> v;
char c;
void zmp()
{
for(int i = 1 ; i <= n ; i++)
{
if(i % 2)
{
for(int j = 1 ; j < n ; j++)
{
cout << '>';
}
cout << 'v' << endl;
}
else
{
cout << 'v';
for(int j = 1 ; j < n ; j++)
{
cout << '<';
}
cout << endl;
}
}
return;
}
void fmp()
{
for(int i = 1 ; i <= n ; i++)
{
if(i % 2)
{
cout << '^';
for(int j = 1 ; j < n ; j++)
{
cout << '<';
}
cout << endl;
}
else
{
for(int j = 1 ; j < n ; j++)
{
cout << '>';
}
cout << '^' << endl;
}
}
return;
}
int main()
{
cin >> n;
cout << "? 1 1" << endl;
zmp();
cin >> x >> y;
if(x != -1 && y != -1)
{
if(n % 2)
{
if(x != n + 1 || y != n)
{
if(x < 1)
{
x++;
cout << "! " << x << ' ' << y << " ^" << endl;
return 0;
}
if(x > n)
{
x--;
cout << "! " << x << ' ' << y << " v" << endl;
return 0;
}
if(y < 1)
{
y++;
cout << "! " << x << ' ' << y << " <" << endl;
return 0;
}
if(y > n)
{
y--;
cout << "! " << x << ' ' << y << " >" << endl;
return 0;
}
}
}
else
{
if(x != n + 1 || y != 1)
{
if(x < 1)
{
x++;
cout << "! " << x << ' ' << y << " ^" << endl;
return 0;
}
if(x > n)
{
x--;
cout << "! " << x << ' ' << y << " v" << endl;
return 0;
}
if(y < 1)
{
y++;
cout << "! " << x << ' ' << y << " <" << endl;
return 0;
}
if(y > n)
{
y--;
cout << "! " << x << ' ' << y << " >" << endl;
return 0;
}
}
}
if(n % 2)
{
cout << "? " << n << ' ' << n << endl;
fmp();
cin >> x >> y;
if(x != -1 && y != -1)
{
if(x < 1)
{
x++;
cout << "! " << x << ' ' << y << " ^" << endl;
return 0;
}
if(x > n)
{
x--;
cout << "! " << x << ' ' << y << " v" << endl;
return 0;
}
if(y < 1)
{
y++;
cout << "! " << x << ' ' << y << " <" << endl;
return 0;
}
if(y > n)
{
y--;
cout << "! " << x << ' ' << y << " >" << endl;
return 0;
}
}
else
{
for(int i = n ; i >= 1 ; i--)
{
if(i % 2)
{
for(int j = n ; j >= 1 ; j--)
{
v.push_back(p{i , j});
}
}
else
{
for(int j = 1 ; j <= n ; j++)
{
v.push_back(p{i , j});
}
}
}
l = 0;
r = v.size() - 1;
while(l <= r)
{
mid = (l + r) / 2;
cout << "? " << v[mid].x << ' ' << v[mid].y << endl;
fmp();
cin >> x >> y;
if(x == -1 && y == -1)
{
ans = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
cout << "? " << n << ' ' << v[ans].y << endl;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
cout << '^';
}
cout << endl;
}
cin >> x >> y;
if(x == -1 && y == -1)
{
cout << "! " << v[ans].x << ' ' << v[ans].y << " v" << endl;
return 0;
}
else
{
if(v[ans].y < y)
{
c = '>';
}
else
{
c = '<';
}
cout << "! " << v[ans].x << ' ' << v[ans].y << ' ' << c << endl;
return 0;
}
}
}
else
{
cout << "? " << n << ' ' << 1 << endl;
fmp();
cin >> x >> y;
if(x != -1 && y != -1)
{
if(x < 1)
{
x++;
cout << "! " << x << ' ' << y << " ^" << endl;
return 0;
}
if(x > n)
{
x--;
cout << "! " << x << ' ' << y << " v" << endl;
return 0;
}
if(y < 1)
{
y++;
cout << "! " << x << ' ' << y << " <" << endl;
return 0;
}
if(y > n)
{
y--;
cout << "! " << x << ' ' << y << " >" << endl;
return 0;
}
}
else
{
for(int i = n ; i >= 1 ; i--)
{
if(i % 2)
{
for(int j = n ; j >= 1 ; j--)
{
v.push_back(p{i , j});
}
}
else
{
for(int j = 1 ; j <= n ; j++)
{
v.push_back(p{i , j});
}
}
}
l = 0;
r = v.size() - 1;
while(l <= r)
{
mid = (l + r) / 2;
cout << "? " << v[mid].x << ' ' << v[mid].y << endl;
fmp();
cin >> x >> y;
if(x == -1 && y == -1)
{
ans = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
cout << "? " << n << ' ' << v[ans].y << endl;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
cout << '^';
}
cout << endl;
}
cin >> x >> y;
if(x == -1 && y == -1)
{
cout << "! " << v[ans].x << ' ' << v[ans].y << " v" << endl;
return 0;
}
else
{
if(v[ans].y < y)
{
c = '>';
}
else
{
c = '<';
}
cout << "! " << v[ans].x << ' ' << v[ans].y << ' ' << c << endl;
return 0;
}
}
}
}
else
{
for(int i = 1 ; i <= n ; i++)
{
if(i % 2)
{
for(int j = 1 ; j <= n ; j++)
{
v.push_back(p{i , j});
}
}
else
{
for(int j = n ; j >= 1 ; j--)
{
v.push_back(p{i , j});
}
}
}
l = 0;
r = v.size() - 1;
while(l <= r)
{
mid = (l + r) / 2;
cout << "? " << v[mid].x << ' ' << v[mid].y << endl;
zmp();
cin >> x >> y;
if(x == -1 && y == -1)
{
ans = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
cout << "? 1 " << v[ans].y << endl;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
cout << 'v';
}
cout << endl;
}
cin >> x >> y;
if(x == -1 && y == -1)
{
cout << "! " << v[ans].x << ' ' << v[ans].y << " ^" << endl;
return 0;
}
else
{
if(v[ans].y < y)
{
c = '>';
}
else
{
c = '<';
}
cout << "! " << v[ans].x << ' ' << v[ans].y << ' ' << c << endl;
return 0;
}
}
return 0;
}


