[문제보기]

 8888. 시험

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

[풀이과정]

필자는 n, t가 1부터 시작하기 때문에 2차원 배열을 가지고 0행에는 각 문제 점수를 0열에는 각 참가자가 맞힌 수를

저장해주었다.

예를 들어

info[0][1]은 1번 문제의 점수, info[0][2]는 2번 문제의 점수, ... , info[0][t]는 t번 문제의 점수가 되고

info[1][0]은 1번 참가자가 문제를 맞힌 수, info[2][0]은 2번 참가자가 문제를 맞힌 수, ... , info[n][0]은 n번 참가자가

문제를 맞힌 수가 된다.

이렇게 각 정보를 info[][] 배열에 저장을 한 후 pair<pair<int,int>, int> person[]에 정보를 넣어주는데

person[].first.first에는 점수, person[].first.second에는 맞힌 수, person[].second에는 참가자번호를 넣어주고

sort()함수를 이용하여 정렬 후 p에 해당하는 정보를 출력해준다.

[소스코드]

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n, t, p;
int info[2001][2001];
pair<pair<int, int>, int> person[2001];

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b){
	if(a.first.first == b.first.first){
		if(a.first.second == b.first.second){
			return a.second < b.second;
		}
		return a.first.second > b.first.second;
	}
	return a.first.first > b.first.first;
}

int main(){
	cin.tie(0);
	cout.sync_with_stdio(false);

	int T;
	cin>>T;
	for(int test_case = 1; test_case <= T; ++test_case){
		memset(info, 0, sizeof(info));
		memset(person, 0, sizeof(person));
		int ans = 0;
		cin >> n >> t >> p;


		for(int i=1; i<=n; i++){
			for(int j=1; j<=t; j++){
				cin >> info[i][j];
				if(info[i][j] == 1){
					info[i][0]++;
				}
				else{
					info[0][j]++;
				}
			}
		}
		
		for(int i=1; i<=n; i++){
			for(int j=1; j<=t; j++){
				if(info[i][j] == 1){
					person[i].first.first += info[0][j];
				}
			}
			person[i].first.second = info[i][0];
			person[i].second = i;
		}
        
		sort(person+1, person+n+1, cmp);

		for(int i=1; i<=n; i++){
			if(person[i].second == p){
				cout << "#" << test_case << " " << person[i].first.first << " " << i << "\n";
				break;
			}
		}

		
	}
	return 0;
}

 

[해결과정 중 실수한 부분]

마지막 for문에서 ans 변수를 사용하여 p에 해당하는 인덱스를 뽑고 break;를 한 다음 출력을 했는데 시간초과가 났다.

하지만 for문안에 출력하는 코드를 넣어 p를 찾자마자 바로 출력을 하고 break를 해주니 'pass'가 떴다.