noip2010-Universal group Rematch--the second question--water connection

Source: Internet
Author: User

Title DescriptionDescription School has a water room, the water room is equipped with a total of M faucet for students to open water, each faucet per second
The quantity of water supply is equal to 1.
Now there are n students ready to pick up water, and their initial water order has been determined. These students according to water order from 1 to n number, I number of students of the water intake is WI. When the water starts, 1 to m students each occupy a faucet, and at the same time turn on the tap to connect water. When one of the students J finished their water demand WJ, the next line of students waiting to receive the water, K immediately take over the position of J classmate began to connect water. The process of substitution is instantaneous, without any waste of water. That is, J classmate at the end of the first X seconds to complete the water, then K classmate x+1 seconds immediately began to pick up water. If the current number of water access n ' less than m, then only n ' Faucet water supply, the other m−n ' faucet closed.
Now give the water intake of n students, according to the above water rules, ask all the students how many seconds to finish the water. input/output format input/output Input Format:
The input file name is water.in.
Line 1th 2 integers n and m, separated by a space, respectively, indicating the number of water and faucet.
Line 2nd n integers W1, w2 、......、 Wn, separated by a space between each two integers, WI indicates the number I
The amount of water to learn.
output Format:
The output file name is Water.out.
The output is only one row, 1 integers, indicating the total time required to receive water. input and Output sample sample Input/output sample Test point # # Input Sample:

"Input Sample 1"
5 3
4 4 1) 2 1
"Input Sample 2"
8 4
23 71 87 32 70 93 80 76

Sample output:

"Output Example 1"
4
"Output Example 2"
163

DescriptionDescription "Input and output Example 1 description"
The 1th second, 3 people connect water. At the end of the 1th second, 1, 2, 3rd students each have received water of 1, 3rd students to finish
Water, 4th students to replace 3rd students began to pick up water.
The 2nd second, 3 people connect water. At the end of the 2nd second, 1, 2nd students each have received water of 2, 4th students have been connected
Water volume is 1.
The 3rd second, 3 people connect water. At the end of the 3rd second, 1, 2nd students each have received water of 3, 4th students have been connected
Water volume is 2. 4th students after the water, No. 5th students to replace the 4th students began to connect water.
The 4th second, 3 people connect water. At the end of the 4th second, 1, 2nd students each have received water of 4, 5th students have been connected
Water volume is 1. 1, 2, 5th students after the water, that is, all the water to complete.
The total water connection time is 4 seconds.
"Data Range"
1≤n≤10000,1≤m≤100 and M≤n;
1≤wi≤100. Ideas: This problem, it seems a little trouble, but not difficult, pure simulation, simulation of the water process in two situations consider: (1) The number of water n<= faucet number, from large to small sort, the output of the largest (2) the number of water n> faucet number, first of all because the order before the row                                 , put the first number of people directly into the star array, and put the remainder in the end array loop n times { Cycles m times, each time-1 (indicating that the person who is now receiving water at this moment), counter time++, and then judge, if someone has finished the water, end the rest of the array of people replace the code as follows:
1#include <stdio.h>2#include <stdlib.h>3 intFunConst void*a,Const void*B)//big to small quick row4 {5    return*(int*) b-* (int*) A;6 }7 intMain ()8 {9     intN,m,i,j;//n number of water tap, number of M tapsTen     intTime=0, KK;//Times One     intw[10001]; A     intstar[10001],end[10001]; -     //freopen ("water.in", "R", stdin); -     //freopen ("Water.out", "w", stdout); thescanf"%d%d",&n,&m); -      for(i=0; i<n;i++)//Enter the number of each person to receive the water, save in the W array -     { -scanf"%d",&w[i]); +     } -     if(n<=m)//The number of water is less than the number of taps, from large to small row order, the direct output of the largest +     { AQsort (W,n,sizeof(int), fun); atprintf"%d\n", w[0]);//Output Maximum -     } -     Else if(n>m)//the number of water connection is greater than the number of taps, start simulation -     { -             for(i=0; i<m;i++)//The person who deposited in the water connection -         { instar[i]=W[i]; -         } to          for(j=i,kk=0; j<=n;j++,kk++)//Deposit The rest of the people +         { -end[kk]=W[j]; the         } *Kk=i;//back up the number of people in the water connection $j=0;Panax Notoginseng          while(n>0)//start simulating water connection -         { the              for(i=0; i<kk;i++)//simulated water intake process, the amount of water required by the person to be connected-1 +             { Astar[i]--; the             }     +time++;//Count +1 -                for(i=0; i<kk;i++)//If a person is finished, the remainder of the end array is replaced $             { $                 if(star[i]==0)//I'm done. -                   { -STAR[I]+=END[J];//Take over a man . thej + +;//end left in the array of people subscript j+1 -n--;//people -1Wuyi                   }                    the             }                                     -         }                 Wuprintf"%d\n", time);//number of outputs -     } About     return 0; $}

noip2010-Universal group Rematch--the second question--water connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.