Algorithm/BOJ
7507 올림픽 게임
henry1214
2018. 2. 20. 01:37
https://www.acmicpc.net/problem/7507
그리디(Greedy) 알고리즘으로 해결할 수 있다. 경기가 빨리 끝나는 순으로 하나씩 본다면 최적해가 보장됨을 알 수 있다.
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 | #include <cstdio> #include <algorithm> using namespace std; struct G { int d,s,e; }; G g[50000]; int n,m; bool cmp(G &g1,G &g2) { return g1.d!=g2.d?g1.d<g2.d:g1.e<g2.e; } int main() { scanf("%d",&n); for(int t=1;t<=n;t++) { scanf("%d",&m); for(int i=0;i<m;i++) scanf("%d %d %d",&g[i].d,&g[i].s,&g[i].e); sort(g,g+m,cmp); int cnt=1,p=0; for(int i=1;i<m;i++) if(g[i].d!=g[p].d || g[i].s>=g[p].e) p=i,cnt++; printf("Scenario #%d:\n%d\n\n",t,cnt); } return 0; } | cs |