icpc:problems:703c_guessing_the_greatest
problems | |
---|---|
名称 | Guessing the Greatest |
题目编号 | 703C |
题目链接 | codeforces.com/… |
来源 | CodeForces |
算法分类 | 二分 |
难易程度 | 小有难度 |
Guessing the Greatest
想法
这是一个半交互的题目,我们可以确定第二个最大值,然后再找第一个最大值在左范围还是右范围,之后二分找这个最大值。因为最大值左边第二个值一定不是原来的第二个值,而最大值右边第二个值一定是最大的值。所以二分找最大值在哪。
代码实现
#include<cstdio>//uncle-lu int ask(int x, int y) { printf("? %d %d\n", x, y); fflush(stdout); int temp; scanf("%d", &temp); return temp; } int main() { int n, smax, temp; scanf("%d", &n); smax = ask(1, n); if(smax != 1)temp = ask(1, smax); if(smax != 1 && temp == smax) { int l = 1, r = smax, mid; while(l + 1 < r) { mid = (l+r)>>1; if(ask(mid, smax) == smax) l = mid; else r = mid; } printf("! %d", l); } else { int l = smax, r = n+1, mid; while(l + 1 < r) { mid = (l+r)>>1; if(ask(smax, mid) == smax) r = mid; else l = mid; } printf("! %d", r); } return 0; }
/app/www/public/data/pages/icpc/problems/703c_guessing_the_greatest.txt · 最后更改: 2023/10/01 10:06 由 温婕莺