POJ1065 Wooden Sticks ACM解题报告(暴力贪心)

这题就是那个3636的兄弟啊,这题数据量还小,暴力直接16MS,不需要二分了可怜(ps.每次我二分都要思考半天)

这题贪心的方法与3636不同,是两个量都升序。因为这题只要大于等于当前的即可不+1;

这题主要是给出贪心的小证明吧。升序排列,第一个木棍为一组,然后和他第一个参数相等木棍都与他一组(因为升序排列而且可以取=)

如果你说这个不是最优解,可以将第一个参数相等的所有木棍看成集合p1,p2,。。。。px;

如果p1中的第一个木棍为第一组,其他的木棍为第二组,p2中的第一个如果不满足大于等于第一组,那么又要开一组,这样明显不是最优解(因为第一组和第二组可以合并,这只是简单的思考证明,不必像数学系那么严谨,毕竟是贪心啊)。代码简直和3636一模一样。

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<stack>
using namespace std;
#define MAX 105
typedef long long LL;
const double pi=3.141592653589793;
const int INF=1e9;
const double inf=1e20;
struct doll
{
    int w,h;
} p[20005],team[20005];
bool cmp(struct doll a,struct doll b)
{
    if(a.w!=b.w) return a.w<b.w;
    else return a.h<b.h;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int m;
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&p[i].w,&p[i].h);
        }
        sort(p,p+m,cmp);
        int count=0;
        for(int i=0; i<m; i++)
        {
            int flag=0;
            for(int j=0;j<count;j++)
            {
                if(team[j].w<=p[i].w&&team[j].h<=p[i].h)
                {
                    flag=1;
                    team[j]=p[i];
                    break;
                }
            }
            if(!flag) team[count++]=p[i];
        }
        printf("%d\n",count);
    }
    return 0;
}

你可能感兴趣的:(ACM,poj)