Submission #1039948

#TimeUsernameProblemLanguageResultExecution timeMemory
1039948ZicrusSorting (IOI15_sorting)C++17
36 / 100
983 ms748 KiB
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;

typedef long long ll;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    vector<ll> h(N);
    for (int i = 0; i < N; i++) h[i] = S[i];

    ll ooo = 0;
    for (int i = 0; i < N; i++) {
        if (h[i] != i) ooo++;
    }
    if (ooo == 0) return 0;

    ll res1 =0;
    ll low = 1, high = M;
    while (low < high) {
        ll g = (low + high) / 2;

        ll res = 0;
        vector<ll> s = h;
        for (int i = 0; i < g; i++) {
            swap(s[X[i]], s[Y[i]]);
            vector<ll> order = s;
            for (int j = i+1; j < M; j++) {
                swap(order[X[j]], order[Y[j]]);
            }
            int firstOut = -1, id = -1;
            for (int j = 0; j < N; j++) {
                if (order[j] != j) {
                    firstOut = order[id = j];
                    break;
                }
            }
            if (firstOut == -1) {
                P[res] = Q[res] = 0;
                res++;
            }
            else {
                ll v = *find(order.begin(), order.end(), id);

                ll id1 = find(s.begin(), s.end(), firstOut) - s.begin();
                ll id2 = find(s.begin(), s.end(), v) - s.begin();
                swap(s[id1], s[id2]);
                P[res] = id1;
                Q[res] = id2;
                res++;
            }

            firstOut = -1;
            for (int j = 0; j < N; j++) {
                if (s[j] != j) {
                    firstOut = s[j];
                    break;
                }
            }
            if (firstOut == -1) return res;
        }
        ll ooo = 0;
        for (int i = 0; i < N; i++) {
            if (s[i] != i) ooo++;
        }
        if (ooo == 0) {
            res1 = res;
            high = g;
        }
        else {
            low = g+1;
        }
    }
	return res1;
}

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:33:44: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
   33 |                     firstOut = order[id = j];
      |                                            ^
sorting.cpp:47:26: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   47 |                 P[res] = id1;
      |                          ^~~
sorting.cpp:48:26: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   48 |                 Q[res] = id2;
      |                          ^~~
sorting.cpp:55:35: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
   55 |                     firstOut = s[j];
      |                                   ^
sorting.cpp:59:40: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   59 |             if (firstOut == -1) return res;
      |                                        ^~~
sorting.cpp:61:12: warning: declaration of 'ooo' shadows a previous local [-Wshadow]
   61 |         ll ooo = 0;
      |            ^~~
sorting.cpp:11:8: note: shadowed declaration is here
   11 |     ll ooo = 0;
      |        ^~~
sorting.cpp:73:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   73 |  return res1;
      |         ^~~~
#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...