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 ...
随机推荐
- (一)PHP基础知识考察点
1,PHP引用变量的考察点: 概念:引用就是用不同的名字访问同一个变量内容. 定义方式: 使用&符号. PHP引用变量的工作原理 这里有个COW copy on write 用zval() ...
- IEnumerable_vs_IEnumerator
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- JSON关联属性转换异常
问题:FastJSON在转换对象过程中,该对象还有关联属性,该属性还是一个对象,就出现栈溢出异常,会报一下错误,解决办法:在该属性类的一边加上@JSONField(serialize=false);有 ...
- 创建文件夹、新建txt文件
1.创建文件夹 QString myMkdir(QString path, QString floderName) //参数 path,创建的文件夹所在路径: 参数floderName,所创建的文件 ...
- 2019 Multi-University Training Contest 6 Nonsense Time (纯暴力)
题意:给你一个n的排列,起初这些数都不能用, 然后还有一个数组 第 i 个数表示下标为 i 的数能够使用. 问每一个 i 对应的最长上升子序列. 题解: 可以通过倒推,从后往前考虑转化一下 ,然后就是 ...
- 激活office2016的心路历程
先转换成VOL版本 32位的office2016用如下代码 @echo off :ADMIN openfiles >nul >nul ||( echo Set UAC = CreateOb ...
- ORM下实现继承的三种方式(TPH TPC TPT)
TPH(Table Per Hierarchy):所有的数据都放在同一个表格内,但是使用辨别标志(Discriminator)的方式来区分 TPC(Table Per Concrete-Type):由 ...
- Activiti添加批注(comment)信息
在每次提交任务的时候需要描述一些批注信息,例如:请假流程提交的时候要描述信息为什么请假,如果领导驳回可以批注驳回原因等 1.添加批注 // 由于流程用户上下文对象是线程独立的,所以要在需要的位置设置, ...
- linux及windows安装maven
一.linux安装maven 1.wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6. ...
- MATLAB 去掉数组里面不要的元素
trainfinger=1 testingfinger=(1:8); testingfinger = testingfinger(~ismember(testingfinger,trainfinger ...