Submission #723057

#TimeUsernameProblemLanguageResultExecution timeMemory
723057sunwukong123Sorting (IOI15_sorting)C++14
20 / 100
1 ms468 KiB
#include <bits/stdc++.h> using namespace std; #define FOR(i,s,e) for(int i = s; i <= (int)e; ++i) #define DEC(i,s,e) for(int i = s; i >= (int)e; --i) #define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL #define db(x) cerr << #x << "=" << x << "\n" #define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n" #define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n" #define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n" #define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n" #define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n" #define reach cerr << "LINE: " << __LINE__ << "\n"; #else #define reach #define db(x) #define db2(x,y) #define db3(a,b,c) #define dbv(v) #define dbvp(v) #define dba(a,ss,ee) #endif mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define f first #define s second #define g0(x) get<0>(x) #define g1(x) get<1>(x) #define g2(x) get<2>(x) #define g3(x) get<3>(x) typedef pair <int, int> pi; typedef tuple<int,int,int> ti3; typedef tuple<int,int,int,int> ti4; int rand(int a, int b) { return a + rng() % (b-a+1); } const int MOD = 1e9 + 7; const int inf = (int)1e9 + 500; const long long oo = (long long)1e18 + 500; template <typename T> void chmax(T& a, const T b) { a=max(a,b); } template <typename T> void chmin(T& a, const T b) { a=min(a,b); } const int MAXN = 200005; int A[MAXN]; int n,m; int X[MAXN], Y[MAXN]; int idx[MAXN]; struct ufds_{ int p[MAXN],sz[MAXN]; ufds_() { iota(p,p+MAXN,0); fill(sz,sz+MAXN,1); } int find(int x) { if(x==p[x])return x; else return p[x]=find(p[x]); } void merge(int x, int y) { x=find(x);y=find(y); if(x==y)return; if(sz[x]>=sz[y]){ sz[x]+=sz[y]; p[y]=x; } else { sz[y]+=sz[x]; p[x]=y; } } }; bool check(int ti) { //you can use ti operations to do everything FOR(i,0,ti-1) { swap(A[X[i]],A[Y[i]]); } FOR(i,0,n-1){ idx[A[i]]=i; } db(ti); int res = 0; dba(A,0,n-1); FOR(i,0,n-1) { int y=idx[i]; db2(i,y); if(i==y)continue; else { // swap index i to index y. db2(i,y); ++res; swap(A[i],A[y]); swap(idx[A[i]],idx[A[y]]); } } db2(res,ti); return res<=ti; } int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) { //FOR(i,0,n-1)A[i]=S[i]; ::n=n; ::m=M; FOR(i,0,m-1)::X[i]=X[i]; FOR(i,0,m-1)::Y[i]=Y[i]; int lo=-1, hi=m+1; while(lo<hi-1) { FOR(i,0,n-1)A[i]=S[i]; int mid=(lo+hi)/2; db(mid); if(check(mid))hi=mid; else lo=mid; } int R=hi; db(R); // i am doing R number of operations. FOR(i,0,n-1)A[i]=S[i]; FOR(i,0,R-1) { swap(A[X[i]],A[Y[i]]); } vector<pi> ops; FOR(i,0,n-1){ idx[A[i]]=i; } FOR(i,0,n-1) { int y=idx[i]; if(i==y)continue; else { // swap index i to index y. ops.pb({A[i],A[y]}); swap(A[i],A[y]); swap(idx[A[i]],idx[A[y]]); } } while(ops.size()<R)ops.pb({0,0}); reverse(all(ops)); FOR(i,0,n-1)A[i]=S[i]; FOR(i,0,n-1)idx[A[i]]=i; reach FOR(i,0,R-1) { db(i); pi cur=ops.back(); ops.pop_back(); int val1=cur.f, val2=cur.s; int Aidx=idx[val1], Bidx=idx[val2]; P[i]=Aidx; Q[i]=Bidx; swap(A[Aidx],A[Bidx]); swap(idx[val1],idx[val2]); swap(A[X[i]],A[Y[i]]); swap(idx[A[X[i]]], idx[A[Y[i]]]); dba(A,0,n-1); } return R; }

Compilation message (stderr)

sorting.cpp: In function 'int rand(int, int)':
sorting.cpp:36:35: warning: conversion from 'std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   36 | int rand(int a, int b) { return a + rng() % (b-a+1); }
      |                                 ~~^~~~~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:98:55: warning: declaration of 'Y' shadows a global declaration [-Wshadow]
   98 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                   ~~~~^~~
sorting.cpp:45:14: note: shadowed declaration is here
   45 | int X[MAXN], Y[MAXN];
      |              ^
sorting.cpp:98:46: warning: declaration of 'X' shadows a global declaration [-Wshadow]
   98 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                          ~~~~^~~
sorting.cpp:45:5: note: shadowed declaration is here
   45 | int X[MAXN], Y[MAXN];
      |     ^
sorting.cpp:98:23: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   98 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:44:5: note: shadowed declaration is here
   44 | int n,m;
      |     ^
sorting.cpp:135:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  135 |  while(ops.size()<R)ops.pb({0,0});
      |        ~~~~~~~~~~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...