Submission #1040084

#TimeUsernameProblemLanguageResultExecution timeMemory
1040084Zicrus정렬하기 (IOI15_sorting)C++17
74 / 100
1091 ms21072 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 P1[], int Q1[]) {
    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;

        vector<int> P(M), Q(M);

        ll res = 0;
        vector<ll> s = h;
        vector<ll> order = s;
        for (int j = 0; j < g; j++) {
            swap(order[X[j]], order[Y[j]]);
        }
        for (int i = 0; i < g; i++) {
            swap(s[X[i]], s[Y[i]]);
            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 {
                auto iter = find(order.begin(), order.end(), id);
                ll v = *iter;

                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]);
                swap(order[iter - order.begin()], order[id]);
                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) break;
        }
        ll ooo = 0;
        for (int i = 0; i < N; i++) {
            if (s[i] != i) ooo++;
        }
        if (ooo == 0) {
            res1 = res;
            high = g;

            for (int i = 0; i < res; i++) {
                P1[i] = P[i];
                Q1[i] = Q[i];
            }
        }
        else {
            low = g+1;
        }
    }
	return res1;
}

Compilation message (stderr)

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