zl857 2021-03-08 23:09 采纳率: 100%
浏览 54
已采纳

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

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)

题目的大概意思是说,有一堆木材需要加工,第一根木材加工需要1分钟,如果其他的木材的长度和宽度都大于或等于已经加工过的木材,则这根木材的加工不费时间,否则时间也为1分钟。求至少需要用多长时间。

本人的解题思路是:

假如木棍A的长度与宽度均大于木棍B,则木棍A是木棍B的子集;

如果木棍A的长度与宽度均小于木棍B,则木棍B是木棍A的母集;

如果木棍A仅有长度或宽度大于木棍B,则木棍A与木棍B不相关,A与B皆为母集;

而加工用的时间就等价于母集的个数;

伪程序如下:

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 2021-03-09 16:59
    关注

    我找不出来哪里错了.

    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条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题