1044. Shopping in Mars (25)

Source: Internet
Author: User

The topics are as follows:

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 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&NBSP; 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, print "I-j" in a line for each pair of I <= J such that Di + ... + dj = M. Note that if the Re is more 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 + ... + D j-m) minimized. 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


When the topic requires generalization, it can be described as: Find all the sub-columns of the sub-columns that meet the requirements of the topic in the linear table of all the elements, and if there are no child columns that meet the requirements, find out the location of all the child columns and the closest child columns that are required by the topic.

To solve this problem, we set two pointers to the current sub-column position, starting and end, in order to meet the requirements, we set the linear table from the 1-n label, the linear table is stored in the container n, define a sum to record the current child column and, Use two containers answers and mincurs to record the location (start and end) of the child columns that meet the topic requirements, and all the child column locations closest to the topic.

① initialization, start=end=1,answers and mincurs are emptied, minsum=inf is used to process the sub-columns that are close to the topic, and M has the sub-columns required by the topic.

② defines a last_end=-1, when the sub-column forward, if Sum>m, that the start should move backward, and end does not move, this time in order to prevent the end position is repeated accumulation, to determine whether the last_end equals end, does not equal to the accumulation, Ensure that the same end is processed only once.

The ③ core logic is as follows:

1. Judge whether Last_end is equal to end, not equal to last_end=end; Sum+=n[end];

2. Judging the current sum

2.1 If sum < M, the sub-column and small, should continue to push forward, that is, end++;

2.2 If sum = = M, indicating that the appropriate child column is found, record the current start and end to the answers container, the next should let start forward, looking for a new child column, the processing method is Sum-=n[start]; start++; Since start is exactly m in its original position, it must now be less than M, and the sub-column should continue to advance, i.e. end++;

2.3 If sum > M, the description of the child column and large, first determine the current child column and sum with the recorded minsum which large

2.3.1 If sum < Minsum is found to have a smaller sub-column close to the topic requirement, the previous mincurs should be emptied, then the current position is recorded, and the update minsum to sum.

2.3.2 if sum = = Minsum, the new sub-column with the nearest condition is found and continues to press into the mincurs.

In addition, this time because the sum is too large, the child column should not advance, so the end is unchanged, only let start forward, thereby narrowing the sum of the child column, the front of the last_end can guarantee that the start activity when the end is not accumulated.

3. After the processing is finished, first determine if the answers has a value, then the output, and then the direct program ends.

4. If the answers does not have a value, the elements in all mincurs are output.


Topic Note: Using CIN and cout has a test point that will time out and use scanf and printf as much as possible in your usual practice.


The procedure is as follows:

#include <iostream> #include <vector> #include <stdio.h> #include <stdlib.h>using namespace std    ; struct cur{int start;    int end;    Cur (int s, int e): Start (s), End (e) {}};int main () {int N;    Long M;    Long minsum = 900000000;    int start,end;    Vector<cur> answers;    Vector<cur> mincurs;    Answers.clear ();    Mincurs.clear ();    int value;    CIN >> N >> M;    Vector<int> Chain (N + 1);        for (int i = 1; I <= N; i++) {scanf ("%d", &value);    Chain[i] = value;    } start = end = 1;    int sum = 0;    int last_end =-1;            while (end <= N) {if (end! = last_end) {last_end = end;        Sum + = Chain[end];        } if (Sum < M) {end++;            }else if (sum = M) {answers.push_back (Cur (start,end));            Sum-= Chain[start];            start++;        end++; }else{//Sum > M if (minsum > Sum) {minsum = sum;                Mincurs.clear ();            Mincurs.push_back (Cur (start,end));            }else if (minsum = = sum) {mincurs.push_back (Cur (start,end));            } Sum-= Chain[start];        start++;        }} for (int i = 0; i < answers.size (); i++) {Cur c = answers[i];    printf ("%d-%d\n", c.start,c.end);    } if (Answers.size () > 0) return 0;        for (int i = 0; i < mincurs.size (); i++) {Cur c = mincurs[i];    printf ("%d-%d\n", c.start,c.end); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1044. Shopping in Mars (25)

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.