Gym 100685 J Just Another Disney Problem 趣题,稳定排序

J. Just Another Disney Problem
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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, XY (1 ≤ X, Y ≤ NX ≠ 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 test(s)
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.


题目链接:http://codeforces.com/gym/100685/problem/J

题目大意:给定你大小未知的n个数,你允许有不超过一万次的询问,每次询问两个数,第i个数是否比第j个数小?然后后台会返回给你一个结果YES或者NO(即一行输入),然后经过多次询问后,你需要给出一个正确的原未知序列的升序排列。

思路:由于n不超过1000,nlogn刚好不超过10000,那么比较次数稳定不超过nlogn的排序就均是可以的,只需要更改判断的部分即可。这里采用了内置的函数,修改了比较规则。简短可AC

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
#define pll pair<LL, LL>
#define pii pair<int, int>
#define X first
#define Y second
#define MAXN 200010
#define lson l, mid, (rt << 1)
#define rson mid + 1, r, (rt << 1 | 1)
const double eps = 1e-10;
bool cmp(int a, int b) {
    printf("1 %d %d\n", a, b);
    string s;
    cin >> s;
    return s[0] == 'Y';
}
int main() {
    int n;
    cin >> n;
    int a[1005];
    for(int i = 0; i < n; i++) {
        a[i] = i + 1;
    }
    stable_sort(a, a + n, cmp);
    cout << 0;
    for(int i = 0; i < n; i++) {
        cout << ' ' << a[i];
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值