首页 > 其他分享 >CodeForces 1526F Median Queries

CodeForces 1526F Median Queries

时间:2023-12-03 12:11:06浏览次数:32  
标签:1526F typedef vc int Median CodeForces long mx define

洛谷传送门

CF 传送门

首先,如果我们确定了 \(1, 2\) 或 \(n - 1, n\) 的位置,我们就能求出原排列。因为题目给了 \(p_1 < p_2\),所以可以不考虑对称性。也就是说我们知道两个位置是 \(1, 2\) 或 \(n - 1, n\) 但不确定究竟是 \(1, 2\) 还是 \(n - 1, n\) 也可以。

然后,如果我们确定了一对挨得够近的一对数 \(i, j\),用它们再去问剩下的,距离最大和次大的就是 \(1, 2\) 或 \(n - 1, n\)。(有个 corner case,若 \(p_i + p_j = n - 1\),那么最大和次大值都有两个,注意判一下)

发现只要 \(|i - j| \le \frac{n - 4}{3}\),距离最大和次大的就是 \(1, 2\) 或 \(n - 1, n\)。

上面用了 \(2(n - 2)\) 次操作,还剩下 \(424\) 次操作留给随机化。随机问一些 \((a, b, c)\),如果回答 \(\le \frac{n - 4}{6}\),那它们两两距离都 \(\le \frac{n - 4}{3}\)。操作次数很充裕,随不到的概率 \(< 10^{-10}\)。

code
// Problem: F. Median Queries
// Contest: Codeforces - Codeforces Round 723 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1526/F
// Memory Limit: 256 MB
// Time Limit: 6000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 100100;

int n, a[maxn];
vector<int> vc[maxn];
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

inline int ask(int x, int y, int z) {
	printf("? %d %d %d\n", x, y, z);
	fflush(stdout);
	scanf("%d", &x);
	return x;
}

void solve() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		vector<int>().swap(vc[i]);
	}
	int mn = 1e9, p = -1, q = -1;
	for (int _ = 0; _ < 420; ++_) {
		int x, y, z;
		do {
			x = rnd() % n + 1;
			y = rnd() % n + 1;
			z = rnd() % n + 1;
		} while (x == y || x == z || y == z);
		int t = ask(x, y, z);
		if (t < mn) {
			mn = t;
			p = x;
			q = y;
		}
	}
	int mx = -1e9;
	for (int i = 1; i <= n; ++i) {
		if (i != p && i != q) {
			int t = ask(i, p, q);
			mx = max(mx, t);
			vc[t].pb(i);
		}
	}
	if ((int)vc[mx - 1].size() > 1 && ask(vc[mx][0], p, vc[mx - 1][0]) > ask(vc[mx][0], p, vc[mx - 1][1])) {
		swap(vc[mx - 1][0], vc[mx - 1][1]);
	}
	p = vc[mx][0];
	q = vc[mx - 1][0];
	a[p] = 1;
	a[q] = 2;
	for (int i = 1; i <= n; ++i) {
		if (i != p && i != q) {
			a[i] = ask(i, p, q) + 2;
		}
	}
	if (a[1] > a[2]) {
		for (int i = 1; i <= n; ++i) {
			a[i] = n - a[i] + 1;
		}
	}
	printf("! ");
	for (int i = 1; i <= n; ++i) {
		printf("%d ", a[i]);
	}
	putchar('\n');
	fflush(stdout);
	scanf("%d", &n);
	if (n == -1) {
		exit(0);
	}
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:1526F,typedef,vc,int,Median,CodeForces,long,mx,define
From: https://www.cnblogs.com/zltzlt-blog/p/17872817.html

相关文章

  • Codeforces Round 878 (Div. 3)
    CodeforcesRound878(Div.3)A:ABCA.CipherShifer题意:在自身后面添加一个字母,但是不能添加自身思路:找到第二个与自身相符的就再找#include<bits/stdc++.h>usingnamespacestd;constintMAX=110;chara[MAX];voidsolve(){intn;cin>>n;for(......
  • [Codeforces] CF1627B Not Sitting
    题意Rahul和Tina在玩一个游戏。游戏在一个\(n\timesm\)的网格图上进行,记第\(r\)行第\(c\)列上的格子为\((r,c)\)。定义\((a,b)\)与\((c,d)\)之间的距离为\(\left|a-c\right|+\left|b-d\right|\)。游戏开始后,Tina会选择恰好\(k\)个格子,并将其涂成粉红色。涂......
  • [Codeforces] CF1659B Bit Flipping
    题面给定一个长为\(n\)的01串,你可以进行\(k\)次操作。每次操作中,你可以选择任意一位,并将除了这一位以外的其它位翻转(\(1\)变\(0\),\(0\)变\(1\)),输出\(k\)次操作后能获得的字典序最大的字符串,并输出每一位在操作中被选择的次数。若有多解输出任意一解。思路可以发现......
  • [Codeforces] CF1675D Vertical Paths
    题目描述给定一棵由\(n\)个顶点组成的有根树。顶点由\(1\)到\(n\)编号。任何顶点都可以是树的根。请在树上找出这样一组路径:每个顶点恰好属于一条路径,每条路径可以包含一个或多个顶点;在每条路径中,每个节点的下一个节点是当前节点的子节点(即路径总是向下——从父节点......
  • Codeforces Round 912 (Div. 2) E - Geo Game
    考虑什么时候会改变答案的奇偶,显然可以根据\(x\oplusy\)的奇偶性分组,在组内进行跳跃不会改变,只有当组间跳跃的时候才会改变。打表观察先手什么时候必胜,其中:\(u\)是当前获胜目标为奇/偶(1/0),\(v\)是位于哪一组,\(a,b\)代表两组还剩多少,\(st\)代表当前答案的奇偶性。intdfs(intu,......
  • 【ErikTse】2023-Codeforces新手训练营 第六期题解
    A.Wrath题目大意给你一个\(L\)数组和\(n\)个人,第\(i\)个人可以使用威力为\(L_i\)的闪电旋风劈击杀前面\(L_i\)人,问你最后能存活多少人?思路差分。开一个数组来标记当前威力的闪电旋风劈能击杀到的最远的人和使用技能的人,最远击杀的人所在的位置+1,自己的位置-1,这样算前缀和时所......
  • Codeforces Round 912 (Div. 2)
    CodeforcesRound912(Div.2)基本概述最难受的一集。A题秒了。B题幸苦推了两个小时,最后也通过了pretest了,结果赛后被HACK。C题知道是DP,但觉得不好推状态转移方程,所以全心全意去做B题了。爆掉\(150\)分B.StORageroom我的思路其实就几乎是答案。之前几乎没怎......
  • [Codeforces] CF1591C Minimize Distance
    CF1591CMinimizeDistance题目一条线上有\(n\)(\(1\len\le2\cdot10^5\))个仓库,第\(i\)个仓库的位置是\(x_i\)(\(1\lei\len\))。你有\(n\)箱货物,要分别运到这\(n\)个仓库里。你的初始位置在点\(0\),一次可以携带\(k\)(\(1\lek\len\))箱货物。在送完携带......
  • [Codeforces] CF1603A Di-visible Confusion
    CF1603ADi-visibleConfusion题目给一个长度为\(n\)的序列\(a_1,a_2,\dots,a_n\),对于每个位置\(i\),如果\(a_i\%\left(i+1\right)\not=0\),就可以将\(a_i\)删掉。删掉之后,后面的数都会往前面移动一位。问能否将序列删成空。数据范围\(1\let\le10^4,1\len\le10^5,1\le......
  • Codeforces Round 883 (Div. 3)
    CodeforcesRound883(Div.3)A.RudolphandCuttheRope题意:有一颗糖果在连在绳子上,求剪短多少根绳子,他能落地思路:只要绳子长度比钉子高度大就不用减#include<bits/stdc++.h>usingnamespacestd;voidsolve(){intn,res=0;cin>>n;while(n--)......