1044. Shopping in Mars (25) time limit MS Memory limit 65536 KB code length limit 16000 B award Program StandardAuthor Chen, Yue
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond have a value (in Mars dollars m$). When making the payment, the chain can is cut at any position for only once and some of the diamonds is taken off the CHA In one by one. Once a diamond is off the chain, it cannot was taken back. For example, if we had a chain of 8 diamonds with the values m$3, 2, 1, 5, 4, 6, 8, 7, and we must pay m$15. We may have 3 options:
1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).
Now given the chain of diamond values and the amount a customer have to pay, you is supposed to list all the paying O Ptions for the customer.
If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.
Input Specification:
Each input file contains the one test case. For each case, the first line contains 2 numbers:n (<=105), the total number of diamonds on the chain, and M (<=108 ), the amount that the customer have to pay. Then the next line contains N positive numbers D1 ... DN (di<=103 for all i=1, ..., N) which is the values of the diamonds. All the numbers in a line is separated by a space.
Output Specification:
For each test case, the print "I-j" in a line for each pair of I <= J such that Di + ... + Dj = M. Note that if there is M Ore than one solution, all the solutions must is printed in increasing order of I.
If There is no solution, output "i-j" for pairs of I <= j such that Di + ... + Dj > M with (Di + ... + dj-m) minim Ized. Again all the solutions must is printed in increasing order of I.
It is guaranteed, the total value of diamonds was sufficient to pay the given amount.
Sample Input 1:
16 153 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
Sample Output 1:
1-54-67-811-11
Sample Input 2:
5 132 4 5 7 9
Sample Output 2:
2-44-5
Submit Code
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <cmath>8#include <string>9#include <map>Ten#include <Set> One using namespacestd; Avector<pair<int,int> >Line ; - #defineINF 100000005 - intMain () { the //freopen ("D:\\input.txt", "R", stdin); - intMinsum;//the minimum value in history - intn,sum,i,j; -scanf"%d%d", &n,&sum);//Minimum value specified + int*dia=New int[n+5]; - for(i=1; i<=n;i++){ +scanf"%d",&dia[i]); A } atdia[0]=dia[1]; -j=1;//Virtual 0 Location - intcursum=dia[1]+dia[0];//the current minimum value -minsum=inf; -Line.push_back (Make_pair (0,0)); - for(i=1; i<=n;i++) {//Pointer Ideas incursum-=dia[i-1]; - while(j<n&&cursum<sum) { tocursum+=dia[++j]; + } - if(cursum>=sum&&cursum<minsum) {//Update the //the cursum>=sum here is for J to be set at the end of the array. * //J before if it has reached the end, I move backwards there may be cursum may be equal to sum, but must be reduced $ line.clear ();Panax Notoginseng Line.push_back (Make_pair (i,j)); -minsum=cursum; the } + Else{ A if(cursum==minsum) {//Insert the Line.push_back (Make_pair (i,j)); + } - } $ } $ for(i=0; I<line.size (); i++){ -printf"%d-%d\n", Line[i].first,line[i].second); - } the return 0; -}
pat1044. Shopping in Mars (25)