Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请高手指点,两种不同的方法,不同的结果,都是AC的代码

Posted by swpujch at 2007-10-21 16:45:05 on Problem 3427 and last updated at 2007-10-21 16:47:05
In Reply To:我不觉得有什么不清楚的 Posted by:frkstyc at 2007-10-21 16:25:33
我们用了两种不同的理解,写了两个不同的代码,都可以过,

一:
直接求,等待年限最长的那棵树的 等待值,然后输出,(假设可以任意时间砍掉树,最后的 浪费值始终为0)
#include<iostream>
using namespace std;
int main()
{
    int n,l,t,sum=0;
    while(scanf("%d %d",&n,&l)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&t);
            t=t%l;
            if(t!=0 && sum<l-t)
              sum=l-t;
        }        
        printf("%d\n",sum);         
    }
    system("pause");
    return 0;
}

二:
假设等待n年,n从1到L,计算每一个n所对应的浪费值,选取最小浪费值的n,(在等待的 过程中不能砍数,到n年结束时,将所有的树全部砍到,最后的浪费值是最小的值)
#include<iostream>
using namespace std;

int data[30000];

int main()
{
    int n,l;
    int k;
    scanf("%d%d",&n,&l);

    int i,j;
    for(i = 0;i<l;i++)
          data[i] = 0;
    
    for(i = 0;i< n;i++)
    {
          scanf("%d",&j);
          j %= l;
          data[j]++;
    }
    int t = l;
    int out,sum,min=2147483641;
    while(t>0)
    {
           sum = 0;
           for(i = 0;i<l;i++)
           {
                 sum += data[i]*i;
           }
           int d = data[l-1];
           for(i = l-1;i>0;i--)
           {
                 data[i] = data[i-1];
           }
           data[0] = d;
           if(sum < min)
           {
                  min = sum;
                  out = l-t;
           }
           t--;           
    }
    printf("%d\n",out);
    
    system("pause");
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator