贪心FB

mac2022-06-30  37

hdu 2037  今年暑假不AC

思路: 定义结构体,排序,然后贪心。

(如需输出最多可看的节目是哪些,可以使用标记数组来记录)

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct T{ int s,e; }; T program[105]; bool cmp(const T a,const T b) { if(a.e != b.e) return a.e < b.e; } bool Selected[105]; int main() { int n; while(scanf("%d",&n)!=EOF && n) { for(int i = 0; i < n; i++) { scanf("%d%d",&program[i].s,&program[i].e); } sort(program,program+n,cmp); int j = 0, cnt = 1; for(int i = 1; i < n; i++) { if(program[i].s >= program[j].e) { cnt++; j = i; } } printf("%d\n",cnt); memset(program,0,sizeof(program)); } return 0; }

 

转载于:https://www.cnblogs.com/Pos-Proteus/p/4713601.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)