zoukankan      html  css  js  c++  java
  • HDU-2037(贪心)

    作为球迷,一定想看尽量多的完整的比赛,

    题目:https://vjudge.net/contest/317575#problem/B

    分析:

    看电视节目时电视节目开始得越晚结束的越早越好!这样才能实现 目标是能看尽量多的完整节目!

    故:

    //目标是能看尽量多的完整节目.
    //对节目按照结束时间从小到大排序,如果结束的时间相同,则按照开始的时间从大到小的排序!
    //对节目按照结束时间从小到大排序,如果结束的时间相同,则按照开始的时间从大到小的排序!
    bool cmp(node u,node v)
    {
    	//开始的晚,结束的早的放在最前面! 
    	//降序 因为如果结束时间相同的话,开始的越迟,看节目的时间越短,你就能尽可能的多看电视!
    	if(u.t2==v.t2)	return u.t1>v.t1;
    	return u.t2<v.t2;	
    }
    

    我的代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    struct node
    {
    	int t1;
    	int t2;
    }a[105];
    
    int n;
    bool cmp(node u,node v)
    {
    	return (u.t2==v.t2 && u.t1>v.t1)||(u.t2<v.t2);
    }
    
    int main()
    {
    	int n;
    	while(~scanf("%d",&n) && n)
    	{
    		int i;
    		for(i=0;i<n;i++) scanf("%d%d",&a[i].t1,&a[i].t2);
    		sort(a,a+n,cmp);
    		
    		int ans=1;			//ans的初值置为1哦。可不是0丫! 
    		int t=a[0].t2;
    		for(i=1;i<n;i++)
    		{
    			if(a[i].t1>=t)	//avoid 覆盖交集。 
    			{
    				t=a[i].t2;
    				ans++;
    			}
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      Reference:

    https://blog.csdn.net/dxx_111/article/details/47210517

  • 相关阅读:
    HDU1443_Joseph_约瑟环
    HDU1568_求fibonacci的前四位
    HDU3368_翻转棋
    HDU1134_catalan_大数运算
    HDU1032_The 3n+1_数学题
    HDU2674_N!模2009
    HDU2067_小兔的棋盘_catalan_递推
    文件读写操作inputStream转为byte[] , 将InputStream写入本地文件
    JVM堆内存调优
    Java使用 POI 操作Excel
  • 原文地址:https://www.cnblogs.com/dragondragon/p/11363360.html
Copyright © 2011-2022 走看看