2021-03-08 23:09

# Wooden_Sticks问题一直wrong，希望指出我程序错误的地方

20

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
```

Source

Asia 2001, Taejon (South Korea)

``````int main()
{
int time（所需时间）
输入T组数据
while(T--)
{
time=1;
输入n根木棍的l，w；
木棍【t】=母集1；
for(i=0;i<n;i++)
{
（明显）：
if(母集1>木棍【i】)
continue;
else if(母集1<=木棍【i】)
{
木棍【i】=母集1；
for（j=0;j<time;j--）
if(有大于母集1的母集j)
删除母集j
}
else
{
if(当前母集为所有母集最后一个)
创造新母集
else
由母集n到母集n+1，go to （明显）
}

}

}``````

``````#include<stdio.h>
#include<string.h>
#define init() a=NULL;b=&stick[t];c=b->p//初始化
struct wood
{
int l,w;
//在链表的就是母集，不在的都为子集
struct wood *p=NULL;
}stick[5005];
int cmp(struct wood a,struct wood b)//比较a与b的关系
{
if(a.l>b.l&&a.w>b.w) return 1;
else if(a.l<=b.l&&a.w<=b.w) return -1;
return 0;
}
int T,n,i,j,k;
int main()
{
int time,t;//time为所需时间 t为链表头
struct wood *a,*b,*c;
scanf("%d",&T);
while(T--)
{
//------------------------------------------//初始化
memset(stick,0,sizeof(stick));
time=1;
t=0;
//------------------------------------------//
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&stick[i].l,&stick[i].w);
init();
for(i=1;i<n;i++)
{
//判断当前木棍与母级关系
while(1)
{
if(cmp(*b,stick[i])==0)
{
if(c==NULL)//该木棍无相关全部母级(链表末尾都无相关)
{
//创造新母集
b->p=&stick[i];
time++;
//链表初始化
init();
break;
}
else
{
//跳转到下一母级
a=b;
b=b->p;
c=b->p;
}
}
else if(cmp(*b,stick[i])==1)
{
//让位母级	(比当前木棍小的不止一个子集)
if(a==NULL)//为链表首位
{
stick[i].p=stick[t].p;
t=i;
}
else
{
a->p=&stick[i];
stick[i].p=c;
}
while(b->p!=NULL)
{
//跳转到下一母级
a=b;
b=b->p;
c=b->p;
if(cmp(stick[i],*b)==-1)
{
time--;a->p=c;b=a;
}
}
init();
break;
}
else
{
init();break;
}
//该木棍为子集不做处理
}
}
printf("%d\n",time);
}
}``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 2条回答

• A_box_of_yogurt 1月前

我找不出来哪里错了.

c++做法, 用了一个sort排序(懒懒排序), 其他的用C语言也可以实现.希望这个代码能帮到你.

``````#include <cstdio>
#include <algorithm>

const int maxn = 5005;
struct node{int l, w;} a[maxn];

int cmp(const node &x, const node &y) {
if(x.l == y.l) return x.w < y.w;
return x.l < y.l;
}

int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d%d", &a[i].l, &a[i].w);
std::sort(a, a + n, cmp);//此处用了c++的sort排序, 其他的都是能用c语言实现的
int ans = 0;
for(int i = 0; i < n; i++) {
if(a[i].w) {
ans++;
int p = a[i].w;
for(int j = i+1; j < n; j++) {
if(p <= a[j].w) {
p = a[j].w;
a[j].w = 0;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}``````
点赞 评论 复制链接分享
• 歇歇 1月前

把错误详细代码发出来

点赞 评论 复制链接分享