Codeforces Round #601 (Div. 2) F. Point Ordering (凸包+交互)
题面:
题意:
给定一个整数(mathit n),代表有一个(mathit n)个点的凸包,但是点的顺序是乱的,并且每一个点的具体坐标是不给你的,但是你可以做不超过(3 imes n) 个询问,你需要在程序结束前输出一个(1dots n) 的全排列,使其对应的点的顺序是一个逆时针的凸包。
每一个询问有以下两种类型:
1、你输出(i,j,k),系统返回给你这三个点对应的三角形的面积的二倍。
2、你输出(i,j,k),系统返回给你
(overrightarrow{a_ia_j},overrightarrow{a_ia_k}) 这两个向量的叉积的数值符号。
思路:
我们分为三步来解决该问题。
1、
任取([1,n])中两个不同的点(直接取点1和点2更方面),将其他(n-2)个点分为在线段((a_1,a_2)) 的两侧(某一侧可能为空)。
2、
对于两侧的每一部分(S_1,S_2),我们分别对其独立的求(S_{Delta 12j},jin S_1or S_2)。
根据凸包的性质,从上面的询问中,可以获得(S_1,S_2)中
距离线段((a_1,a_2)) 最远的点,而对于每一部分,它们若按照逆时针顺序的话,点到线段的距离(可以由面积得到),应该是先递增到峰值,然后递减。
3、
那么我们既然可以根据面积确定了峰值,就可以直接询问叉积的符号来确定每一部分中除了峰值的其他点在峰值的左边还是右边。就可以确定出整个凸包的你指针下标排列了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int sign_1[maxn];
ll area[maxn];
std::vector<pair<ll, int>> v1, v2;
std::vector<pair<ll, int>> ans1, ans2, ans3, ans4;
int main()
{
#if DEBUG_Switch
freopen("D:\code\input.txt", "r", stdin);
#endif
//freopen("D:\code\output.txt","w",stdout);
cin >> n;
repd(i, 3, n) {
cout << 2 << " " << 1 << " " << 2 << " " << i << endl;
cin >> sign_1[i];
}
repd(i, 3, n) {
cout << 1 << " " << 1 << " " << 2 << " " << i << endl;
cin >> area[i];
}
repd(i, 3, n) {
if (sign_1[i] == -1) {
v1.push_back(mp(area[i], i));
} else {
v2.push_back(mp(area[i], i));
}
}
sort(ALL(v1));
sort(ALL(v2));
reverse(ALL(v1));
reverse(ALL(v2));
int temp;
if (sz(v1) > 0) {
for (int i = 1; i < sz(v1); ++i) {
cout << 2 << " " << 1 << " " << v1[0].se << " " << v1[i].se << endl;
cin >> temp;
if (temp == -1) {
ans3.push_back(v1[i]);
} else {
ans4.push_back(v1[i]);
}
}
ans3.push_back(v1[0]);
}
if (sz(v2) > 0) {
for (int i = 1; i < sz(v2); ++i) {
cout << 2 << " " << 1 << " " << v2[0].se << " " << v2[i].se << endl;
cin >> temp;
if (temp == -1) {
ans1.push_back(v2[i]);
} else {
ans2.push_back(v2[i]);
}
}
ans1.push_back(v2[0]);
}
sort(ALL(ans1));
sort(ALL(ans2));
reverse(ALL(ans2));
sort(ALL(ans3));
sort(ALL(ans4));
reverse(ALL(ans4));
cout << "0 1";
for (auto x : ans3) {
cout << " " << x.se;
}
for (auto x : ans4) {
cout << " " << x.se;
}
cout << " 2";
for (auto x : ans1) {
cout << " " << x.se;
}
for (auto x : ans2) {
cout << " " << x.se;
}
cout << endl;
return 0;
}