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 (≤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 Npositive 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

分析:一开始直接模拟发现测试点2和5超时了,解决办法:sum是严格递增的,所以可以用二分来加速。。不过这题的二分好难写,注意下标位置

原始代码:
 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-15.17.46
 * Description : A1044
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 ;
 int a[maxn];
 int n,S,nearS=INF;
 struct Node{
     int l,r;
 };
 vector<Node> ans;
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,m;
     scanf("%d%d",&n,&m);
     ;i<=n;i++){
         scanf("%d",&a[i]);
     }
     int sum,i,j;
     ;i<=n;i++){
         sum=;
         for(j=i;j<=n;j++){
             sum+=a[j];
             if(sum>=m){
                 if(nearS>sum){
                     ans.clear();
                     nearS=sum;
                     ans.push_back(Node{i,j});
                 }
                 else if(nearS==sum){
                     ans.push_back(Node{i,j});
                 }
                 break;
             }
         }
     }
     ;i<ans.size();i++){
         printf("%d-%d\n",ans[i].l,ans[i].r);
     }
     ;
 }

二分:

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-15.17.46
 * Description : A1044
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 ;
 int sum[maxn],nearS=INF;
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,m;
     scanf("%d%d",&n,&m);
     sum[]=;
     ;i<=n;i++){
         scanf("%d",&sum[i]);
         sum[i]+=sum[i-];
     }
     ;i<=n;i++){
         ,sum[i-]+m)-sum;
         ]-sum[i-]==m){
             nearS=m;
             break;
         }
         ]<nearS){
             nearS=sum[j]-sum[i-];
         }
     }
     ;i<=n;i++){
         ,sum[i-]+nearS)-sum;
         ]-sum[i-]==nearS){
             printf();
         }
     }
     ;
 }

自定义二分。。

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-15.17.46
 * Description : A1044
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 ;
 int sum[maxn],nearS=INF;
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,m;
     scanf("%d%d",&n,&m);
     sum[]=;
     ;i<=n;i++){
         scanf("%d",&sum[i]);
         sum[i]+=sum[i-];
     }
     ;i<=n;i++){
         ,sum[i-]+m)-sum;
         ]-sum[i-]==m){
             nearS=m;
             break;
         }
         ]<nearS){
             nearS=sum[j]-sum[i-];
         }
     }
     ;i<=n;i++){
         ,sum[i-]+nearS)-sum;
         ]-sum[i-]==nearS){
             printf();
         }
     }
     ;
 }



1044 Shopping in Mars (25 分)的更多相关文章

  1. PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)

    1044 Shopping in Mars (25 分)   Shopping in Mars is quite a different experience. The Mars people pay ...

  2. 1044 Shopping in Mars (25分)(二分查找)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  3. 1044 Shopping in Mars (25 分)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  4. PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]

    题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...

  5. 【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)

    题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. ...

  6. 1044. Shopping in Mars (25)

    分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...

  7. PAT (Advanced Level) 1044. Shopping in Mars (25)

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. PAT甲题题解-1044. Shopping in Mars (25)-水题

    n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...

  9. A1044 Shopping in Mars (25 分)

    一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...

  10. pat1044. Shopping in Mars (25)

    1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shop ...

随机推荐

  1. L1-036 A乘以B

    看我没骗你吧 —— 这是一道你可以在 10 秒内完成的题:给定两个绝对值不超过 100 的整数 A 和 B,输出 A 乘以 B 的值. 输入格式: 输入在第一行给出两个整数 A 和 B(−100≤A, ...

  2. 会话跟踪session

    会话跟踪 HTTP是“无状态”协议:客户程序每次读取Web页面,都打开到web服务器的单独的连接,而且,服务器也不自动维护客户的上下文信息.类似客户决定结账时,如何确定之前创建的购物车中哪个属于此客户 ...

  3. Java Socket 实现HTTP服务器核心

    原文链接:http://www.ihuxu.com/p/235.html   首先了解下HTTP协议: wikiPedia的说明很好,在此不重复了.链接:http://zh.wikipedia.org ...

  4. STL标准库-算法-常用算法

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 介绍11种STL标准库的算法,从这11种算法中总结一下算法的基本使用 1.accumulate() 累加 2.for_each( ...

  5. 萤石A1互联网报警盒子破解细节分析

    攻击点分析:  萤石A1互联网报警盒子使用“全无线解决方案”,传感器的报警通过433.92MHz射频信号发送给报警主机,报警主机可以通过Wi-Fi联网,将报警上传萤石云端,云端会将信息推送到手机端的“ ...

  6. AVAudioPlayer播放音频文件时没声音

    AVAudioPlayer播放一个mp3文件时,居然没有声音.mp3文件是放在工程里面的,路径没有错误但就是死活没有声音. func playSound() { let notifyUrl = NSB ...

  7. 12.2 linux下的线程

    什么是线程: 在一个程序里的一个执行路线就叫做线程(thread),更准确的定义是:线程是“一个进程内部的控制序列” 一切进程至少都有一个执行线程 进程与线程: 进程是资源竞争的基本单位 线程是程序执 ...

  8. ODBC的基础架构

    *) 基本概念:1. 应用程序(Application)2. ODBC驱动管理器(ODBC Driver Manager) 负责管理应用程序和驱动程序间的通信,主要功能包括:解析DSN (数据源名称, ...

  9. UnicodeDammit

    UnicodeDammit 是BS内置库, 主要用来猜测文档编码. 编码自动检测 功能可以在Beautiful Soup以外使用,检测某段未知编码时,可以使用这个方法: from bs4 import ...

  10. 【JUnit】@Test 报错,"Test cannot be resolved to a type"

    想用单元测试 JUnit 单元测试下写好的方法,发现写 @Test 标签报错了,"Test cannot be resolved to a type" 原来是项目没有导入 JUni ...