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. google breakpad for linux(2)

    breakpad 是什么 breakpad 是一个包含了一系列库文件和工具的开源工具包,使用它可以帮助我们在程序崩溃后进行一系列的后续处理,如现场的保存(core dump),及事后分析(重建 cal ...

  2. [Ynoi2019模拟赛]Yuno loves sqrt technology II(二次离线莫队)

    二次离线莫队. 终于懂了 \(lxl\) 大爷发明的二次离线莫队,\(\%\%\%lxl\) 二次离线莫队,顾名思义就是将莫队离线两次.那怎么离线两次呢? 每当我们将 \([l,r]\) 移动右端点到 ...

  3. jzoj3086 [分層圖最短路]

    分層圖最短路即可 #include<bits/stdc++.h> using namespace std; #define N 1000010 int n,m,v[N*2],nxt[N*2 ...

  4. 1-1 Vue的介绍

    简单介绍Vue Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易 ...

  5. 【sping揭秘】4、某些无法注册到IOC容器的对象如何交给spring托管

    可以实现spring的factoryBean 接口,这样可以加入spring的IOC容器 比如现在有一个类叫MyObject,我们没有这个对象的源码,无法对这个对象进行操作,那么我们如何加入sprin ...

  6. Storm中的定时任务

    1.全局定时器 import java.util.Map; import backtype.storm.Config; import backtype.storm.Constants; import ...

  7. NIO基础之同步、异步、阻塞、非阻塞

    这里区分几个概念,也是常见但是容易混淆的概念,就是标题中的同步.异步.阻塞.非阻塞. 一.同步与异步 同步与异步,关心的是消息通信的机制.也就是调用者和被调用者之间,消息是如何进行通知的.如果是调用者 ...

  8. ES6基础教程一 学习笔记

    一.变量的声明 1.var 声明全局变量 在ES6中var用来声明全局变量. 2.let 声明局部变量 3.const 声明常量 二.变量的解构赋值 //1.数组赋值 let [a,b,c]=[1,2 ...

  9. kafka 生产者基本操作

    kafka自带了一个在终端演示生产者发布消息的脚本--kafka-console-producer.sh 运行该脚本会启动一个进程,在运行该脚本时可以传递相应配置以覆盖默认配置. 参数-- -- pr ...

  10. DataSet 多表关系

    protected void Page_Load(object sender, EventArgs e) { string connectionString = @"Data Source= ...