1044. Shopping in Mars (25)

来源:互联网 发布:手机关闭蜂窝移动数据 编辑:程序博客网 时间:2024/05/29 13:29

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

边输入计算现有长度的总价,如果相等则可以直接输出头尾,如果大于所需价格则从头开始截,截止小于或者等于总价,等于就输出,小于继续循环;

#include <iostream>#include <vector>#include <cstdio>#include <utility>#define MAX 100005using namespace std;int main(){    int n, total;    int value = 0;    int pre = 1;    vector<int> a(MAX);    vector< pair<int, int> > mint;//记录不存在相等时的最小项链    scanf("%d%d", &n, &total);    int flag = 1;    int mintotal = MAX;    for(int i=1; i<=n; i++)    {        scanf("%d", &a[i]);        value += a[i];        if(value == total)//价格相等则输出        {            printf("%d-%d\n", pre, i);            flag = 0;        }        else if(value > total)//价格较大时        {            while(value > total)            {               if(flag)//如果不存在相等价格时                {                    if(value < mintotal)//如果价格比最小的价格还要小则清空之前记录并记录最小价格                    {                        mintotal = value;                        mint.clear();                        mint.push_back(make_pair(pre, i));                    }                    else if(value == mintotal)                    {                        mint.push_back(make_pair(pre, i));                    }                }                value -= a[pre];                pre++;            }            if(value == total)//可能正好减至相等就输出            {                printf("%d-%d\n", pre, i);                flag = 0;            }        }    }    if(flag)    {        for(int i=0; i<mint.size(); i++)            printf("%d-%d\n", mint[i].first, mint[i].second);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米5手机关机充电自动开机怎么办 华为平板怎么解锁密码忘了怎么办 华为荣耀手机开锁密码忘记了怎么办 畅玩7x密码忘了怎么办 过了时的手机没有刷机包怎么办? 刷了个刷机包游戏玩不了了怎么办? 华为麦芒5手机外放声音小怎么办 微信显示存储卡已拔出怎么办 储存卡已拔出微信头像不可用怎么办 智能手机的电话卡取不出来了怎么办 换了苹果手机通讯录没了怎么办 手机玻璃膜一角翘起来了怎么办 华为畅玩7x耗电快怎么办 魅蓝5s充电器死机了怎么办 苹果手机乐动力不计步数怎么办 意大利居留按手印时间过了怎么办 酷派t1手机解析包出现问题怎么办 p新买的手机壳有味怎么办 门锁钥匙口竖着钥匙放不进去怎么办 摩拜单车被别人骑走了怎么办 捡到苹果8p手机怎么办才能自己用 用力按压导致玻尿酸变形移位怎么办 华为麦芒5应用锁密码忘了怎么办 华为麦芒6应用锁密码忘了怎么办 华为手机的设置不在桌面了怎么办 华为手机所有应用都不在桌面怎么办 华为麦芒5设置页面不显示怎么办 华为麦芒5主屏页面不显示怎么办 6s p换屏幕原装太贵怎么办 4g手机开不开机黑屏怎么办 华为麦芒5 4g信号差怎么办 华为麦芒手机锁屏密码忘了怎么办 华为麦芒5相机拍相片倒了怎么办 红米5a开不了机怎么办 华为沾了海水打不开机怎么办 华为麦芒手机忘记锁屏密码怎么办 华为手机的方框键摁不了怎么办 笔记本自动更新到一半太慢了怎么办 华为麦芒5音量下键乱跑了怎么办 麦芒6手机QQ视频没声音怎么办 18:9看16:9黑边怎么办