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 ...
随机推荐
- js 定位到某个锚点的方法
html页面内可以设置锚点,锚点定义 Html代码 ? 1 <a name="firstAnchor">&nsbp;</a> 锚点使用 Html代 ...
- 为什么程序员都不喜欢使用switch,而是大量的 if……else if ?
作者:熊爸爸 原文:http://3g.163.com/tech/article/E02RDE6C0511SDDL.html 请用5秒钟的时间查看下面的代码是否存在bug. OK,熟练的程序猿应该已经 ...
- 19.SimLogin_case03
# 模拟登录GitHub import requests from lxml import etree class Login(): def __init__(self): self.headers ...
- Python flask 构建可扩展的restful apl✍✍✍
Python flask 构建可扩展的restful apl 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课 ...
- [课后作业] 第002讲:用Python设计第一个游戏 | 课后测试题
试题: 0. 什么是BIF? 1. 用课堂上小甲鱼教的方法数一数 Python3 提供了多少个 BIF? 2. 在 Python 看来:'FishC' 和 'fishc' 一样吗? 3. 在小甲鱼看来 ...
- Keywords Search HDU2222 AC自动机模板题
ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...
- LJJ爱数数
LJJ爱数数 求\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\epsilon(gcd(i,j,k))(\frac{1}{i}+\frac{1}{j}==\frac{1} ...
- 洛谷P4027 [NOI2007]货币兑换
P4027 [NOI2007]货币兑换 算法:dp+斜率优化 题面十分冗长,题意大概是有一种金券每天价值会有变化,你可以在某些时间点买入或卖出所有的金券,问最大收益 根据题意,很容易列出朴素的状态转移 ...
- Zuul的过滤器
过滤器类型与请求生命周期: Zuul中定义了4种标准过滤器类型,这些过滤器类型对应于请求的典型生命周期 PRE: 这种过滤器在请求被路由之前调用.可利用这种过滤器实现身 ...
- NSLayoutConstraint 开源框架
https://github.com/cloudkite/Masonry Masonry is a light-weight layout framework which wraps AutoLayo ...