Wooden Sticks POJ 1065(简单dp)

原题

题目链接

题目分析

题意很明确,就是要维护单调递增的序列,最后看有多少种单调序列即可,设定一个dp数组,cnt表示数组大小,初始化为0,然后把所有木头从小到大排个序,当遍历到木头i时,如果dp数组里有比木头i还小的,就代替它,否则就dp[cnt++]=木头i,最后cnt就是答案.

代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
18
19 struct N
20 {
21     int l,w;
22 }stick[5000];
23 int dp[5000];
24 int cnt;
25
26 bool cmp(N a,N b)
27 {
28     if(a.l==b.l) return a.w<b.w;
29     return a.l<b.l;
30 }
31
32 void add(int i)
33 {
34     for(int j=0;j<cnt;j++)
35     {
36         if(stick[i].w>=dp[j])
37         {
38             dp[j]=stick[i].w;
39             return ;
40         }
41     }
42     dp[cnt++]=stick[i].w;
43 }
44
45 int main()
46 {
47 //    freopen("black.in","r",stdin);
48 //    freopen("black.out","w",stdout);
49     int t;
50     cin>>t;
51     while(t--)
52     {
53         int n;
54         cin>>n;
55         for(int i=0;i<n;i++) cin>>stick[i].l>>stick[i].w;
56         sort(stick,stick+n,cmp);
57         cnt=0;
58         for(int i=0;i<n;i++) add(i);
59         cout<<cnt<<endl;
60     }
61     return 0;
62 }

原文地址:https://www.cnblogs.com/VBEL/p/11408359.html

时间: 2024-08-29 20:24:21

Wooden Sticks POJ 1065(简单dp)的相关文章

挑战程序设计竞赛2.3:Wooden Sticks POJ - 1065

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

Wooden Sticks POJ - 1065

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

poj -1065 Wooden Sticks (贪心or dp)

http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都大于前一根木棍,那么这根木棍不需要生产时间,问你最少的生产时间是多少? 首先可以贪心,先按长度 l排序,如果l相同,按宽度w排序. 从i=0开始,每次把接下来的i+1  -  n-1 的没有标记并且长度和宽度大于等于i这根木棍的长度和宽度标记起来. 1 #include<cstdio> 2 #in

Milking Time (poj 3616 简单DP)

Language: Default Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5290   Accepted: 2183 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule he

POJ 1065 Wooden Sticks Greed,DP

排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath>

poj 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你n个木块,分别给出其长度和重量,然后要对这些木块进行加工,如果木块1的长度和重量都不大于木块2, 那么这两个木块可以放在一起加工,从而只用花费1个时间单位.问要如何进行加工从而能够花费最少时间单位. 知识点: 偏序集:若一个集合A为偏序集,那么满足:1.任意一个元素X属于集合,那么这个元素X≤X 2

POJ 1065 Wooden Sticks

POJ 1065 Wooden Sticks n根木材长l_i重w_i,前一根木材大于后一根的话要浪费一分钟准备机器,求最省方案? 我们把问题抽象出来,那就是:把一个数列划分成最少的最长不升子序列 Dilworth定理:对于一个偏序集,最少链划分等于最长反链长度. 这个问题是二元组的最少链划分,那么我们以a[]为关键字大小进行排序,如果a[]中相同就按照b[]排序,根据 Dilworth定理,然后题目就变成了求b[]序列中最长严格下降子序列长度了. 1 #include <iostream>

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 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 woodworkin

poj 2193 Lenny&#39;s Lucky Lotto Lists 简单dp

//poj 2193 //sep9 #include <iostream> using namespace std; typedef __int64 INT; INT dp[16][2048]; int n,m; int main() { int cases,t=0; scanf("%d",&cases); while(cases--){ scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp));