Wooden Sticks

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

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
 
好吧,这是一道简单题,,,仔细点就好;
思路是;把木棍按照长度优先从小到大排好,当长度一定是按宽度从小到大排好,然后比较宽度就ok了
,具体见代码,,,,
 
简单易懂型:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int l;
int w;
int k;
}a[]; int main()
{
int T,n,j,i,temp,sum,p;
scanf("%d",&T);
while(T--)
{
sum=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].w);
a[i].k=;//标记
}
for(i=;i<=n;i++)//选择 why!!!
{
p=i;
for(j=i+;j<=n;j++)
if(a[p].l>a[j].l||a[p].l==a[j].l&&a[p].w>a[j].w) //长度优先排,然后是宽度,都排好,从小到大
p=j;
if(p!=i)
{
int t;
t=a[i].l;
a[i].l=a[p].l;
a[p].l=t;
t=a[i].w;
a[i].w=a[p].w;
a[p].w=t;
}
}
// for(i=1;i<=n;i++)
// printf("%d %d %d \n",a[i].l,a[i].w,a[i].k);
for(i=;i<=n;i++)
{
if(a[i].k==)
{
temp=a[i].w;
for(j=i+;j<=n;j++)
{
if(a[j].w>=temp&&a[j].k==)
{
a[j].k=;
temp=a[j].w;
sum++;
} }
}
}
printf("%d\n",n-sum);
}
return ;
} /*
4
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
7
1 12 1 5 2 6 2 8 2 4 4 3 3 7 */

然后是更加简洁的排序sort,快排。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int l;
int w;
int k;
}a[]; int cmp(node a,node b)
{
if(a.w==b.w)
return a.l<b.l;
return a.w<b.w;
} int main()
{
int T,n,j,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d%d",&a[i].l,&a[i].w);
a[i].k=;//标记
}
sort(a,a+n,cmp);
for(i=;i<n;i++)
printf("%d %d %d \n",a[i].l,a[i].w,a[i].k); int num=n;
int count=;
while(num)
{
int tl=;int tw=;
for(i=;i<n;i++)
{
if(a[i].k==&&a[i].w>=tw&&a[i].l>=tl)
{
tl=a[i].l; tw=a[i].w; a[i].k=;num--;
}
}
count++;
}
printf("%d\n",count);
}
return ;
}

稍作修改:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int l;
int w;
int k;
}a[]; int cmp(node a,node b)//从小到大排,,,对于结构体
{
if(a.l==b.l)
return a.w<b.w;
return a.l<b.l;
}
int main()
{
int T,n,j,i,temp,sum;
scanf("%d",&T);
while(T--)
{
sum=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].w);
a[i].k=;//标记
}
sort(a+,a+n+,cmp);
for(i=;i<=n;i++)
printf("%d %d %d \n",a[i].l,a[i].w,a[i].k);
for(i=;i<=n;i++)
{
if(a[i].k==)
{
temp=a[i].w;
for(j=i+; j<=n; j++)
{
if(a[j].w>=temp&&a[j].k==)
{
a[j].k=;
temp=a[j].w;
sum++;
} }
}
}
printf("%d\n",n-sum);
}
return ;
}

是的,冒泡嘛,也是可以的,,,

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int l;
int w;
int k;
}a[]; int cmp(node a,node b)
{
if(a.l==b.l)
return a.w<b.w;
return a.l<b.l;
}
int main()
{
int T,n,j,i,temp,sum;
scanf("%d",&T);
while(T--)
{
sum=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].w);
a[i].k=;//标记
}
for(i=;i<=n;i++)//冒泡
{
for(j=;j<n-i;j++)//j从1开始就错了。。。
if(a[j].l>a[j+].l)
{
int vv;
vv=a[j].w; a[j].w=a[j+].w; a[j+].w=vv;
vv=a[j].l; a[j].l=a[j+].l; a[j+].l=vv; }
} for(i=;i<=n;i++)
printf("%d %d %d \n",a[i].l,a[i].w,a[i].k);
for(i=;i<=n;i++)
{
if(a[i].k==)
{
temp=a[i].w;
for(j=i+; j<=n; j++)
{
if(a[j].w>=temp&&a[j].k==)
{
a[j].k=;
temp=a[j].w;
sum++;
} }
}
}
printf("%d\n",n-sum);
}
return ;
}

推荐第二种,,,,本题还可以用dp做,具体代码,有待有序更新。。。o(∩_∩)o 哈哈

Wooden Sticks(hdu1501)(sort,dp)的更多相关文章

  1. HDU 1087 Super Jumping! Jumping! Jumping!(最长上升子序列,dp)

    以下引用自:http://www.cnblogs.com/Lyush/archive/2011/08/31/2161314.html沐阳 该题可以算是一道经典的DP题了,题中数据是这样的.以 3 1 ...

  2. Pie(求最小身高差,dp)

    Pie Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. Matrix Swapping II(求矩阵最大面积,dp)

    Matrix Swapping II Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. HDU 6249 Alice’s Stamps(2017 CCPC-Final G题,DP)

    题目链接 HDU 6249 题意 给定$m$个区间,在这些区间中选出不超过$k$个,求被覆盖的点的数量的最大值. 设$f[i][j]$表示选到第$i$个点并选了$j$个区间的时候能得到的最大答案. 处 ...

  5. hdu 1159 Common Subsequence(最长公共子序列,DP)

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

  6. 覆盖问题:最大覆盖问题(Maximum Covering Location Problem,MCLP)和集覆盖问题(Location Set Covering Problem,LSCP)

    集覆盖问题研究满足覆盖所有需求点顾客的前提下,服务站总的建站个数或建 设费用最小的问题.集覆盖问题最早是由 Roth和 Toregas等提出的,用于解决消防中心和救护车等的应急服务设施的选址问题,他们 ...

  7. 敬请贤者:WEB、IOS开发(2年以上经验,大专);CTO、产品经理,运营专员 电商服装鞋饰买手(2年以上经验,服装或鞋类);体验店店长 (2年以上经验,服装或鞋类) 工作地点:丰台南苑路;有意者小窗QQ2211788980 - V2EX

    敬请贤者:WEB.IOS开发(2年以上经验,大专):CTO.产品经理,运营专员 电商服装鞋饰买手(2年以上经验,服装或鞋类):体验店店长 (2年以上经验,服装或鞋类) 工作地点:丰台南苑路:有意者小窗 ...

  8. H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)

    H5打包的app实现支付及支付宝支付,本章主要详解支付宝支付,微信支付请查看另一篇“H5使用codovar插件实现微信支付(微信APP支付模式,前端)” ps:本文只试用H5开发的,支付宝 APP支付 ...

  9. H5使用codovar插件实现微信支付(微信APP支付模式,前端)

    H5打包的app实现微信支付及支付宝支付,本章主要详解微信支付,支付宝支付请查看另一篇“H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)” ps:本文只试用H5开发的,微信 AP ...

随机推荐

  1. jQuery小案例

    Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> <head> <script ty ...

  2. Linux系统VIM编辑器管理(2)

    VI/VIM模式概述 在 Linux 的世界中,绝大部分的配置文件都是以 ASCII 的纯文本形态存在,因此利用简单的文字编辑软件就能够修改设定了,与微软的 Windows 系统不同的是,如果你用惯了 ...

  3. 05_python_字典

    一.字典定义 字典是python中唯一的映射类型,以{ }括起来的键值对组成,在dict中key是唯一的.在保存时,根据key来计算一个内存地址,然后把key-value保存至地址中.这种算法是has ...

  4. angularJS详解

    这篇文章转载  kooky798 的博客,http://blog.csdn.net/yy374864125/article/details/41349417, 写到这么详细也是没谁了,必须粉一个 1 ...

  5. CSV Data Set Config设置

    Jmeter参数化常用的两种方法: 1.使用函数助手 2.CSV Data Set Config 本章主要讲解CSV Data Set Config设置 1.Filename:文件名,指保存信息的文件 ...

  6. CSP攻略

    看完三篇文章应该就懂了csp是干嘛的. https://www.cnblogs.com/Wayou/p/intro_to_content_security_policy.html https://ww ...

  7. [Spring]IOC控制反转和DI依赖注入

    从之前算起到现在接触Spring也已经有几天了,进度也不是很快,就只弄懂了控制反转和依赖注入那么一点东西.然后敲了两个demo 主要是因为之前没有学过,然后网上资源很多但是都不是面向我们初学者的,大多 ...

  8. 3DMax——室内设计:墙体+吊顶

    1.导入CAD平面图 2.将导入的平面图全部选中→颜色设置为其他颜色→设置为组(设置为组,是为了后期选材质方便) 3.选中图形,选择移动工具,输入坐标为0,右键选择冻结当前选择 4.右键“角度捕捉切换 ...

  9. odoo开发笔记--模型中常用的方法

    create方法在数据表中插入一条记录(或新建一个对象的resource)格式:def create(self,cr,uid,vals,context={})参数:vals:待新建记录的字段值,是一个 ...

  10. 本机spark 消费kafka失败(无法连接)

    本机spark 消费kafka失败(无法连接) 终端也不报错 就特么不消费:  但是用console的consumer  却可以 经过各种改版本 ,测试配置,最后发现 只要注释掉 kafka 配置se ...