博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1044 Shopping in Mars (25 分)
阅读量:4484 次
发布时间:2019-06-08

本文共 4964 字,大约阅读时间需要 16 分钟。

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$). 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 Npositive 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 分析:一开始直接模拟发现测试点2和5超时了,解决办法:sum是严格递增的,所以可以用二分来加速。。不过这题的二分好难写,注意下标位置 原始代码:
1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Mered1th 5 * Date : 2019-02-26-15.17.46 6 * Description : A1044 7 */ 8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 using namespace std;19 const int INF=1000000010;20 const int maxn=100010;21 int a[maxn];22 int n,S,nearS=INF;23 struct Node{24 int l,r;25 };26 vector
ans;27 int main(){28 #ifdef ONLINE_JUDGE29 #else30 freopen("1.txt", "r", stdin);31 #endif32 int n,m;33 scanf("%d%d",&n,&m);34 for(int i=1;i<=n;i++){35 scanf("%d",&a[i]);36 }37 int sum,i,j;38 for(i=1;i<=n;i++){39 sum=0;40 for(j=i;j<=n;j++){41 sum+=a[j];42 if(sum>=m){43 if(nearS>sum){44 ans.clear();45 nearS=sum;46 ans.push_back(Node{i,j});47 }48 else if(nearS==sum){49 ans.push_back(Node{i,j});50 }51 break;52 }53 }54 }55 for(int i=0;i

 

 

二分:

1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Mered1th 5 * Date : 2019-02-26-15.17.46 6 * Description : A1044 7 */ 8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 using namespace std;19 const int INF=1000000010;20 const int maxn=100010;21 int sum[maxn],nearS=INF;22 int main(){23 #ifdef ONLINE_JUDGE24 #else25 freopen("1.txt", "r", stdin);26 #endif27 int n,m;28 scanf("%d%d",&n,&m);29 sum[0]=0;30 for(int i=1;i<=n;i++){31 scanf("%d",&sum[i]);32 sum[i]+=sum[i-1];33 }34 for(int i=1;i<=n;i++){35 int j=upper_bound(sum+i,sum+n+1,sum[i-1]+m)-sum;36 if(sum[j-1]-sum[i-1]==m){37 nearS=m;38 break;39 }40 else if(j<=n &&sum[j]-sum[i-1]

 

自定义二分。。

1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Mered1th 5 * Date : 2019-02-26-15.17.46 6 * Description : A1044 7 */ 8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 using namespace std;19 const int INF=1000000010;20 const int maxn=100010;21 int sum[maxn],nearS=INF;22 int main(){23 #ifdef ONLINE_JUDGE24 #else25 freopen("1.txt", "r", stdin);26 #endif27 int n,m;28 scanf("%d%d",&n,&m);29 sum[0]=0;30 for(int i=1;i<=n;i++){31 scanf("%d",&sum[i]);32 sum[i]+=sum[i-1];33 }34 for(int i=1;i<=n;i++){35 int j=upper_bound(sum+i,sum+n+1,sum[i-1]+m)-sum;36 if(sum[j-1]-sum[i-1]==m){37 nearS=m;38 break;39 }40 else if(j<=n &&sum[j]-sum[i-1]

 

 

转载于:https://www.cnblogs.com/Mered1th/p/10438988.html

你可能感兴趣的文章
反射+特性实现 类和XML文档的序列化反序列化
查看>>
日常方法
查看>>
用于后台管理基于iview,vue的穿梭框
查看>>
关于HashMap
查看>>
RuntimeException、Exception和error的区别
查看>>
数据库原理知识
查看>>
Git rebase
查看>>
C语言程序结构
查看>>
linux 设置固定IP centOS6.5
查看>>
Java 重写(Override)与重载(Overload) (转子Runood)
查看>>
Linux常用命令总结
查看>>
飞扬的小鸟(NOIP2014)(丧病DP题)
查看>>
UITableView 的使用小点
查看>>
SpringBoot项目在新电脑上的配置运行,包括JDK+MAVEN+Git+SpringBoot配置等
查看>>
bzoj 3754: Tree之最小方差树 模拟退火+随机三分
查看>>
基于JavaMail的Java邮件发送:简单邮件发送
查看>>
那些年,我追过的绘图工具
查看>>
单例实现方式
查看>>
04 Python并发编程(守护进程,进程锁,进程队列)
查看>>
win7下安装fedora16
查看>>