HDU--1051:Wooden Sticks (贪心)

来源:互联网 发布:ai cc2017 mac 中文包 编辑:程序博客网 时间:2024/06/05 18:07

1. 题目源地址:http://acm.hdu.edu.cn/showproblem.php?pid=1051

2. 解题代码:

//HOJ--1051:Wooden Sticks#include<iostream>#include<algorithm>#include<memory.h>#define INF 10005using namespace std;struct node{   int length;   int weight;}stick[5005];int cmp(node a,node b){    if(a.length != b.length)       return a.length > b.length;    else        return a.weight > b.weight;}int main(){    int caseNum,N,temp,ans;    int i,j;    int visited[5005];    cin>>caseNum;        while(caseNum--)    {       cin>>N;       for(i=0;i<N;i++)          cin>>stick[i].length>>stick[i].weight;                 sort(stick,stick+N,cmp);       memset(visited,0,sizeof(visited));              ans=0;       for(i=0;i<N;i++)       {          if(visited[i])             continue;                       ans++;            temp=stick[i].weight;          for(j=i+1;j<N;j++)          {             if(!visited[j] && stick[j].weight<=temp)             {                visited[j]=1;                temp=stick[j].weight;             }          }      }       cout<<ans<<endl;   }    return 0;}


0 0
原创粉丝点击