Submission #718577

# Submission time Handle Problem Language Result Execution time Memory
718577 2023-04-04T11:00:13 Z myrcella Sorting (IOI15_sorting) C++17
Compilation error
0 ms 0 KB
//by szh
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;

#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}


#include "sorting.h"

const int maxn = 6e5+10;
 
int n,m;
int a[maxn],b[maxn];
pii ans[maxn];
pii p[maxn];
deque <int> q[maxn];
int id[maxn];
pii pos[maxn];	
pii qid[maxn];
 
bool check(int cnt) {
	rep(i,0,n) {
		while (!q[i].empty()) q[i].pop_back();
		q[i].pb(i);
		id[i] = i;
		b[i] = a[i];
	}
	rep(i,0,cnt) {
		int x=p[i].fi,y=p[i].se;
		if (x==y) continue;
		q[id[x]].pb(y);
		q[id[y]].pb(x);
		swap(id[x],id[y]);
		qid[i] = {id[x],id[y]};
	}
	rep(i,0,n) {
		pos[q[i].back()].fi = q[i].front();
		pos[a[i]].se = i;
	}
	int hi = 0;
	rep(i,0,cnt) {
		int x = p[i].fi,y=p[i].se;
		if (x!=y) {
    		swap(b[x],b[y]);
    		pos[b[x]].se = x,pos[b[y]].se = y;
    		
    		q[qid[i].fi].pop_front(),q[qid[i].se].pop_front();
    		pos[q[qid[i].fi].back()].fi = q[qid[i].fi].front();
    		pos[q[qid[i].se].back()].fi = q[qid[i].se].front();
		}
		pii cur = {0,0};
		while (hi<n and pos[hi].fi==pos[hi].se) hi++;
		if (hi<n) cur = pos[hi];
		x = cur.fi,y=cur.se;
		if (x!=y) {
		    swap(b[x],b[y]);
		    pos[b[x]].se = x,pos[b[y]].se = y;
		}
      	ans[i] = cur;
	}
	while (hi<n and pos[hi].fi==pos[hi].se) hi++;
	return hi==n;
}
 
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N,m= M;
    int l=0,r=M;
    rep(i,0,n) a[i] =S[i];
    rep(i,0,M) p[i] = {X[i],Y[i]};
    while (l<r) {
		int mid=l+r>>1;
		if (check(mid)) r = mid;
		else l = mid+1;
	}
	assert(check(l));
	rep(i,0,l) P[i] = ans[i].fi, Q[i] = ans[i].se;
	return l;
}

int main()
{
	int N, M;
	scanf("%d",&N);
	int S[233],X[233],Y[233],P[233],Q[233];
	for (int i = 0; i < N; ++i)
		scanf("%d",&S[i]);
	scanf("%d",&M);
	for (int i = 0; i < M; ++i) {
	    scanf("%d%d",&X[i],&Y[i]);
	}
	int ans = findSwapPairs(N, S, M, X, Y, P, Q);
	printf("%d\n", ans);
	for (int i = 0; i < ans; ++i)
		printf("%d %d\n", P[i], Q[i]);
	return 0;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:89:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   89 |   int mid=l+r>>1;
      |           ~^~
sorting.cpp: In function 'int main()':
sorting.cpp:109:6: warning: declaration of 'ans' shadows a global declaration [-Wshadow]
  109 |  int ans = findSwapPairs(N, S, M, X, Y, P, Q);
      |      ^~~
sorting.cpp:32:5: note: shadowed declaration is here
   32 | pii ans[maxn];
      |     ^~~
sorting.cpp:101:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |  scanf("%d",&N);
      |  ~~~~~^~~~~~~~~
sorting.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |   scanf("%d",&S[i]);
      |   ~~~~~^~~~~~~~~~~~
sorting.cpp:105:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |  scanf("%d",&M);
      |  ~~~~~^~~~~~~~~
sorting.cpp:107:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |      scanf("%d%d",&X[i],&Y[i]);
      |      ~~~~~^~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccY2Y95s.o: in function `main':
grader.c:(.text.startup+0x0): multiple definition of `main'; /tmp/ccxFj31r.o:sorting.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status