답안 #212427

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212427 2020-03-22T23:29:17 Z Rayaabualjamal 카멜레온의 사랑 (JOI20_chameleon) C++14
100 / 100
71 ms 504 KB
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <queue>
#include <iomanip> 
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(j, a, b) for (int j = a; j >= b; j--)
#include "chameleon.h"
#include <cstdio>
#include <cstdlib>
using namespace std;
//To build sub vectors easily:
vector <int> sub_vector(int b, int e, vector <int>& f){
    vector <int> r(e-b);
    rep(i,0,e-b){
        r[i]=f[i+b];
    }
    return r;
}
//To binary search:
int binary(int s, int ee, int e, vector <int>& curr){
    int start=s, end = ee;
    while(start<end-1)
    {
        int middle = (start+end)/2;
        vector <int> sub = sub_vector(middle,e, curr);
        // for(int i:sub){
        //     cout << i << " ";
        // }
        //cout << endl;
        int see = Query(sub);
        if(sub.size()==see)
            end=middle;
        else
            start=middle;
    }
    return start;
}
//visited is to not Answer the same couple twice
vector <bool> visited;
//verti is where I keep the special vertices for each vertex
vector <vector <int> > verti;
vector <pair <int, int> > graph;
//dfs to build the graph:
void dfs(int p){
    vector <int> ch = {2,1,1};
    if(graph[p].first!=-1&&graph[p].second!=-1){
        return ;
    }
    
    rep(j,1,3){
        vector <int> ctry = {p, verti[p][0], verti[p][j]};
        if(Query(ctry)==1){
            ch[0]-=100;
            ch[j]-=100;
            break;
        }
    }
    rep(i,0,3){
        if(ch[i]>0){
            graph[p].second = verti[p][i];
            graph[verti[p][i]].first = p;
            dfs(verti[p][i]);
            break;
        }
    }
    return ;
}
void Solve(int N) {
    int n=N*2;
    vector <vector <int> > v(4);
    verti.resize(n+1);
    visited.assign(n+1,0);
    graph.assign(n+1,{-1,-1});
    // loop thro all vertices:
    rep(i,1, n+1){
        vector <int> curr;
        vector<int> which= {-1,-1,-1,-1};
        //find all special vertices for this vertex:
        rep(j,0,4){
            curr = v[j];
            curr.push_back(i);
            int check = Query(curr);
            //if it has no special vertices in this vector store it's size:
            if(check==curr.size()){
                which[j]=curr.size();
                continue;
            }
          	int start = 0, end = curr.size();
          	//binary seach all possible special vertices that is in this vector:
            while(check!=curr.size()){
                int remov = binary(start, end, curr.size(), curr);
                verti[i].push_back(curr[remov]);
                verti[curr[remov]].push_back(i);
              	end = remov;
                curr.erase (curr.begin()+remov);
                //check if there is still a special vertex:
                check = Query(curr);
            }
        }
        //choose the place that has the least size to insert 'i' in it 
        int place = 5, capasity = 0;
        rep(i,0,4){
            if(which[i]!=-1&& capasity<which[i]){
                place=i;
            }
        }
        v[place].push_back(i);
    }
    rep(i,1,n+1){
        //if already done skip
        if(visited[i])continue;
        //if it has a only one kid
        if(verti[i].size()==1){
            Answer(i,verti[i][0]);
            visited[i]=1;
            visited[verti[i][0]]=1;
        } else if(verti[i].size()>1&& graph[i].first!=-1){
            //if it's graph was built: get the answer
            rep(j,0,3){
                if(verti[i][j]!=graph[i].first&& verti[i][j]!=graph[i].second){
                    Answer(i,verti[i][j]);
                    visited[i]=1;
                    visited[verti[i][j]]=1;
                }
            }
        } else if(verti[i].size()>1){
            //if it's graph was not built: build the graph then answer
            dfs(i);
            rep(j,0,3){
                if(verti[i][j]!=graph[i].first&& verti[i][j]!=graph[i].second){
                    Answer(i,verti[i][j]);
                    visited[i]=1;
                    visited[verti[i][j]]=1;
                }
            }
        }
    }
}

Compilation message

chameleon.cpp: In function 'int binary(int, int, int, std::vector<int>&)':
chameleon.cpp:36:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(sub.size()==see)
            ~~~~~~~~~~^~~~~
chameleon.cpp: In function 'void Solve(int)':
chameleon.cpp:89:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(check==curr.size()){
                ~~~~~^~~~~~~~~~~~~
chameleon.cpp:95:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(check!=curr.size()){
                   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 30 ms 384 KB Output is correct
4 Correct 29 ms 384 KB Output is correct
5 Correct 33 ms 384 KB Output is correct
6 Correct 29 ms 384 KB Output is correct
7 Correct 29 ms 384 KB Output is correct
8 Correct 32 ms 504 KB Output is correct
9 Correct 30 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 432 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 432 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 392 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 6 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 66 ms 480 KB Output is correct
4 Correct 70 ms 504 KB Output is correct
5 Correct 71 ms 504 KB Output is correct
6 Correct 68 ms 504 KB Output is correct
7 Correct 67 ms 428 KB Output is correct
8 Correct 68 ms 504 KB Output is correct
9 Correct 68 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 30 ms 384 KB Output is correct
4 Correct 29 ms 384 KB Output is correct
5 Correct 33 ms 384 KB Output is correct
6 Correct 29 ms 384 KB Output is correct
7 Correct 29 ms 384 KB Output is correct
8 Correct 32 ms 504 KB Output is correct
9 Correct 30 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 432 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 392 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 6 ms 384 KB Output is correct
28 Correct 4 ms 384 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 66 ms 480 KB Output is correct
31 Correct 70 ms 504 KB Output is correct
32 Correct 71 ms 504 KB Output is correct
33 Correct 68 ms 504 KB Output is correct
34 Correct 67 ms 428 KB Output is correct
35 Correct 68 ms 504 KB Output is correct
36 Correct 68 ms 384 KB Output is correct
37 Correct 54 ms 504 KB Output is correct
38 Correct 30 ms 384 KB Output is correct
39 Correct 50 ms 504 KB Output is correct
40 Correct 51 ms 504 KB Output is correct
41 Correct 49 ms 480 KB Output is correct
42 Correct 30 ms 384 KB Output is correct
43 Correct 68 ms 384 KB Output is correct
44 Correct 51 ms 384 KB Output is correct
45 Correct 51 ms 384 KB Output is correct