Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26095    Accepted Submission(s): 10554

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 prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:

(a) The setup time for the first wooden stick is 1 minute. 
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.

You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

 
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.
 
Output
The output should contain the minimum setup time in minutes, one per line.
 
Sample Input
3
5
4 9 5 2 2 1 3 5 1 4
3
2 2 1 1 2 2
3
1 3 2 2 3 1
 
Sample Output
2
1
3

有若干组木棍:木棍的长度为I 重为W   现在用机器加工这些木头: 规则如下:

1;加工第一组木头会花去1分钟
2:设加工的下一组木头的长度为 I'  重量为W‘   如果   I=<I' 且 W=<W'  ,则加工这根木头不需要花费时间。

问,所花费的最少时间是多少?

可以先按长度和重量从小到大排序,长度相同按重量从小到大排

每次从最前面没选过的开始,计算有多少种递增子序列,加一点技巧会更清楚,具体看代码中间的while部分

 #include<bits/stdc++.h>
using namespace std;
struct node
{
int l,w,flag;
}a[];
void init()
{
for(int i=;i<;i++)
{
a[i].l=;a[i].w=;a[i].flag=;
}
}
bool cmp(node x,node y)
{
if(x.l==y.l)return x.w<y.w;
else return x.l<y.l;
}
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
init();
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d %d",&a[i].l,&a[i].w);
}
sort(a,a+n,cmp);
// for(int i=0;i<n;i++)
// {
// printf("%d %d\n",a[i].l,a[i].w);
// }
int ans=,j=;
while(j<n)//判断有没有全部选完 ,核心部分
{
ans++;
int tempx=,tempy=;
for(int i=;i<n;i++)
{
if(a[i].l>=tempx&&a[i].w>=tempy&&a[i].flag)//flag==1表示没有被选过
{
a[i].flag=;//选过的标记,下次就不选了
j++;
tempx=a[i].l;
tempy=a[i].w;
}
}
}
printf("%d\n",ans);
}
}
return ;
}

hdu1051 Wooden Sticks(贪心+排序,逻辑)的更多相关文章

  1. 1270: Wooden Sticks [贪心]

    点击打开链接 1270: Wooden Sticks [贪心] 时间限制: 1 Sec 内存限制: 128 MB 提交: 31 解决: 11 统计 题目描述 Lialosiu要制作木棍,给n根作为原料 ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDU1051 Wooden Sticks 【贪婪】

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. Hdu1051 Wooden Sticks 2017-03-11 23:30 62人阅读 评论(0) 收藏

    Wooden Sticks Problem Description There is a pile of n wooden sticks. The length and weight of each ...

  5. HDOJ.1051 Wooden Sticks (贪心)

    Wooden Sticks 点我挑战题目 题意分析 给出T组数据,每组数据有n对数,分别代表每个木棍的长度l和重量w.第一个木棍加工需要1min的准备准备时间,对于刚刚经加工过的木棍,如果接下来的木棍 ...

  6. HDU 1051 Wooden Sticks (贪心)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. uvalive 2322 Wooden Sticks(贪心)

    题目连接:2322 Wooden Sticks 题目大意:给出要求切的n个小木棍 , 每个小木棍有长度和重量,因为当要切的长度和重量分别大于前面一个的长度和重量的时候可以不用调整大木棍直接切割, 否则 ...

  8. HDU - 1051 Wooden Sticks 贪心 动态规划

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)    ...

  9. POJ-1065 Wooden Sticks,排序+最长单减子序列!

                                                       Wooden Sticks 题意:有一台机器处理木材,最开始需要一分钟准备,如果后面处理的木材比前 ...

随机推荐

  1. PHP------数组的遍历

    echo current($attr); //取当前元素的value值 echo key($attr); //取当前元素的key next($attr); //将数组里面的指针指向下一个(向下移)pr ...

  2. SQL 关联两个表的视图总结

    视图就是一条select查询语句,是一张虚拟表. table a  , table b  以表a基表(a LEFT  JOIN b) 1.1 当update view时 更新view中表b字段并且表b ...

  3. @RequestParam和@PathVariable的区别及其应用场景

    @RequestParam和@PathVariable这两者之间区别不大,主要是请求的URL不一样 用@RequestParam请求接口时,URL是:http://www.test.com/user/ ...

  4. h5做的app和原生app的区别

    之所以说h5做的app和原生app的区别,是因为一位博友的问题: 随着 h5 的普及,是不是不再需要开发 app ? 我的回答是要分业务需求,分场合而定. 比如现在的微信小程序这么流行,甚至也取代了不 ...

  5. event对象,ie8及其以下

    event 对象是 JavaScript 中一个非常重要的对象,用来表示当前事件.event 对象的属性和方法包含了当前事件的状态.当前事件,是指正在发生的事件:状态,是与事件有关的性质,如引发事件的 ...

  6. 删除oracle实例

    1.在开始菜单中,点击ORAHOME目录下的"Configuration and Migration Tools"下的"Database Configuration As ...

  7. IOC-AutoFac

    学习过程中参考博客: AutoFac文档:http://www.cnblogs.com/wolegequ/archive/2012/06/09/2543487.html AutoFac使用方法总结:P ...

  8. Cocopods Search失败的坑

    最近看了下如何使用cocopods来制作自己的公有库,然后果断的按照教程做了一遍,然后提交审核.完成之后意外的发现使用pod search xxx的时候报了一大堆的日志出来,pod的其他功能可以使用, ...

  9. mybatis传单个参数,和<if>标签同时使用的问题

    // Mapper.java EmerEvent selectByAlarmId(Integer alarmId); // Mapper.xml <select id="selectB ...

  10. C++笔记011:C++对C的扩展——变量检测增强

    原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 在C语言中重复定义多个同名的变量是合法的,多个同名的全局变量最终会被链接到全局数据区的同一个地址空间上. 在C++中,不允许定义多个同名的 ...