您的位置:首页 > 其它

【贪心】poj3636 nested dolls

2012-05-27 22:50 302 查看
好水啊好水啊。。。

不过输入输出有点恶心,cout和cin就超时,scanf和printf就AC了。。。

#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct Node{
int w,h;
};

int cmp(const void* elem1,const void*elem2){
Node *p,*q;
p=(Node *)elem1; q=(Node *)elem2;
if((p->h)!=(q->h))
return (p->h)-(q->h);
else return (q->w)-(p->w);
}

Node node[20005];
bool selected[20005];

int main(){
int t,n,counter,maximum;
scanf("%d",&t);
//cin>>t;
while(t--){
//cin>>n;
scanf("%d",&n);
counter=0;
for(int i=0;i<n;i++)
scanf("%d%d",&node[i].w,&node[i].h);
//cin>>node[i].w>>node[i].h;
for(int i=0;i<n;i++)
selected[i]=false;
qsort(node,n,sizeof(Node),cmp);
for(int i=0;i<n;i++){
if(selected[i]==true)
continue;
counter++;
maximum=node[i].w;
selected[i]=true;
for(int j=i+1;j<n;j++){
if(selected[j]==false&&node[j].w>maximum){
selected[j]=true;
maximum=node[j].w;
}
}//end for loop
}
printf("%d\n",counter);
//cout<<counter<<endl;
}
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: