728x90

1. ๋ฌธ์ œ ์š”์•ฝ

์—ด์€ ์ปฌ๋Ÿผ, ํ–‰์€ ํŠœํ”Œ
์ฒซ๋ฒˆ์งธ ์ปฌ๋Ÿผ์€ ๊ธฐ๋ณธํ‚ค๋กœ์„œ ๋ชจ๋“  ํŠœํ”Œ์— ๋Œ€ํ•ด ๊ทธ ๊ฐ’์ด ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ๋ณด์žฅ

(์š”์•ฝํ•˜์ž๋ฉด id ๊ฐ™์€ ๊ณ ์œ  ๋ฒˆํ˜ธ๋ผ๋Š” ๊ฒƒ)


1. ํ•ด์‹œ ํ•จ์ˆ˜๋Š” col, row_begin, row_end ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Œ
2. ํ…Œ์ด๋ธ”์˜ ํŠœํ”Œ(ํ–‰)์„ col๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•˜๋˜, 
๋งŒ์•ฝ ๊ทธ ๊ฐ’์ด ๋™์ผํ•˜๋ฉด ๊ธฐ๋ณธํ‚ค์ธ ์ฒซ ๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•จ


์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ์—์„œ S_i๋ฅผ i๋ฒˆ์งธ ํ–‰์˜ ํŠœํ”Œ์— ๋Œ€ํ•ด ๊ฐ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ i๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋“ค์˜ ํ•ฉ์œผ๋กœ ์ •์˜
row_begin <= i <= row_end ์ธ ๋ชจ๋“  S_i๋ฅผ ๋ˆ„์ ํ•˜์—ฌ bitwise XOR ํ•œ ๊ฐ’์„ ํ•ด์‹œ ๊ฐ’์œผ๋กœ์„œ ๋ฐ˜ํ™˜ํ•จ

 

 

2. ์ฝ”๋“œ

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int sortIndex;

bool cmp(vector<int> lValue,vector<int> rValue){
    if(lValue[sortIndex]==rValue[sortIndex]) return lValue[0]>rValue[0];
    return lValue[sortIndex]<rValue[sortIndex];
}

int solution(vector<vector<int>> data, int col, int row_begin, int row_end) {
    int answer = 0;
    sortIndex=col-1;
    
    /*for(int i=0;i<data.size();i++){
        for(int j=0;j<data[i].size();j++){
            printf("%d ",data[i][j]);
        }
        printf("\n");
    }*/
    
    sort(data.begin(),data.end(),cmp);
    
    /*printf("\n\n");
    
    for(int i=0;i<data.size();i++){
        for(int j=0;j<data[i].size();j++){
            printf("%d ",data[i][j]);
        }
        printf("\n");
    }*/
    
    int s2;
    for(int i=row_begin-1;i<row_end;i++){
        s2=0;
        for(int j=0;j<data[i].size();j++){
            s2+=data[i][j]%(i+1);
            //printf("%d : %d %d ~~\n",(i+1),data[i][j],data[i][j]%(i+1));
        }
        //printf("%d !!\n",s2);
        answer= answer^s2;
    }
    
    
    return answer;
}

 

 

 

 

3. ํ•ด์„ค

๋จผ์ €, ํ…Œ์ด๋ธ”์˜ ํŠœํ”Œ์„ col ๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•ฉ๋‹ˆ๋‹ค

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ col ๋ฒˆ์žฌ ์ด๊ธฐ ๋–„๋ฌธ์— vector์˜ col-1์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์•ผํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค

int solution(vector<vector<int>> data, int col, int row_begin, int row_end) {
    int answer = 0;
    sortIndex=col-1;
    ...
}
bool cmp(vector<int> lValue,vector<int> rValue){
    if(lValue[sortIndex]==rValue[sortIndex]) return lValue[0]>rValue[0];
    return lValue[sortIndex]<rValue[sortIndex];
}

 

sortIndex๋ผ๋Š” ๋ณ€์ˆ˜์— col-1์„ ํ• ๋‹นํ•ด์ฃผ๊ณ , algorithm ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ sortํ•จ์ˆ˜์™€ ์ œ๊ฐ€ ์ •์˜ํ•˜๋Š” cmp ๋น„๊ตํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค

๋งŒ์•ฝ ๋น„๊ต ๋ฒกํ„ฐ๋“ค์˜ sortIndex ์œ„์น˜์˜ ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค

 

๊ทธ ํ›„์—๋Š” ^ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ XOR ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค

 

 

๋น„ํŠธ์—ฐ์‚ฐ XOR ์ด๋ž€?

๋ฒ ํƒ€์  OR ์—ฐ์‚ฐ์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š”๋ฐ

๋‘ ๋น„ํŠธ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋งŒ 1์ด ๋˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์ด ๋˜๋Š” ํŠน์ดํ•œ ์—ฐ์‚ฐ์ž

 

์—ฐ์‚ฐ ๊ฒฐ๊ณผ
0 ^ 0 0
0 ^ 1 1
1 ^ 0 1
1 ^ 1 0

 

int s2;
for(int i=row_begin-1;i<row_end;i++){
    s2=0;
    for(int j=0;j<data[i].size();j++){
        s2+=data[i][j]%(i+1);
    }
    answer= answer^s2;
}

 

row_begin-1 ์—์„œ row_end๊นŒ์ง€ ๋น„ํŠธ์—ฐ์‚ฐ์„ ํ•œ ๊ฐ’๋“ค์„ answer์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค

data[i][j]%(i+1)

 

๋Š” ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ์—์„œ S_i๋ฅผ i๋ฒˆ์งธ ํ–‰์˜ ํŠœํ”Œ(ํ–‰)์— ๋Œ€ํ•ด ๊ฐ ์ปฌ๋Ÿฝ์˜ ๊ฐ’์„ i๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€์ž…๋‹ˆ๋‹ค

 

 

 

 

๋ฐ˜์‘ํ˜•

+ Recent posts