Codeforces Round #504 E. Down or Right

题目描述:交互题。 有一个\(n \times n\)的方阵,有一些格子是障碍,从\((1, 1)\)出发,只能向右向下走,能走到\((n, n)\),你有\(4n\)次询问,每次询问\((r_1, c_1)\)能否走到\((r_2, c_2)\),但这两个点的曼哈顿距离要大于\(n-1\),最后输出一条从\((1, 1)\)到\((n, n)\)的路径。

solution

从\((1, 1)\)出发,优先向下走,向下走能到\((n, n)\)就向下走,走到对角线。然后从\((n, n)\)出发,优先向左走,\((1, 1)\)能到左边的点就向左走,走到对角线,这样构造能保证最终在对角线的点一定重合。

因为优先向下走能保证\(D_1-R_1\)最大,那后面一半的\(R_2-D_2=(n-1-R_1)-(n-1-D_1)=D_1-R_1\)最大,而优先向左走正是保证\(R_2-D_2\)最大,因此对角线的点一定重合。

时间复杂度:\(O(2n)\)

#include <bits/stdc++.h>
using namespace std; int n;
vector<char> ans;
char st[10]; bool ask(int r1, int c1, int r2, int c2)
{
printf("? %d %d %d %d\n", r1, c1, r2, c2);
fflush(stdout);
scanf("%s", st);
return st[0]=='Y';
}
void solve()
{
scanf("%d", &n);
int x=1, y=1;
for (int i=1; i<=n-1; ++i)
if (ask(x+1, y, n, n)) x++, ans.push_back('D');
else y++, ans.push_back('R'); x=n, y=n;
for (int i=1; i<=n-1; ++i)
if (ask(1, 1, x, y-1)) y--, ans.push_back('R');
else x--, ans.push_back('D'); printf("! ");
for (int i=0; i<n-1; ++i) putchar(ans[i]);
for (int i=n*2-2-1; i>=n-1; --i) putchar(ans[i]);
puts("");
fflush(stdout);
}
int main()
{
solve();
return 0;
}

Codeforces Round #504 E. Down or Right的更多相关文章

  1. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  2. Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...

  3. E - Down or Right Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1023/problem/E 交互题 #include <cstdio> #include <cstdlib> #i ...

  4. Codeforces Round #504:D. Array Restoration

    D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...

  5. Codeforces Round 504

    (交互题真神奇,,,我自己瞎写了一发目测样例都没过去就AC了...) (只出了两题的竟然没掉下蓝名真是可怕) A:我的代码太不美观了,放个同学的(因为我是c++63分的蒟蒻所以根本不知道那些函数怎么用 ...

  6. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration

    我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...

  7. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C-Bracket Subsequence

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  8. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-A-Single Wildcard Pattern Matching

    #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...

  9. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) E. Down or Right

    从(1,1,n,n)每次只变一个坐标,进行询问. 如果问到对角线有距离限制, 再从(1,1,n/2,n/2)询问到(n/2,n/2,n,n) 记住前半部分贪心忘上走,后本部分贪心往右走 因为最后的路线 ...

随机推荐

  1. hdu 5638 Toposort (拓扑排序+线段树)

    Toposort Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  2. UltraISO制作U盘启动盘

    第一步:导入镜像文件 文件->打开 第二步: 启动->写入硬盘映像 隐藏启动分区选择“无” 然后等待即可.

  3. 【BZOJ1835】基站选址(线段树)

    [BZOJ1835]基站选址(线段树) 题面 BZOJ 题解 考虑一个比较暴力的\(dp\) 设\(f[i][j]\)表示建了\(i\)个基站,最后一个的位置是\(j\)的最小代价 考虑如何转移\(f ...

  4. bzoj3533【Sdoi2014】向量集

    题目描述 维护一个向量集合,在线支持以下操作:"A x y (|x|,|y| < =10^8)":加入向量(x,y);" Q x y l r (|x|,|y| &l ...

  5. C++ explicit constructor/copy constructor note

    C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用 #include <iostream> #include ...

  6. 浏览器json数据格式化

    在浏览器上作接口测试的时候看到json 格式的数据是密密麻麻的一片,眼睛都花了..  如: 设置方法:  chrome  的右上角选择,然后---  更多工具---  扩展程序  ----   JSO ...

  7. R语言外部数据读取

    0  引言 使用R语言.Python等进行数据处理的第一步就是要导入数据(也可以使用UCI数据集),下文主要根据R语言的帮助文档来介绍外部文件数据的导入方法和注意事项.下面先附上一些指令. 1 格式r ...

  8. Spring MVC原理介绍

    1.Spring Web MVC是什么 spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解 ...

  9. bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=4519 最小割树 #include<queue> #include<cstdio&g ...

  10. 动态规划:POJ No 2385 Apple Catching

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...