Wooden Sticks(hdu1501)(sort,dp)
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9541 Accepted Submission(s): 3917
(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).
#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)的更多相关文章
- HDU 1087 Super Jumping! Jumping! Jumping!(最长上升子序列,dp)
以下引用自:http://www.cnblogs.com/Lyush/archive/2011/08/31/2161314.html沐阳 该题可以算是一道经典的DP题了,题中数据是这样的.以 3 1 ...
- Pie(求最小身高差,dp)
Pie Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- Matrix Swapping II(求矩阵最大面积,dp)
Matrix Swapping II Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 6249 Alice’s Stamps(2017 CCPC-Final G题,DP)
题目链接 HDU 6249 题意 给定$m$个区间,在这些区间中选出不超过$k$个,求被覆盖的点的数量的最大值. 设$f[i][j]$表示选到第$i$个点并选了$j$个区间的时候能得到的最大答案. 处 ...
- hdu 1159 Common Subsequence(最长公共子序列,DP)
题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...
- 覆盖问题:最大覆盖问题(Maximum Covering Location Problem,MCLP)和集覆盖问题(Location Set Covering Problem,LSCP)
集覆盖问题研究满足覆盖所有需求点顾客的前提下,服务站总的建站个数或建 设费用最小的问题.集覆盖问题最早是由 Roth和 Toregas等提出的,用于解决消防中心和救护车等的应急服务设施的选址问题,他们 ...
- 敬请贤者:WEB、IOS开发(2年以上经验,大专);CTO、产品经理,运营专员 电商服装鞋饰买手(2年以上经验,服装或鞋类);体验店店长 (2年以上经验,服装或鞋类) 工作地点:丰台南苑路;有意者小窗QQ2211788980 - V2EX
敬请贤者:WEB.IOS开发(2年以上经验,大专):CTO.产品经理,运营专员 电商服装鞋饰买手(2年以上经验,服装或鞋类):体验店店长 (2年以上经验,服装或鞋类) 工作地点:丰台南苑路:有意者小窗 ...
- H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)
H5打包的app实现支付及支付宝支付,本章主要详解支付宝支付,微信支付请查看另一篇“H5使用codovar插件实现微信支付(微信APP支付模式,前端)” ps:本文只试用H5开发的,支付宝 APP支付 ...
- H5使用codovar插件实现微信支付(微信APP支付模式,前端)
H5打包的app实现微信支付及支付宝支付,本章主要详解微信支付,支付宝支付请查看另一篇“H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)” ps:本文只试用H5开发的,微信 AP ...
随机推荐
- Effective C++笔记:继承与面向对象设计
关于OOP 博客地址:http://www.cnblogs.com/ronny 转载请注明出处! 1,继承可以是单一继承或多重继承,每一个继承连接可以是public.protected或private ...
- MariaDB 主从同步与热备(14)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- [JavaScript] 根据指定宽度截取字符串
/** * 根据指定宽度截取字符串 * @param desc 原始字符串 * @param width 该显示的宽度 * @param fontsize 字体大小 12px * @returns { ...
- Python做web开发,推荐几个能立马上手的小项目
Python这门优美的语言是非常适合web开发的,基于Python的Django框架简单便捷且很强大. 那么作为新手该如何上手这门语言?一切不敲代码的学编程手段都是扯淡,今天就推荐一些适合新手练手的P ...
- 机器学习-Matplotlib绘图(柱状图,曲线图,点图)
matplotlib 作为机器学习三大剑客之一 ,比热按时无比强大的 matplotlib是绘图库,所以呢我就分享一下简单的绘图方式 #柱状图 #导报 柱状图 import matplotlib. ...
- Python使用动态的变量名
当我们在使用Python处理一些重复性很高的事情时,有时候需要很多的变量来存放一些暂行性的数据,由于这些变量的数量很大,所以这使我们就会想到能不能使用循环来像生成数据值一样生成变量名呢,当然是可以的 ...
- MyBatis框架介绍及其实操
一.基本概念和介绍 数据持久化的概念 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称.例如,文件的存储.数据的读取等都是数据持久化操作.数据模型可以是任何数据 ...
- odoo datetime 直接修改模版语言 去掉时分秒
<field name='date_order' widget='date'/> 利用date widget即可使dateime类型的显示为date.
- Java生成某段时间内的随机时间
上代码: import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { /** * 生成随机时间 ...
- ES6箭头函数this指向
普通函数中的this: 1. this总是代表它的直接调用者(js的this是执行上下文), 例如 obj.func ,那么func中的this就是obj 2.在默认情况(非严格模式下,未使用 'us ...