分数 25

全屏浏览题目

切换布局

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

代码长度限制

16 KB

时间限制

300 ms

内存限制

64 MB

理解柳神代码并加以注释,柳神思路见1044. Shopping in Mars (25)-PAT甲级真题(二分查找)_1044 柳婼_柳婼的博客-CSDN博客

#include<bits/stdc++.h>
using namespace std;
const int N=100004;
int n,m;
int sum[N];
void fun(int i,int &j,int &temp){//二分查找 
    int left=i,right=n;
    while(left<right){
        int mid=(left+right)/2;
        if(sum[mid]-sum[i-1]>=m)right=mid;
        else left=mid+1;
    }
    j=right;
    temp=sum[j]-sum[i-1];
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){//每次输入并记录当前端点到起点的总和
        cin>>sum[i];
        sum[i]+=sum[i-1]; 
    }
    int min=sum[n];//记录当前大于等于m且靠近m的所需钻石的最小值 
    vector<int>res;
    for(int i=1;i<=n;i++){
        int j,temp;
        fun(i,j,temp);//每次计算靠近m的最小区间 
        if(temp>min)continue;//比最小值要大,说明之前已经有比当前还要靠近m的区间了,故跳过 
        if(temp>=m){//可能满足条件的区间的值 
            if(temp<min){//比之前记录的最小值要小,则更新最小值并把结果数组清空 
                res.clear();
                min=temp;
            }
            res.push_back(i);//成对压入区间端点 
            res.push_back(j);
        }
    }
    for(int i=0;i<res.size();i+=2)printf("%d-%d\n",res[i],res[i+1]);//成对输出 
    return 0;
}

1044 Shopping in Mars(柳神39行代码+详细注释)相关推荐

  1. 1140 Look-and-say Sequence(22行代码+详细注释)

    分数 20 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 Look-and-say sequence is a sequence of integers as the follow ...

  2. python同时注释多行代码_python怎么同时对多行代码进行注释

    学会向程序中添加必要的注释,也是很重要的.注释不仅可以用来解释程序某些部分的作用和功能(用自然语言描述代码的功能),在必要时,还可以将代码临时移除,是调试程序的好帮手. 当然,添加注释的最大作用还是提 ...

  3. python怎么同时对多行代码进行注释?

    转载自品略图书馆 http://www.pinlue.com/article/2020/04/1007/4410139993103.html 学会向程序中添加必要的注释,也是很重要的.注释不仅可以用来 ...

  4. 【Python】python初学者应该知道与其他语言差异化的高效编程技巧(附测试代码+详细注释)

    目录 1. 交换变量 2. 集合去重 3. 列表推导.集合推导和字典推导 4. 统计字符串中各个字符出现的次数 5.优雅地打印JSON数据 6.行内的if语句 6. 符合正常逻辑的数值比较 7. 田忌 ...

  5. 【综合评价分析】熵权算法确定权重 原理+完整MATLAB代码+详细注释+操作实列

    [综合评价分析]熵权算法确定权重 原理+完整MATLAB代码+详细注释+操作实列 文章目录 1. 熵权法确定指标权重 (1)构造评价矩阵 Ymn (2)评价矩阵标准化处理 (3)计算指标信息熵值 Mj ...

  6. c语言期中项目实战二—简易扫雷,思路分析加代码详细注释

    c语言期中项目实战二-简易扫雷,思路分析+代码详细注释 游戏介绍 项目步骤 模块化编程 设置菜单 设置棋盘 打印棋盘 布置雷 排查雷 总结及总代码和详细注释 游戏介绍 扫雷这个经典游戏,直到现在仍有很 ...

  7. 【综合评价分析】topsis评价 原理+完整MATLAB代码+详细注释+操作实列

    [综合评价分析]topsis评价 原理+完整MATLAB代码+详细注释+操作实列 文章目录 1.TOPSIS法的原理 2.TOPSIS法案例分析 3.建立模型并求解 3.1数据预处理 3.2代码实现数 ...

  8. 手写YOLOv3|代码详细注释

    手写YOLOv3|代码详细注释 一. 数据预处理 一. Yolov3网络 一. Train 一. Detection 源代码:https://github.com/eriklindernoren/Py ...

  9. 【题意分析】1044 Shopping in Mars (25 分)【滑动窗口】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Shopping in Mars is quite a different experience. The Mars people ...

最新文章

  1. Spring-依赖注入
  2. 计算机基础:离散数学和完备性
  3. 如何在Hybris commerce里创建一个media对象
  4. Android之AudioManager(音频管理器)详解
  5. 分布式系统认证方案_分布式系统认证方案_Spring Security OAuth2.0认证授权---springcloud工作笔记136
  6. java的继承和访问_Java基础篇:如何解决成员的访问和继承?
  7. HTML_简单JQ的AJAX响应式交互
  8. 游戏开发者如何有效的编写游戏策划文档
  9. gis属性表怎么导成excel_ArcGIS中将属性表直接导出为Excel的方法
  10. 多轮检索式对话——【ACL 2017】SMN
  11. JAVA300集——面向对象编程-类和对象-构造方法及其重载-方法调用
  12. oracle通过什么命令释放锁,oracle检测锁,释放锁
  13. 罗德里格斯公式 理解、推导
  14. python网页自动填写_Windows下使用python3 + selenium实现网页自动填表功能
  15. 以Java工程师为例,技术面试应该怎么准备?
  16. 重新安装键盘鼠标后,PS2键盘无法识别问题!
  17. html学习(06)-网页制作【学成在线】
  18. 如何将本地项目存入华为云
  19. 文秘与计算机的知识,18文秘02李荧 随着现代科学技术的发展,计算机已经
  20. 【MySQL】多表联合查询、连接查询、子查询

热门文章

  1. 猫眼电影字体反爬-自动处理字体加密
  2. 《时令绝杀技——向上汇报》
  3. 位运算符:按位取反(~)、位与()、位或(|)、位异或(^);左移运算符(<<)、右移运算符(>>)
  4. 【现代机器人学】学习笔记七:开链动力学(前向动力学Forward dynamics 与逆动力学Inverse dynamics)
  5. android 三星打印机,安卓逆袭传统 三星X4300LX复合机首测
  6. 谷歌重磅:可以优化自己的优化器!手动调参或将成为历史!?
  7. 微信图像接口 html,图像接口
  8. 线性代数:通过向量组个数和维数判别向量组线性相关性
  9. 浅谈利用NLG技术来进行游戏自动化(生成随机剧情随机对话)的可行性
  10. 2022年双十一洗面奶选购指南