zoukankan      html  css  js  c++  java
  • poj 1083 Moving Tables

    题目

    两种做法,开始用贪心做的,有种情况没考虑到,结果排序错了。

    这个例子,感觉上有三个交点,以为是30,其实是20.

    贪心代码:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    struct node{
        int x,y;
    };
    node no[250];
    int flag[250];
    int cmp(node a,node b)
    {
        if(a.x<b.x)
            return 1;
        else if(a.x==b.x)
            return a.y<b.y;
        else return 0;
    }
    int main()
    {
        int n,a,b;
        //freopen("input.txt","r",stdin);
       int T;cin>>T;
       while(T--)
       {
           cin>>n;
           for(int i=0;i<n;i++)
            {
                cin>>a>>b;
                if(a%2==1) a++;
                if(b%2==1) b++;
                if(a>b){
                    int t = a;
                    a = b;
                    b  = t;
                }
                no[i].x=a,no[i].y=b;
            }
            sort(no,no+n,cmp);
           memset(flag,0,sizeof(flag));
            int cnt = 0;
            for(int i=0;i<n;i++)
            {
                if(flag[i]==0){
                    cnt++,flag[i]=1;
                    for(int j=i+1;j<n;j++)
                        if(no[j].x>no[i].y&&flag[j]==0)
                        {
                            flag[j]=1;
                            no[i].y=no[j].y;
                        }
                }
            }
            printf("%d
    ",cnt*10);
       }
        return 0;
    }
    

    法二:

    using namespace std;
    int main()
    {
    	int s[410],n,a,b;
    	int T;cin>>T;
    	while(T--)
    	{
    	    memset(s,0,sizeof(s));
    		cin>>n;
    		for(int i=0;i<n;i++)
    		{
    			cin>>a>>b;
    			if(a>b) {
    				int t=a;
    					a = b;
    					b = t;
    			}
    			if(a%2==1) a++;
    			if(b%2==1) b++;
    			for(int j=a;j<=b;j++)
    				s[j]++;
    		}
    		sort(s,s+401);
    		printf("%d
    ",s[400]*10);
    	}
        return 0;
    }
  • 相关阅读:
    借用构造函数实现继承
    原型链
    创建对象 之 组合使用构造函数模式和原型模式
    6.原型对象的问题
    Spring MVC
    AOP
    谈谈对Spring IOC的理解
    Mybatis3.x与Spring4.x整合(转)
    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
    Appweb写法
  • 原文地址:https://www.cnblogs.com/qie-wei/p/10160149.html
Copyright © 2011-2022 走看看