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 ...
随机推荐
- error C2054:在“inline”之后应输入“(”
转自VC错误:http://www.vcerror.com/?p=79 问题描述: error C2054:在"inline"之后应输入"(" 按照编译错误的提 ...
- HttpURLConnection与HttpClient浅析AAAA
. GET请求与POST请求 HTTP协议是现在Internet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源. 在介绍HttpURLConnectio ...
- 分享18道Java基础面试笔试题(面试实拍)
上图来自Java技术栈微信群里的群友分享,看起来比较基础,但不一定人人都答得上来. 图片比较模糊,小编把题目进行了文字化. 1.你最常上的两个技术站和最常使用的两个app分別进什么?主要解决你什么需求 ...
- 20.multi_case02
# 多进程,使用Process对象 from multiprocessing import Process def f(name): print('hello', name) if __name__ ...
- <数据分析>初级入门
1.何为数据分析? 数据分析是指用适当的统计方法对收集来的大量数据进行分析,将它们加以汇总和理解消化,以求最大化地开发数据的功能,发挥数据的作用. 直接的理解:提炼杂乱无章的数据背后的信息,总结出研究 ...
- ABP Linq 扩展的 WhereIf 查询内部实现
public static class QueryableExtensions { public static IQueryable<T> WhereIf<T>(this IQ ...
- C#真他妈神奇,一个函数都不用写就能实现一个简单的邮件发送工具
MailMessage EmaillMessage = new MailMessage( //创建一个对象 new MailAddress(loning.Te ...
- Java之实现多线程
保证同步的几种方法: (1) 同步方法,synchronized 关键字修饰方法.由于Java中的每个对象都有一个内置锁,当用该关键词修饰时,内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则 ...
- spring:bean的作用范围和生命周期
bean的作用范围调整: <!--bean的作用范围调整 bean标签的scope属性: 作用:用于指定bean的作用范围 取值:常用的就是单例的和多例的 singleton:单例的(默认值) ...
- 【学术篇】洛谷1550——打井Watering Hole
题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...