답안 #824140

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
824140 2023-08-13T15:19:52 Z Khizri 저울 (IOI15_scales) C++17
100 / 100
18 ms 5416 KB
#include "scales.h"
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
#define endl "\n"
const int mxn=2e5+5;
vector<vector<int>>vt[mxn];
int L[mxn],R[mxn],M[mxn],iq[mxn],jq[mxn],kq[mxn],dq[mxn],op[mxn],nxt=1,que[mxn];
int fnd(int i,int j,int k,int a,vector<int>&vt){
    if(max(vt[i],max(vt[j],vt[k]))<vt[a]){
        int mn=min(vt[i],min(vt[j],vt[k]));
        if(mn==vt[i]) return i;
        if(mn==vt[j]) return j;
        return k;
    }
    vector<int>v={vt[i],vt[j],vt[k]};
    sort(all(v));
    int mn;
    if(v[0]>vt[a]){
        mn=v[0];
    }
    else if(v[1]>vt[a]){
        mn=v[1];
    }
    else{
        mn=v[2];
    }
    if(mn==vt[i]) return i;
    if(mn==vt[j]) return j;
    return k;
}
void build(int node){
    ll mx=INF;
    int ix,jx,kx,dx=0;
    for(int i=0;i<6;i++){
        for(int j=i+1;j<6;j++){
            for(int k=j+1;k<6;k++){
                ll a=0,b=0,c=0;
                for(int idx=0;idx<vt[node].size();idx++){
                    if(vt[node][idx][i]>max(vt[node][idx][j],vt[node][idx][k])){
                        a++;
                    }
                    else if(vt[node][idx][j]>max(vt[node][idx][i],vt[node][idx][k])){
                        b++;
                    }
                    else{
                        c++;
                    }
                }
                if(max({a,b,c})<=que[node]){
                    op[node]=1;
                    ix=i,jx=j,kx=k;
                }
            }
        }
    }
    for(int i=0;i<6;i++){
        for(int j=i+1;j<6;j++){
            for(int k=j+1;k<6;k++){
                ll a=0,b=0,c=0;
                for(int idx=0;idx<vt[node].size();idx++){
                    if(vt[node][idx][i]<min(vt[node][idx][j],vt[node][idx][k])){
                        a++;
                    }
                    else if(vt[node][idx][j]<min(vt[node][idx][i],vt[node][idx][k])){
                        b++;
                    }
                    else{
                        c++;
                    }
                }
                if(max({a,b,c})<=que[node]){
                    op[node]=2;
                    ix=i,jx=j,kx=k;
                }
            }
        }
    }
    for(int i=0;i<6;i++){
        for(int j=i+1;j<6;j++){
            for(int k=j+1;k<6;k++){
                ll a=0,b=0,c=0;
                for(int idx=0;idx<vt[node].size();idx++){
                    if(vt[node][idx][i]>min(vt[node][idx][j],vt[node][idx][k])&&vt[node][idx][i]<max(vt[node][idx][j],vt[node][idx][k])){
                        a++;
                    }
                    else if(vt[node][idx][j]>min(vt[node][idx][i],vt[node][idx][k])&&vt[node][idx][j]<max(vt[node][idx][i],vt[node][idx][k])){
                        b++;
                    }
                    else{
                        c++;
                    }
                }
                if(max({a,b,c})<=que[node]){
                    mx=a*a*a+b*b*b+c*c*c;
                    op[node]=3;
                    ix=i,jx=j,kx=k;
                }
            }
        }
    }
    for(int d=0;d<6;d++){
        for(int i=0;i<6;i++){
            if(i==d) continue;
            for(int j=i+1;j<6;j++){
                if(j==d) continue;
                for(int k=j+1;k<6;k++){
                    if(k==d) continue;
                    ll a=0,b=0,c=0;
                    for(int idx=0;idx<vt[node].size();idx++){
                        int res=fnd(i,j,k,d,vt[node][idx]);
                        if(res==i){
                            a++;
                        }
                        else if(res==j){
                            b++;
                        }
                        else{
                            c++;
                        }
                    }
                    if(max({a,b,c})<=que[node]){
                        mx=a*a*a+b*b*b+c*c*c;
                        op[node]=4;
                        ix=i,jx=j,kx=k,dx=d;
                    }
                }
            }
        }
    }
    L[node]=++nxt;
    M[node]=++nxt;
    R[node]=++nxt;
    que[L[node]]=que[node]/3;
    que[R[node]]=que[node]/3;
    que[M[node]]=que[node]/3;
    iq[node]=ix;
    jq[node]=jx;
    kq[node]=kx;
    dq[node]=dx;
    if(op[node]==1){
        for(int idx=0;idx<vt[node].size();idx++){
            if(vt[node][idx][ix]>max(vt[node][idx][jx],vt[node][idx][kx])){
                vt[L[node]].pb(vt[node][idx]);
            }
            else if(vt[node][idx][jx]>max(vt[node][idx][ix],vt[node][idx][kx])){
                vt[M[node]].pb(vt[node][idx]);
            }
            else{
                vt[R[node]].pb(vt[node][idx]);
            }
        }
    }
    else if(op[node]==2){
        for(int idx=0;idx<vt[node].size();idx++){
            if(vt[node][idx][ix]<min(vt[node][idx][jx],vt[node][idx][kx])){
                vt[L[node]].pb(vt[node][idx]);
            }
            else if(vt[node][idx][jx]<min(vt[node][idx][ix],vt[node][idx][kx])){
                vt[M[node]].pb(vt[node][idx]);
            }
            else{
                vt[R[node]].pb(vt[node][idx]);
            }
        }
    }
    else if(op[node]==3){
        for(int idx=0;idx<vt[node].size();idx++){
            if(vt[node][idx][ix]>min(vt[node][idx][jx],vt[node][idx][kx])&&vt[node][idx][ix]<max(vt[node][idx][jx],vt[node][idx][kx])){
                vt[L[node]].pb(vt[node][idx]);
            }
            else if(vt[node][idx][jx]>min(vt[node][idx][ix],vt[node][idx][kx])&&vt[node][idx][jx]<max(vt[node][idx][ix],vt[node][idx][kx])){
                vt[M[node]].pb(vt[node][idx]);
            }
            else{
                vt[R[node]].pb(vt[node][idx]);
            }
        }
    }
    else{
        for(int idx=0;idx<vt[node].size();idx++){
            int res=fnd(ix,jx,kx,dx,vt[node][idx]);
            if(res==ix){
                vt[L[node]].pb(vt[node][idx]);
            }
            else if(res==jx){
                vt[M[node]].pb(vt[node][idx]);
            }
            else{
                vt[R[node]].pb(vt[node][idx]);
            }
        }
    }
    if(vt[L[node]].size()>1){
        build(L[node]);
    }
    if(vt[M[node]].size()>1){
        build(M[node]);
    }
    if(vt[R[node]].size()>1){
        build(R[node]);
    }
}
vector<int>query(int node){
    if(vt[node].size()==1){
        return vt[node][0];
    }
    int x;
    if(op[node]==1){
        x=getHeaviest(iq[node]+1,jq[node]+1,kq[node]+1);
    }
    else if(op[node]==2){
        x=getLightest(iq[node]+1,jq[node]+1,kq[node]+1);
    }
    else if(op[node]==3){
        x=getMedian(iq[node]+1,jq[node]+1,kq[node]+1);
    }
    else{
        x=getNextLightest(iq[node]+1,jq[node]+1,kq[node]+1,dq[node]+1);
    }
    if(x==iq[node]+1){
        return query(L[node]);
    }
    else if(x==jq[node]+1){
        return query(M[node]);
    }
    else{
        return query(R[node]);
    }
}
void init(int T) {
    vector<int>v(6);
    iota(all(v),1);
    do{
        vt[1].pb(v);
    }while(next_permutation(all(v)));
    que[1]=243;
    build(1);
}
 
void orderCoins() {
    vector<int>vt=query(1);
    int arr[6];
    for(int i=0;i<6;i++){
        arr[vt[i]-1]=i+1;
    }
    answer(arr);
}
/*
g++ scales.cpp grader.cpp ; .\a.exe
*/

Compilation message

scales.cpp: In function 'int fnd(int, int, int, int, std::vector<int>&)':
scales.cpp:19:45: warning: declaration of 'vt' shadows a global declaration [-Wshadow]
   19 | int fnd(int i,int j,int k,int a,vector<int>&vt){
      |                                 ~~~~~~~~~~~~^~
scales.cpp:17:20: note: shadowed declaration is here
   17 | vector<vector<int>>vt[mxn];
      |                    ^~
scales.cpp: In function 'void build(int)':
scales.cpp:49:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |                 for(int idx=0;idx<vt[node].size();idx++){
      |                               ~~~^~~~~~~~~~~~~~~~
scales.cpp:60:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
   60 |                 if(max({a,b,c})<=que[node]){
      |                    ~~~~~~~~~~~~^~~~~~~~~~~
scales.cpp:71:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |                 for(int idx=0;idx<vt[node].size();idx++){
      |                               ~~~^~~~~~~~~~~~~~~~
scales.cpp:82:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
   82 |                 if(max({a,b,c})<=que[node]){
      |                    ~~~~~~~~~~~~^~~~~~~~~~~
scales.cpp:93:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |                 for(int idx=0;idx<vt[node].size();idx++){
      |                               ~~~^~~~~~~~~~~~~~~~
scales.cpp:104:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
  104 |                 if(max({a,b,c})<=que[node]){
      |                    ~~~~~~~~~~~~^~~~~~~~~~~
scales.cpp:120:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |                     for(int idx=0;idx<vt[node].size();idx++){
      |                                   ~~~^~~~~~~~~~~~~~~~
scales.cpp:132:36: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
  132 |                     if(max({a,b,c})<=que[node]){
      |                        ~~~~~~~~~~~~^~~~~~~~~~~
scales.cpp:152:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |         for(int idx=0;idx<vt[node].size();idx++){
      |                       ~~~^~~~~~~~~~~~~~~~
scales.cpp:165:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |         for(int idx=0;idx<vt[node].size();idx++){
      |                       ~~~^~~~~~~~~~~~~~~~
scales.cpp:178:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |         for(int idx=0;idx<vt[node].size();idx++){
      |                       ~~~^~~~~~~~~~~~~~~~
scales.cpp:191:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  191 |         for(int idx=0;idx<vt[node].size();idx++){
      |                       ~~~^~~~~~~~~~~~~~~~
scales.cpp:43:8: warning: variable 'mx' set but not used [-Wunused-but-set-variable]
   43 |     ll mx=INF;
      |        ^~
scales.cpp: In function 'void init(int)':
scales.cpp:241:15: warning: unused parameter 'T' [-Wunused-parameter]
  241 | void init(int T) {
      |           ~~~~^
scales.cpp: In function 'void orderCoins()':
scales.cpp:252:16: warning: declaration of 'vt' shadows a global declaration [-Wshadow]
  252 |     vector<int>vt=query(1);
      |                ^~
scales.cpp:17:20: note: shadowed declaration is here
   17 | vector<vector<int>>vt[mxn];
      |                    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5360 KB Output is correct
2 Correct 15 ms 5392 KB Output is correct
3 Correct 13 ms 5388 KB Output is correct
4 Correct 13 ms 5332 KB Output is correct
5 Correct 14 ms 5320 KB Output is correct
6 Correct 13 ms 5392 KB Output is correct
7 Correct 13 ms 5376 KB Output is correct
8 Correct 13 ms 5376 KB Output is correct
9 Correct 13 ms 5332 KB Output is correct
10 Correct 13 ms 5324 KB Output is correct
11 Correct 13 ms 5308 KB Output is correct
12 Correct 14 ms 5356 KB Output is correct
13 Correct 13 ms 5332 KB Output is correct
14 Correct 13 ms 5416 KB Output is correct
15 Correct 13 ms 5352 KB Output is correct
16 Correct 13 ms 5392 KB Output is correct
17 Correct 14 ms 5332 KB Output is correct
18 Correct 13 ms 5332 KB Output is correct
19 Correct 13 ms 5372 KB Output is correct
20 Correct 13 ms 5356 KB Output is correct
21 Correct 13 ms 5400 KB Output is correct
22 Correct 13 ms 5332 KB Output is correct
23 Correct 13 ms 5332 KB Output is correct
24 Correct 13 ms 5332 KB Output is correct
25 Correct 17 ms 5376 KB Output is correct
26 Correct 13 ms 5376 KB Output is correct
27 Correct 15 ms 5332 KB Output is correct
28 Correct 13 ms 5332 KB Output is correct
29 Correct 15 ms 5328 KB Output is correct
30 Correct 13 ms 5332 KB Output is correct
31 Correct 13 ms 5380 KB Output is correct
32 Correct 13 ms 5324 KB Output is correct
33 Correct 13 ms 5412 KB Output is correct
34 Correct 18 ms 5296 KB Output is correct
35 Correct 13 ms 5332 KB Output is correct
36 Correct 18 ms 5304 KB Output is correct
37 Correct 13 ms 5316 KB Output is correct
38 Correct 13 ms 5380 KB Output is correct
39 Correct 13 ms 5380 KB Output is correct
40 Correct 18 ms 5332 KB Output is correct