1044. Shopping in Mars (25)

1.题目要求在一个数组中,找出等于amount的连续子序列和,如果不存在等于的,则找出大于amount的最小子序列的和。

2.使用尺取法,如果取得范围总和大于需要pay的,删掉头部。

3.如果取得范围综合小于pay的,增加尾部。

4.注意边界情况和没有相等的情况,细节可查看代码。

5.利用一个minAns的变量进行记录答案数值。

部分测试用例:

16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
 
5 13
2 4 5 7 9
 
 10 1
 2 3 4 5 6 1 2 3 4 6 
 
 10 1
 2 3 4 5 1 1 2 1 1 1
 ans:
 5-5
 6-6
 8-8
 9-9
 10-10
 
 10 0
 2 3 4 5 1 1 2 1 1 1
 ans:
 5-5
 6-6
 8-8
 9-9
 10-10
 
 10 -1
 2 3 4 5 1 1 2 1 1 1
 ans:
 5-5
 6-6
 8-8
 9-9
 10-10
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 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

AC代码:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <memory.h>
#include <map>
#include <set>
#include "limits.h"
using namespace std;

/*
16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
 
5 13
2 4 5 7 9
 
 10 1
 2 3 4 5 6 1 2 3 4 6 
 
 10 1
 2 3 4 5 1 1 2 1 1 1
 ans:
 5-5
 6-6
 8-8
 9-9
 10-10
 
 10 0
 2 3 4 5 1 1 2 1 1 1
 ans:
 5-5
 6-6
 8-8
 9-9
 10-10
 
 10 -1
 2 3 4 5 1 1 2 1 1 1
 ans:
 5-5
 6-6
 8-8
 9-9
 10-10
 */
bool cmp(const pair<int,pair<int,int>>&a,const pair<int,pair<int,int>>&b)
{
    if(a.first<b.first)
        return true;
    else if(a.first==b.first&&a.second.first<b.second.first)
        return true;
    else return false;
}
int main(void)
{
    int sum,pay;
    scanf("%d %d",&sum,&pay);
    vector<int> diamonds(sum);
    for(int i=0;i<sum;i++)
    {
        scanf("%d",&diamonds[i]);
    }
    int i=0,j=0;
    int value=diamonds[0];//默认输入的数组大于0
    int minAns=INT_MAX;
    vector<pair<int,pair<int,int>>> ans(0);
    while(1)
    {
        if(value==pay&&i<=j)
        {//加入i<=j,避免出现2-1,6-4之类的,j小于i的情况
            minAns=min(minAns,value);
            ans.push_back({value,{i+1,j+1}});
            value-=diamonds[i++];
        }
        else if(value>pay&&i<=j)
        {//加入i<=j,避免出现2-1,6-4之类的,j小于i的情况
            if(value<=minAns)
            {//value>pay && value<=minAns,所以 pay<value<=minAns,minAns>pay
                minAns=value;
                ans.push_back({value,{i+1,j+1}});
            }
            value-=diamonds[i++];
            
        }
        else//value<pay
        {
            j++;
            if(j<diamonds.size())
                value+=diamonds[j];
            else
                break;
        }
    }
    sort(ans.begin(), ans.end(), cmp);
    for(int k=0;k<ans.size();k++)
    {
        if(ans[k].first==minAns)
            printf("%d-%d\n",ans[k].second.first,ans[k].second.second);
        else break;
    }
    
    
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注