Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with 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 that a customer has to pay, you are supposed to list all the paying options 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 has to pay. Then the next line contains N positive numbers D1 ... DN (Di<=103 for all i=1, ..., N) which are the values of the diamonds. All the numbers in a line are 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 there are more than one solution, all the solutions must be 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) minimized. Again all the solutions must be printed in increasing order of i.
It is guaranteed that the total value of diamonds is sufficient to pay the given amount.
Sample Input 1:16 15 3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13Sample Output 1:
1-5 4-6 7-8 11-11Sample Input 2:
5 13 2 4 5 7 9Sample Output 2:
2-4 4-5
题目大意:
在火星上购物是一种完全不同的体验。火星人用一串钻石来支付。每一个钻石都有价值(火星上的美元)。在付款的时候,链条只能在任意位置切割一次,而且钻石被一个一个的从链条上取下。一旦钻石离开了链条,它就不能被收回。例如,如果我们有一个8个钻石的链条,它的价值分别为3, 2, 1, 5, 4, 6, 8, 7,我们必须支付15.我们可能有三种选择:
1.切断4-6之间的链,从1到5的位置取下钻石(值为3+2+1+5+4=15)。
2.在5之前或6之后切割,从4到6取下钻石(值为5+4+6=15)。
3.在8之前切割,从7到8取下钻石(值8+7=15)。
现在给出钻石的价值链条和用户必须支付的金额,你应该列出用户的所有付费选项。
如果不可能支付准确的金额,你必须给出损失最小的解决方案。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含两个数字:N(<=10的5次方),表示链上的钻石总数,M(<=10的8次方),是客户必须支付的金额。然后下一行包含N个正整数D1 ... DN (Di<=10的3次方对于1到N)是钻石的值。直线上得所有数字用空格隔开。
输出规范:
对于每个测试用例,在一行中打印“i-j”,每对i<=j,这样Di + ... + Dj = M 。注意如果有多个解决方案,所有的解决方案都必须按递增顺序打印。
如果没有解,输出“i-j”每对i<=j,这样Di + ... + Dj > M 并且Di + ... + Dj -M的值最小。同样,所有的解决方案都必须以递增的顺序打印出来。
保证钻石的总价值足以支付一定数量的支付金额。
代码:
#include<stdio.h>
int arr[100001];
struct node
{
int start;
int end;
}dis[100001];
int main()
{
int i,j,n,m,k,t,Min,sum,l;
scanf("%d %d",&n,&m);
t=0;
Min=0;
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
if(Min<m)
Min+=arr[i];
}
sum=0;
l=0;
for(i=1,j=1;j<=n+1;)
{
if(sum<m)
{
sum+=arr[j];
j++;
}
else
{
if(sum==Min)
{
dis[l].start=i;
dis[l].end=j-1;
l++;
}
else if(sum<Min)
{
Min=sum;
l=0;
dis[l].start=i;
dis[l].end=j-1;
l++;
}
sum-=arr[i];
i++;
}
}
for(i=0;i<l;i++)
{
printf("%d-%d\n",dis[i].start,dis[i].end);
}
return 0;
}