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. PS插件CameraRaw-HSL色彩模式

    一.HSL百度百科 HSL色彩模式是工业界的一种颜色标准,是通过对色相(H).饱和度(S).明度(L)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,HSL即是代表色相,饱和度,明度三 ...

  2. 【062新题】OCP 12c 062出现大量新题-15

    choose one In your Oracle 12c database, you plan to execute the command: SQL> CREATE TABLESPACE t ...

  3. linux防火墙(三)—— iptables语法之匹配条件

    一.iptables规则的匹配条件类型有三类 1.通用匹配:可直接使用,不依赖于其他条件或扩展,包括网络协议.IP地址.网络接口等条件 2.隐含匹配:要求以特定的协议匹配作为前提,包括端口.TCP标记 ...

  4. 一步步Cobol 400 上手自学入门教程01 - 基础概念

    先学习基础概念 1.COBOL字符:包含: User-defined words 用户定义字符 ŸSystem-names ŸReserved words 关键字 2.用户定义字符User-defin ...

  5. 基于alpine用dockerfile创建的nginx镜像

    1.下载alpine镜像 [root@docker43 ~]# docker pull alpine Using default tag: latest Trying to pull reposito ...

  6. Cookies与session的区别

    Cookies 机制 Cookies是服务器在本地机器上存储的一段文本,并随每一个请求发送至同一个服务器. IETF RFC2965 HTTP State Management Mechanism 是 ...

  7. 读书笔记(01) - JSON - JavaScript高级程序设计

    JSON与JavaScript对象 JSON是一种表示结构化数据的存储格式,语法格式上与JavasScript对象有些类似. TIPS: 与JavaScript对象的格式区别 不支持变量.函数或对象实 ...

  8. StreamSets学习系列之StreamSets的集群安装(图文详解)

    不多说,直接上干货! 若是集群安装 需要在对应节点执行相同的操作. 见 StreamSets学习系列之StreamSets支持多种安装方式[Core Tarball.Cloudera Parcel . ...

  9. docker namespaces

    https://docs.docker.com/engine/security/userns-remap/#prerequisites 注:以下验证环境为centos7.5 docker 18.09. ...

  10. apache 服务器概述--安装(一)

    一.安装httpd,elinks浏览器 [root@ ~]# yum install elinks httpd -y [root@ ~]# elinks www.baidu.com 二.配置文件 # ...