EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Ecology tax

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3443 Accepted: 1769

Description

In a big and rich on natural resources country, the government started a campaign to control deforestation. In fact the government is not too interested in how many trees get fallen, but rather how effectively the wood is utilized. So a law was passed which requires every logging company to pay amount of money in proportion to amount of wood that it wastes during operation.

A felling quota on some territory was allotted to a company in this country. Company lorries may only transport logs of exactly L meters long. So when a tree gets sawed into logs, the remainder is wasted.

Trees in this country grow exactly 1 meter per year, so the company may decrease the amount of tax to be paid by simply waiting for some years. Your task is to determine the number of years needed to achieve smallest possible tax. If there is more than one answer, find minimal (earliest) one.

Input

Input file contains number of trees N, length of log L, followed by integers i1 i2 ... iN — heights of each tree.

Constraints

1 ≤ N ≤ 30000, 1 ≤ Lik ≤ 30000

Output

Output file must contain single integer — number of years to wait.

Sample Input

Sample Input 1
3 1 
10 15 11
Sample Input 2
3 2
5 3 6

Sample Output

Sample Output 1
0
Sample Output 2
1

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

Source

Northeastern Europe 2006, Far-Eastern Subregion

最近信心全无,水点简单题把。。。。
#include <iostream>

using namespace std;

const int N=30000;
int height[N];
int l,n;
int min_rem, wait_year,rem;

int main()
{
    while(cin>>n>>l)
    {
        min_rem=N+1;
        wait_year=-1;
        for(int i=0; i<n; i++)
            cin>>height[i];

        for(int i=0; i<=l-1; i++)
        {
            rem=0;
            for(int j=0; j<n; j++)
            {
                rem+=(height[j]%l);
                height[j]++;
            }
            if(rem<min_rem)
            {
                 min_rem=rem;
                 wait_year=i;
            }
        }

        cout<<wait_year<<endl;
    }
    return 0;
}
 
posted on 2011-05-31 16:38  Eric-Yang  阅读(281)  评论(0编辑  收藏  举报