1044 Shopping in Mars(25 分)

1044 Shopping in Mars(25 分)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M ) . W h e n m a k i n g t h e p a y m e n t , t h e c h a i n c a n b e c u t a t a n y p o s i t i o n f o r o n l y o n c e a n d s o m e o f t h e d i a m o n d s a r e t a k e n o f f t h e c h a i n o n e b y o n e . O n c e a d i a m o n d i s o f f t h e c h a i n , i t c a n n o t b e t a k e n b a c k . F o r e x a m p l e , i f w e h a v e a c h a i n o f 8 d i a m o n d s w i t h v a l u e s M 3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:

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).
Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
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 (≤10
​5
​​ ), the total number of diamonds on the chain, and M (≤10
​8
​​ ), the amount that the customer has to pay. Then the next line contains N positive numbers D
​1
​​ ⋯D
​N
​​ (D
​i
​​ ≤10
​3
​​ 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 13
Sample Output 1:
1-5
4-6
7-8
11-11
Sample Input 2:
5 13
2 4 5 7 9
Sample Output 2:
2-4
4-5

解题思路:
运用二分查找,否则会超时。把输入数组元素转化为其前面元素的和,输入为正,所以sum数组递增。要得到从i~j的和为m,即求sum[j]-sum[i-1]。用二分查找找到第一个大于sum[i-1]+m的元素位置j。
只要有sum[j-1]-sum[i-1]==m,就表示有解。
否则sum[j-1]-sum[i-1]肯定

#include<cstdio> 
#include<algorithm>
using namespace std;
const int maxn=1e5+10;

int sum[maxn];
int upper_bound(int l,int r,int x){
    int left=l,right=r,mid;
    while(left<right){
        mid=(left+right)/2;
        if(x<sum[mid]){
            right=mid;
        }else{
            left=mid+1;
        }
    }
    return left;
}

int main(){
    int n,m,ans=1e8+10;
    sum[0]=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&sum[i]);
        sum[i]=sum[i-1]+sum[i];
    }
    for(int i=1;i<=n;i++){
        int j=upper_bound(1,n+1,m+sum[i-1]);
        if(sum[j-1]-sum[i-1]==m){
            ans=m;
            break;
        }else if(j<=n&&sum[j]-sum[i-1]<ans){
            ans=sum[j]-sum[i-1];
        }
    }
    for(int i=1;i<=n;i++){
        int j=upper_bound(1,n+1,sum[i-1]+ans);
        if(sum[j-1]-sum[i-1]==ans){
            printf("%d-%d\n",i,j-1);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/chenyutingdaima/article/details/82079972