Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11581 Accepted Submission(s): 4780
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
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11581 Accepted Submission(s): 4780
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
cmp函数的运用,
bool cmp(T x,T y)
{
return x>y;
}
在原木棍上安装不需要时间,在原木棍上安装的条件是,长和宽都比原来的大或者等于,一轮一轮的来,从第一个木棍开始,如果接下来的木棍有符合的,就把这个木棍安装到原木棍上,然后这个新安装的木棍就变成了正在等待新木棍安装,它就变成了老木棍。。。一轮完了之后,就开始新的一轮,从下一个还没有安装的木棍开始,a.cnt=1都是已经安装了的,=0的在过程执行当中有两个意思,一个是需要重新安装的,一个是还没有用到的。
最后a.cnt=0的就是需要重头安装的,遍历一遍就得出需要的时间了。
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
struct node
{
int l,w,cnt;
}a[5005],de;
bool cmp(node x,node y)
{
return x.l<y.l||(x.l==y.l&&x.w<y.w);
}
int main(void)
{
// ifstream cin("1051.txt");
int t,n;
while(cin>>t)
{
for(int i=1;i<=t;i++)
{
cin>>n;
for(int j=0;j<n;j++)
{
cin>>a[j].l>>a[j].w;
a[j].cnt=0;
}
sort(a,a+n,cmp);
int sum=0;
for(int i=0;i<n;i++)
{
if(a[i].cnt==0)
{
de=a[i];
for(int j=i+1;j<n;j++)
{
if(a[j].cnt==0)
{
if(de.w<=a[j].w)
{
de=a[j];
a[j].cnt=1;
}
}
}
}
}
for(int i=0;i<n;i++)
{
if(a[i].cnt==0)
sum++;
}
cout<<sum<<endl;
}
}
return 0;
}
在来个简单版的,用的是贪心算法惯用的模板。这个程序也是在刷了很多题之后才出来的感觉,貌似贪心算法的这个模板很好用。。。
<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct stick
{
int l;
int w;
}stick[5005],ss[5005];
bool cmp(struct stick a,struct stick b)
{
if(a.l!=b.l) return a.l<b.l;
else return a.w<b.w;
}
int main(void)
{
// freopen("E.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&stick[i].l,&stick[i].w);
/* if(stick[i].l<stick[i].w)
{
int t;
t=stick[i].l;
stick[i].l=stick[i].w;
stick[i].w=t;
}*/
}
sort(stick+1,stick+n+1,cmp);
ss[1].l=stick[1].l;
ss[1].w=stick[1].w;
int k=1;
int time=1;
for(int i=2;i<=n;i++)
{
int ok=0;
for(int j=1;j<=k;j++)
{
if(stick[i].w>=ss[j].w)
{
ss[j].l=stick[i].l;
ss[j].w=stick[i].w;
ok=1;
break;
}
}
if(ok==0)
{
ss[++k].l=stick[i].l;
ss[k].w=stick[i].w;
time++;
}
}
printf("%d\n",time);
}
return 0;
}