Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 해시 구현
- qorwns
- C/C++ 구현
- heap
- Stack 이란
- 시간 복잡도
- AVL 시간 복잡도
- 자료구조
- 백준 17471
- 백준 17779
- 백준 2447
- 백준 17822
- 1764
- 게리멘더링2
- ㅣ풀이
- 스택의 특징
- dfs
- c#
- 백준 5397
- 조세퍼스 순열
- 원판 돌리기
- 백준
- 백준 1158
- 버킷 정렬
- 별 찍기 10
- 해시구현
- 5397
- 백준 1406
- 풀이
- 구현
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 7453] 합이 0인 네 정수 본문
요구사항
indexA, indexB, indexC, indexD를 골라서 0이 되게 하는 개수 구하기
풀이
4000*4000*4000*4000는 제한시간 초과가 난다
AB 더한 배열 CD 더한 배열을 구하여
AB 오름차순 index : i
CD 내림차순 index : j
으로 0이 되는지 확인하여 되면 되는게 몇개인지 계산
0보다 클 경우 j++
0보다 작을 경우 i++
아래 코드는 시간초과
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 4040;
int n;
int map[4040][6];
int vab[MAX*MAX];
int vcd[MAX*MAX];
bool com1(int a, int b) {
if (a < b) return true;
return false;
}
bool com2(int a, int b) {
if (a > b) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
cin >> map[i][j];
}
}
//12, 34 따로 저장해줒 배열 완성 시킨
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
vab[cnt] = { map[i][0] + map[j][1] };
vcd[cnt] = { map[i][2] + map[j][3] };
cnt++;
}
}
sort(vab, vab+cnt, com1); // 오름
sort(vcd, vcd+cnt, com2); // 내림
long long ans = 0;
int i = 0, j = 0;
while (i < cnt && j < cnt) {
int minnum = vab[i];
int maxnum = vcd[j];
//cout << "min" << minnum << " " << maxnum << endl;
if (minnum + maxnum == 0) {
//ans++;
//cout << "A" << endl;
int ncnt = 1;
int mcnt = 1;
for (int k = i+1; k < cnt; k++) {
if (minnum == vab[k]) ncnt++;
}
for (int k = j + 1; k < cnt; k++) {
if (maxnum == vcd[k]) mcnt++;
}
ans += (ncnt*mcnt);
i += ncnt;
j += mcnt;
}
else if (minnum + maxnum < 0) {
i++;
}
else if (minnum + maxnum > 0) {
j++;
}
}
printf("%lld\n", ans);
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 5719] 거의 최단 경로 (0) | 2020.03.16 |
---|---|
[백준 1072] 게임 (0) | 2020.03.14 |
[백준 1300] k번째 수 (0) | 2020.03.14 |
[백준 15652]N과 M(4) (0) | 2020.03.12 |
[백준 15651] N과 M(3) (0) | 2020.03.12 |
Comments