<문제>

 

1트

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> cards){
    int answer = 1;
    vector<bool>open(cards.size()+1,0);
    int cnt =0;
    for(auto x : cards){
        int s = x;
        int scr = 0;
        while(!open[s]){
            open[s] = 1;
            s = cards[s-1];
            scr++;
        }
        cnt++;
        scr == cards.size()? answer = 0 : answer *= scr;
        if(cnt == 2) break;
    }
    return answer;
}

이건 그냥 제일 큰거 두개 찾는게 아니라

그냥 첫상자, 그리고 안열린 애들중에 바로 그 다음상자만 가지고 하는거라

게임 취지랑 맞지 않음.

 

그거 보완해서

2트

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> cards){
    vector<bool>open(cards.size()+1,0);
    vector<int>v;
    int cnt =0;
    for(auto x : cards){
        int s = x;
        int scr = 0;
        while(!open[s]){
            open[s] = 1;
            s = cards[s-1];
            scr++;
        }
        v.push_back(scr);
    }
    partial_sort(v.begin(), v.begin()+2, v.end(), greater());
    if(v[0]==cards.size()) return 0;
    else return v[0]*v[1];
}

 

 우선 안열린 상자가 없을 때까지 싹 다 돌아본 다음에

vector의 가장 큰 두개만 가지고 출력!

+ Recent posts