Submission #959587

# Submission time Handle Problem Language Result Execution time Memory
959587 2024-04-08T13:21:11 Z 8pete8 Sorting (IOI15_sorting) C++17
36 / 100
1 ms 684 KB
#include "sorting.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
//#define int long long
#define double long double
const int mod=1e9+7,mxn=3e5+5,lg=60,inf=1e9,minf=-1e9;
int n;
int getmin(vector<int>v,vector<int>pos){
	int ans=0;
	vector<int>rev(n);
	for(int i=0;i<n;i++)rev[v[i]]=i;
	for(int i=0;i<n;i++){
		if(v[pos[i]]==v[rev[i]])continue;
		rev[v[pos[i]]]=rev[i];
		swap(v[pos[i]],v[rev[i]]);
		ans++;
	}
	return ans;
}
int findSwapPairs(int N, int v[], int m, int x[], int y[], int p[], int q[]) {
	n=N;
	int l=0,r=m;
	vector<int>pos(n);
	for(int i=0;i<n;i++)pos[i]=i;//position that will end up at i
	vector<int>o;
	for(int i=0;i<n;i++)o.pb(v[i]);
	auto get=[&](int mid){
		int ans=0,cur=0;
		vector<int>rev(n);
		for(int i=0;i<n;i++)rev[v[i]]=i;
		vector<int>pos2(n);//where we will end up standing at i
		for(int i=0;i<n;i++)pos2[i]=i;
		for(int i=0;i<mid;i++)swap(pos2[x[i]],pos2[y[i]]);
		for(int i=0;i<n;i++)pos[pos2[i]]=i;
		vector<pii>what(mid);
		for(int i=0;i<mid;i++){
			swap(pos[pos2[x[i]]],pos[pos2[y[i]]]);
			swap(pos2[x[i]],pos2[y[i]]);
			swap(rev[v[x[i]]],rev[v[y[i]]]);
			swap(v[x[i]],v[y[i]]);
			while(cur<n&&pos[cur]==rev[cur])cur++;
			if(cur<n){
				what[i]={pos[cur],rev[cur]};
				swap(rev[v[what[i].f]],rev[v[what[i].s]]);
				swap(v[what[i].f],v[what[i].s]);
				cur++;
				ans++;
			}
			else what[i]={0,0};
		}
		while(cur<n&&pos[cur]==rev[cur])cur++;
		return make_pair((cur>=n),what);
	};
	pair<int,vector<pii>>ans{inf,{}};
	while(l<=r){
		int mid=l+(r-l)/2;
		int cnt=0;
		pair<int,vector<pii>>bru=get(mid);
		if(bru.f)r=mid-1,ans=min(ans,{mid,bru.s});
		else l=mid+1;
		for(int i=0;i<n;i++)v[i]=o[i];
	}
	for(int i=0;i<ans.f;i++)p[i]=ans.s[i].f,q[i]=ans.s[i].s;
	return ans.f;
}/*
int32_t main(){
	int n;cin>>n;
	vector<int>v(n);
	for(int i=0;i<n;i++)cin>>v[i];
	int m;cin>>m;
	vector<int>x(m),y(m);
	for(int i=0;i<m;i++)cin>>x[i]>>y[i];
	vector<int>p(m),q(m);
	cout<<findSwapPairs(n,v,m,x,y,p,q);
}*/
/*
observation:
each starting element will end up somewhere after swapping
each element have distinct path to the end position
we can swap element to swap their path
swap can be made in any order?
if the min num of swap <= num of action
then its doable
min num of swap can increase by at most 1 per action (swapping it back to same pos)
so num of action-min num of swap is monotone
we can do bs? and check o(n)?
finding min num of swap: greedy?
*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:88:7: warning: unused variable 'cnt' [-Wunused-variable]
   88 |   int cnt=0;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 684 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 684 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Incorrect 1 ms 604 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -