1044. Shopping in Mars

Source: Internet
Author: User

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.


Segment error ... I don't know, I'm not sure. The array has opened up very big ...

#include <stdio.h> #include <stdlib.h> #include <vector>using namespace std; #define N 10001typedef        struct{int start,end;        Long sum;    }node;int num[n];int N;long cost;int Main () {scanf ("%d%ld", &n,&cost);    int i,j;    Vector<node> equal;    Vector<node> min;    Long now=0,minsum=100000002;    Node temp;    for (i=1;i<=n;i++) scanf ("%d", num+i);         for (i=1;i<=n;i++) {now+=num[i];         J=i;                while (Now<cost&&j<n) {j + +;                NOW+=NUM[J];         } temp.start=i;         Temp.end=j;         Temp.sum=now;             if (now==cost) {equal.push_back (temp); } else if (temp.sum>cost) {if (temp.sum<minsum) {Minsum=te                 Mp.sum;                 Min.clear ();                 Min.push_back (temp); } else if (temp.sum==minsum) min.push_bACK (temp);         } now=0; } if (Equal.size () ==0) {for (I=0;i<min.size (); i++) {printf ("%d-%d\n", min[i].start,min             [I].end]; }} else {for (i=0;i<equal.size (); i++) {printf ("%d-%d\n", equal[i].start,equal[             I].end);    }} system ("Pause");    return 0; }



1044. Shopping in Mars

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.