POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)
描述
http://poj.org/problem?id=1065
木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次).
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21277 | Accepted: 9030 |
Description
(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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) ,
and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since
there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1
, 2 ) , ( 2 , 5 ) .
Input
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 2n 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
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
Source
分析
神原理...
要求最少停多少次,就是要求单调不降的子序列的个数 x 最多为多少(每次停完都是一个单调不降的子序列),问题转化为求 x 的最小值.
我们现将木棍按照其中一种属性升序(不降)排序,这时另一种属性的最长下降子序列的长度记为 L .可以证明 x >=L.(鸽笼原理).
详细题解:
http://www.hankcs.com/program/cpp/poj-1065-wooden-sticks.html
注意:
1.二分的边界.在找满足 a [ k ] <= -1 的 k 的最小值时,可能 dp 数组中已经没有 -1 了,也就是 n 个位置全部被占满了,也就是整个序列就是一个下降序列,此时会找到 n 的位置,再 -1 答案就错误了,所以开始的时候将 1 ~ n + 1 都赋为 -1 ,之后 dp 时查找在 1 ~ n 查找,因为 dp 结束之前最多是 n - 1 个,不会把 dp 数组填满,数组中一定还有 -1 ,就一定存在满足 a [ k ] <= v (v>0) 的 k ,计算总长度时在 1~n+1 查找,确保有满足 a [ k ] <= -1 的 k .
#include<cstdio>
#include<algorithm>
#define read(a) a=getnum()
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
using namespace std; const int maxn=;
struct node {int l,w;}wood[maxn];
int q,n;
int dp[maxn]; inline int getnum(){ int r=,k=;char c;for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;for(;c>=''&&c<='';c=getchar()) r=r*+c-''; return r*k; } bool comp(node x,node y) { return x.l<y.l; } int bsearch(int l,int r,int v)
{
while(l<r)
{
int m=l+(r-l)/;
if(dp[m]<=v) r=m;
else l=m+;
}
return l;
} void solve()
{
sort(wood+,wood+n+,comp);
for1(i,,n+) dp[i]=-;
for1(i,,n)
{
int idx=bsearch(,n,wood[i].w);
dp[idx]=wood[i].w;
}
int ans=bsearch(,n+,-)-;
printf("%d\n",ans);
} void init()
{
read(q);
while(q--)
{
read(n);
for1(i,,n) { read(wood[i].l); read(wood[i].w); }
solve(); }
} int main()
{
#ifndef ONLINE_JUDGE
freopen("wood.in","r",stdin);
freopen("wood.out","w",stdout);
#endif
init();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("wood.out");
#endif
return ;
}
POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)的更多相关文章
- HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场
题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...
- Gym 100851G Generators (vector+鸽笼原理)
Problem G. Generators Input file: generators.in Output file: generators.outLittle Roman is studying li ...
- 非 动态规划---LIS
题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度.(见动态规划---LIS) /* 题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度 ...
- poj 3370 鸽笼原理知识小结
中学就听说过抽屉原理,可惜一直没机会见识,现在这题有鸽笼原理的结论,但其实知不知道鸽笼原理都可以做 先总结一下鸽笼原理: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两 ...
- poj 2356鸽笼原理水题
关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...
- UVA 10620 - A Flea on a Chessboard(鸽笼原理)
UVA 10620 - A Flea on a Chessboard 题目链接 题意:给定一个跳蚤位置和移动方向.如今在一个国际象棋棋盘上,左下角为黑格,一个格子为s*s,推断是否能移动到白格子.问要 ...
- CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)
题目链接 Points Inside A Polygon 题意 给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...
- 1393 0和1相等串 鸽笼原理 || 化简dp公式
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...
- Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)
To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...
随机推荐
- 【转】【SQL SERVER】怎样处理作业中的远程服务器错误(42000)
(SQL SERVER)怎样处理作业中的远程服务器错误(42000) 问: 1.我创建了一个链接服务器. 2.在两台服务器之间创建了新的SQL用户. 3.编写了访问链接服务器的SQL语句,执行成功. ...
- Linux中tshark(wireshark)抓包工具使用方法详解
在Linux下,当我们需要抓取网络数据包分析时,通常是使用tcpdump抓取网络raw数据包存到一个文件,然后下载到本地使用wireshark界面网络分析工具进行网络包分析.最近才发现,原来wires ...
- jdbc-connect-oracle12c-pdb/cdb(jdbc连接oracle12c的pdb和cdb)
1 本文简介: 通过特意引发问题,聚焦问题,解决问题,并循序渐进 最后总结jdbc连接oracle12c中cdb和pdb的条件. 软件环境:Redhat7.1+orcacle12c 2 ...
- Objective-c单例模式详解
转载自:http://www.jianshu.com/p/85618bcd4fee 单例模式出现以后,关于它的争执就一直存在.在开发项目中,有很多时候我们需要一个全局的对象,而且要保证全局有且仅有一份 ...
- Dao模型设计(基于Dao与Hebernate框架)
以前没有Dao设计模型之前,一般都是这样的流程: ①先设计实体对象 学生对象: package com.itheima.domain; import java.io.Serializable; imp ...
- (hdu)1160 FatMouse's Speed
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- 【BZOJ3529】【莫比乌斯反演 + 树状数组】[Sdoi2014]数表
Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于 ...
- 代码方式删除SVN
public static void delect(File s) { File b[] = null; if (s.exists()) {// 判读是否存在 if (s.isDirectory()) ...
- IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一
// // WPSuggest.h // OC-API-网络访问 // // Created by wangtouwang on 15/3/24. // Copyright (c) 2015年 wan ...
- centos 6.5 安装jenkins
Installation sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...