Just Another Disney Problem
2015-10-09 15:24
246 查看
题目描述:
J. Just Another Disney ProblemTime Limit: 1000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d Java class name: (Any)
Submit Status PID: 49360
Input/Output: standard input/output
Evil wizard Jafar has a huge collection of lamps. He likes touching them, wiping the dust, looking at his reflection in them. He loves all of them almost equally, they are very beautiful, but for every two of them he likes one of them more than another one. Jafar keeps his lamps in a very long hall, all lamps in one line. One day he decided to arrange all lamps along his way from one side of the hall to another in such a way, that for every two neighboring lamps he likes the next one more than previous. In other words Jafar would like a some kind of an ascending order of lamp’s quality. You are a new servant of wizard and you should fulfill the desire of your master. The main problem is that you don’t know anything about Jafar’s preferences. You can ask Jafar for any two lamps which one is better, but you should be careful, he is very busy now in his plans for world domination and you shouldn’t ask him too much questions. Note that preferences could be non-transitive. You should output desired order of all lapms or report Jafar that it doesn’t exist.
Input
First number — N (1 ≤ N ≤ 1000). Answer for every question — string “YES”, if Y is better than X, and “NO”, if X is better than Y.
Output
Your questions — one line with three integers 1, X, Y (1 ≤ X, Y ≤ N, X ≠ Y). You should ask not more than 10 000 questions. In the last line: integer 0, then N integers ai (1 ≤ ai ≤ N) — the desired permutation or N zeros if such permutation doesn’t exist. All integers in the lines should be separated by spaces.
Sample Input
Note
The pipe from your program to the interactor program and the pipe back have limited size. Your program must read from the standard input to avoid deadlock. Deadlock condition is reported as Time-limit exceeded.
To flush the standard output stream use the following statements:
In C use fflush(stdout);
In C++ use cout.flush();
In Java use System.out.flush();
If your program receives EOF (end-of-file) condition on the standard input, it MUST exit immediately with exit code 0. Failure to comply with this requirement may result in Time-limit exceeded error.
题解:
挺好的一道题目,思路:1的位置已经定了,然后问2和1的关系,然后问3的关系,每次问和谁的关系都是先问中间的,然后二分,这样<=10次就可以确定,然后插入.这道题目用n^2的插入就好
重点:
类似于开心辞典的二分询问代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <ctype.h> #include <limits.h> #include <cstdlib> #include <algorithm> #include <vector> #include <queue> #include <map> #include <stack> #include <set> #include <bitset> #define CLR(a) memset(a, 0, sizeof(a)) #define REP(i, a, b) for(int i = a;i < b;i++) #define REP_D(i, a, b) for(int i = a;i <= b;i++) typedef long long ll; using namespace std; const int maxn = 1e3+100; int a[maxn], b[maxn]; int n; char str[20]; void solve() { a[1] = 1; for(int i = 2;i<=n;i++) { int l = 1, r = i; while(l < r) { int mid = (l+r)/2; printf("1 %d %d\n", i, a[mid]); fflush(stdout); scanf("%s", str); if(str[0] =='Y') { r = mid; } else { l = mid+1; } } for(int j = 1;j<r;j++) b[j] = a[j]; b[r] = i; for(int j = r;j<=i-1;j++) b[j+1] = a[j]; for(int j = 1;j<=i;j++) a[j] = b[j]; } printf("0"); fflush(stdout); for(int i = 1;i<=n;i++) { printf(" %d", a[i]); fflush(stdout); } printf("\n"); fflush(stdout); } int main() { //freopen("10Jin.txt", "r", stdin); //freopen("10Jout.txt", "w", stdout); while(scanf("%d", &n)!=EOF) { solve(); } return 0; }
相关文章推荐
- 一些技术书籍推荐
- Xcode—https 方式使用git代码托管
- 字符编码笔记:ASCII,Unicode和UTF-8
- Android中打开手机中存在的图片软件
- XML学习小记(一)
- javascript的闭包和变量
- android浏览器开发小技巧集锦(转)
- MySQL导入.sql文件及常用命令
- Lecture 2 Introduction
- JS键盘的键码(event.keyCode)
- 2015年四川省赛||弱校联萌十一大决战之强力热身 H. Range Query 二分图的最大匹配确定匹配顺序
- poj3259解题报告
- PostGreSQL数据库的检查
- linux CentOS 6.5 中安装与配置JDK-7
- 在PHP中为文本框设置默认值
- POJ 2449 K短路
- 无向图中节点的迭代得到从起始节点到结束节点之间的所有路径,并从中得到最短路径的节点
- 【createJS】:点击Bitmap上传图片的纠结
- 去掉a标签的虚线框,避免出现奇怪的选中区域
- Diesel engine looks after shop for just about every individual member of my family because of the deals