# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
532834 |
2022-03-04T03:33:55 Z |
ivan24 |
Sorting (IOI15_sorting) |
C++14 |
|
0 ms |
0 KB |
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
#define F first
#define S second
ll min (ll x, ll y){
return ((x < y) ? x : y);
}
ll max (ll x, ll y){
return ((x > y) ? x : y);
}
const ll INF = 1e18;
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
cout.setstate(ios_base::failbit);
p[0] = 0; q[0] = 1;
bool perfect = true;
for (ll i = 0; n > i; i++){
if (s[i] != i) perfect = false;
}
if (perfect) return 0;
ll lo = 1,hi = m,md;
md = (lo+hi/2);
while (hi >= lo){
md = (lo+hi)/2;
cout << "md: " << md << endl;
vi v,v_idx;
v.resize(n);
v_idx.resize(n);
for (ll i = 0; n > i; i++){
v[i] = s[i]; v_idx[v[i]] = i;
}
for (ll i = 0; md > i; i++){
swap(v[x[i]],v[y[i]]);
swap(v_idx[v[x[i]]],v_idx[v[y[i]]]);
}
vi cur,cur_idx;
cur.resize(n);
cur_idx.resize(n);
vii swaps;
swaps.assign(md,ii(0,0));
for (ll i = 0; n > i; i++){
cur[i] = i; cur_idx[i] = i;
}
/*
cout << "v: ";
for (auto i: v) cout << i << " ";
cout << endl;
cout << "v_idx: ";
for (auto i: v_idx) cout << i << " ";
cout << endl;
cout << "cur: ";
for (auto i: cur) cout << i << " ";
cout << endl;
cout << "cur_idx: ";
for (auto i: cur_idx) cout << i << " ";
cout << endl;
*/
ll ptr = 0;
for (ll i = md-1; i >= 0; i--){
while (n > ptr && cur_idx[ptr] == v_idx[ptr]) ptr++;
if (ptr >= n) continue;
//cout << ptr << endl;
swaps[i] = ii(cur_idx[ptr],cur_idx[v[cur_idx[ptr]]]);
swap(cur[cur_idx[ptr]],cur[cur_idx[v[cur_idx[ptr]]]]);
swap(cur_idx[ptr],cur_idx[v[cur_idx[ptr]]]);
/*
cout << "swapped: " << swaps[i].F << " and " << swaps[i].S << " ptr: " << ptr << endl;
cout << "cur: ";
for (auto i: cur) cout << i << " ";
cout << endl;
cout << "cur_idx: ";
for (auto i: cur_idx) cout << i << " ";
cout << endl;
cout << "v: ";
for (auto i: v) cout << i << " ";
cout << endl;
cout << "v_idx: ";
for (auto i: v_idx) cout << i << " ";
cout << endl;
*/
swap(v_idx[v[x[i]]], v_idx[v[y[i]]]);
swap(v[x[i]],v[y[i]]);
swap(cur_idx[cur[x[i]]],cur_idx[cur[y[i]]]);
swap(cur[x[i]],cur[y[i]]);
}
bool works = true;
for (ll i = 0; n > i; i++) works = works && (cur[i] == v[i]);
/*
cout << "after: \n";
cout << "v: ";
for (auto i: v) cout << i << " ";
cout << endl;
cout << "v_idx: ";
for (auto i: v_idx) cout << i << " ";
cout << endl;
cout << "cur: ";
for (auto i: cur) cout << i << " ";
cout << endl;
cout << "cur_idx: ";
for (auto i: cur_idx) cout << i << " ";
cout << endl;
*/
if (works){
hi = md;
for (ll i = 0; md > i; i++){
p[i] = swaps[i].F;
q[i] = swaps[i].S;
}
}else
lo = md+1;
if (hi == lo && hi == md) return md;
}
return md;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sorting.h"
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;
static inline int _read() {
if (_charsNumber < 0) {
exit(1);
}
if (!_charsNumber || _currentChar == _charsNumber) {
_charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
_currentChar = 0;
}
if (_charsNumber <= 0) {
return -1;
}
return _buffer[_currentChar++];
}
static inline int _readInt() {
int c, x, s;
c = _read();
while (c <= 32) c = _read();
x = 0;
s = 1;
if (c == '-') {
s = -1;
c = _read();
}
while (c > 32) {
x *= 10;
x += c - '0';
c = _read();
}
if (s < 0) x = -x;
return x;
}
int main()
{
_inputFile = fopen("sorting2.in", "rb");
_outputFile = fopen("sorting2.out", "w");
int N, M;
N = _readInt();
int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
for (int i = 0; i < N; ++i)
S[i] = _readInt();
M = _readInt();
int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
for (int i = 0; i < M; ++i) {
X[i] = _readInt();
Y[i] = _readInt();
}
int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
int ans = findSwapPairs(N, S, M, X, Y, P, Q);
fprintf(_outputFile, "%d\n", ans);
for (int i = 0; i < ans; ++i)
fprintf(_outputFile, "%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:11:11: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
11 | #define F first
sorting.cpp:120:33: note: in expansion of macro 'F'
120 | p[i] = swaps[i].F;
| ^
sorting.cpp:12:11: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
12 | #define S second
sorting.cpp:121:33: note: in expansion of macro 'S'
121 | q[i] = swaps[i].S;
| ^
sorting.cpp:125:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
125 | if (hi == lo && hi == md) return md;
| ^~
sorting.cpp:127:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
127 | return md;
| ^~
/usr/bin/ld: /tmp/ccQnB5pJ.o: in function `main':
grader.c:(.text.startup+0x0): multiple definition of `main'; /tmp/ccia7eKK.o:sorting.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status