hdoj1051Wooden Sticks



/*这道题目是先要排序的,按照长度或者重量排都可以,

当长度(重量)相同时就按照重量(长度)排,

从大到小或从小到大都可以!这里我懂的,没有问题!

排序之后,问题就可以简化,(假设按照长度不等时长度排,

长度等是按照重量排,我假设按照从大到小来排!)即求排序

后的所有的重量值最少能表示成几个集合。长度就不用再管了,

从数组第一个数开始遍历,只要重量值满足条件,那么这两个

木棍就满足条件!*/

<span style="font-size:18px;">#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define maxn 5010
struct node{
 int l,w;
}stk[maxn];
bool cmp(node y,node x){
 if(x.l<y.l) return true;
 if(x.l==y.l&&x.w<y.w) return true;
 return false;
}
int main()
{
 int visit[5010],t,n,sum;
 scanf("%d",&t);
 while(t--)
 {
  memset(visit,0,sizeof(visit));
        memset(&stk,0,sizeof(&stk));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        scanf("%d %d",&stk[i].l,&stk[i].w);
        sort(stk,stk+n,cmp);
        sum=0;
        for(int i=0;i<n;i++)
        {
            if(!visit[i])
            {
                for(int j=i;j<n;j++)
                if(stk[j].l>=stk[i].l&&stk[j].w>=stk[i].w&&!visit[j])
                visit[j]=1,stk[i].l=stk[j].l,stk[i].w=stk[j].w;
                sum++; //这个循环是重点。
            }
        }
        printf("%d\n",sum);
 }
    return 0;
}   </span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-10 07:06:29

hdoj1051Wooden Sticks的相关文章

HDOJ1051-Wooden Sticks(贪心)

Problem Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

hdu1455 Sticks 深搜 强剪枝

Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6035    Accepted Submission(s): 1704 Problem Description George took sticks of the same length and cut them randomly until all parts becam

POJ-2513 Colored Sticks 【欧拉通路+Trie】

Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color? Input Input is a

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与

poj 1011 Sticks ,剪枝神题

木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿来一组等长的木棒.将它们随机地砍断.使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你设计一个程序,帮助乔治计算木棒的可能最小长度.每一节木棍的长度都用大于零的整数表示. Input 输入包括多组数据,每组数据包括两

Pick-up sticks(判断两直线相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11335   Accepted: 4250 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting