PAT甲级——A1044 Shopping in Mars
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:
- 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).
- Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
- 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 (≤), the total number of diamonds on the chain, and M (≤), the amount that the customer has to pay. Then the next line contains N positive numbers D1⋯DN (Di≤103 for all ,) 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
> with (Di
+ ... + Dj
−) 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
【题意】
给出一个数字序列与一个数S,在数字序列中求出所有和值为S的连续子序列(区间下标左端点小的先输出,左端点相同时右端点小的先输出)。若没有这样的序列,求出和值恰好大于S的子序列(即在所有和值大于S的子序列中和值最接近S)。假设序列下标从1开始。
#include <iostream>
#include <vector>
using namespace std;
//暴力解法,复杂度为O(N),没通过测试,超时
int N, M;
vector<int>value,sum;
vector<pair<int, int>>res, minRes;//res存刚好切割值等于付款值,min存切割值为最小的
void Way1()
{
int minV = M;
for (int i = ; i <= N; ++i)
{
int s = ;
for (int j = i; j <= N; ++j)
{
s += value[j];
if (s < M)continue;
else if (s == M)
res.push_back(make_pair(i, j));
else if (s > M)
{
if (minV == (s - M))
minRes.push_back(make_pair(i, j));
else if (minV > (s - M))
{
minV = s - M;
minRes.clear();
minRes.push_back(make_pair(i, j));
}
}
break;
}
}
}
//方法二,使用二分法
int upper_bound(int L, int &tempS)
{
int left = L, right = N, mid;
while (left < right)
{
mid = (left + right) / ;
if (sum[mid]-sum[L-] >= M)
right = mid;
else
left = mid + ;
}
tempS = sum[right] - sum[L - ];
return right;
} void Way2()
{
int minV = sum[N], tempS;
for (int i = ; i <= N; ++i)//左端
{
int j = upper_bound(i, tempS);
if (tempS >minV)continue;
else if (tempS >= M)
{
if (tempS < minV)
{
res.clear();
minV = tempS;
}
res.push_back(make_pair(i, j));
}
}
} int main()
{
cin >> N >> M;
value.resize(N + );
sum.resize(N + );
for (int i = ; i <= N; ++i)
{
cin >> value[i];
sum[i] = sum[i - ] + value[i];
}
Way2();
if (res.size() > )
for (auto a : res)
cout << a.first << "-" << a.second << endl;
else
for (auto a : minRes)
cout << a.first << "-" << a.second << endl;
return ;
}
PAT甲级——A1044 Shopping in Mars的更多相关文章
- PAT 甲级 1044 Shopping in Mars
https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...
- PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay ...
- A1044. Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]
题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...
- PAT 甲级 1027 Colors in Mars (20 分)
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way a ...
- PAT 甲级 1027 Colors in Mars
https://pintia.cn/problem-sets/994805342720868352/problems/994805470349344768 People in Mars represe ...
- PAT 甲级 1027 Colors in Mars (20 分)(简单,进制转换)
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way ...
- PAT甲级——A1027 Colors in Mars
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- PAT甲级——1027 Colors in Mars
1027 Colors in Mars People in Mars represent the colors in their computers in a similar way as the E ...
随机推荐
- hdu多校第一场1003 (hdu6580)Milk 背包
题意: 有一个n*m的矩阵,左右可以随便走,但只能在每一行的中点往下走,每走一格花费时间1. 现在这个矩阵里放了k瓶牛奶,第i个牛奶喝下去需要ti时间 起点是(1,1) 对于每个i∈[1,k],问喝掉 ...
- PAT甲级——A1131 Subway Map【30】
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- JAVA工具包_BeanUtils
简介 大多数的java开发者通常在创建Java类的时候都会遵循JavaBean的命名模式,对类的属性生成getters方法和setters方法.通过调用相应的getXxx和setXxx方法,直接访问这 ...
- Java工具之NotePad++使用技巧
按住Alt键 拖动鼠标左键 批量添加 如,等 批量添加逗号, 下面, 竖排 变 横排 ctrl + f 使用正则表达式 \r\n 替换换行符 使用:sql语句中的 过滤条件 in中,往往适合范围查找 ...
- Python全栈开发:进度条
import sys import time for i in range(31): # 清空打印内容 sys.stdout.write("\r") # 控制输出样式 sys.st ...
- 记录openSUSE 源码安装node.js
openSUSE版本: 42.2 目标:安装好 Node.js v6.10.3 在终端中可以使用 "su" 命令,切换到root用户. 1. 安装 gcc,gcc-c++ zypp ...
- 2016.9.24初中部上午NOIP普及组比赛总结
2016.9.24初中部上午NOIP普及组比赛总结 2016.09.24[初中部 NOIP普及组 ]模拟赛 其实这次我没比赛,早上去参加亲子活动去了. 不过在下午我做完了所有的题,感觉还好. 进度 现 ...
- DRF 请求生命周期以及各模块解析
目录 rest_framework框架的封装特点 原生Django与DRF比较 APIView 的请求生命周期 请求模块(request) 解析模块(parser_classes) 异常模块(exce ...
- 单独安装Babel或者Less
1.直接安装Babel法: 1)初始化自动创建package.json npm init 2)首先全局安装Babel. npm install -g babel-cli 3)项目安装Babel. np ...
- React中的Ajax
React中的Ajax 组件的数据来源,通常是通过Ajax请求从服务器获取,可以使用componentDidMount方法设置Ajax请求,等到请求成功,再用this.setState方法重新渲染UI ...