Problem X: "Greedy" closed interval problem time limit: 1 Sec memory limit: up to MB
Submitted: Resolution: 9
Submitted State [Discussion Version] The title describes the method of rescuing Li Xulin from the "space-time Trap" by Zhang Qiman, who came back through the magic clock and other students at the Academy of Witchcraft and Wizardry. They set up n monitoring points for historical timelines, each monitoring point can monitor a period of time in history, and we can simply be considered as n-closed intervals on the x-axis. However, some monitoring point monitoring time periods are overlapping, which interferes with the accuracy of monitoring. Try to remove as few closed intervals as possible so that the remaining closed intervals do not intersect. Enter the number n (1≤n≤40000) of the closed interval of the first behavior, followed by the 2 endpoints of the closed interval of n behavior. The output output removes as few as possible the number of closed intervals. Sample input
310 2015 1020 15
Sample output
2
idea: because it is closed interval, more than the previous question increase does not intersect can.
Code:
#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;structnode{intbegin; intend;}; Node tt[ the];intCMP (node A,node b) {returna.end<b.end| | a.end==b.end&&a.begin<B.begin;}intMain () {intN; intT; ints=0; intK//records the last recorded program. //while (scanf ("%d", &n)!=eof&&n) {scanf"%d",&N); for(intI=0; i<n;i++) {scanf ("%d%d",&tt[i].begin,&tt[i].end); if(tt[i].begin>tt[i].end) {T=Tt[i].begin; Tt[i].begin=Tt[i].end; Tt[i].end=T; }} sort (Tt,tt+n,cmp); S+=1; K=0; for(intI=1; i<n;i++){ if(tt[i].begin>tt[k].end) {s++; K=i; }} printf ("%d\n", N-s); S=0; //} return 0;}
The problem of "greedy" closed interval