1044 Shopping in Mars(25 分)

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 (≤10​5​​), the total number of diamonds on the chain, and M (≤10​8​​), the amount that the customer has to pay. Then the next line contains N positive numbers D​1​​⋯D​N​​ (D​i​​≤10​3​​ 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

题目大意:给出一串数表示钻石的价值,并且给出m,如果有一连串钻石价值正好等于m,那么就输出这一串;如果没有正好相等的和,那么就输出那些>m,并且和m差值最小的!

//第一次提交这样,就是通过两层循环来做,

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int dio[];
int main() {
int n,m;
cin>>n>>m;
for(int i=;i<n;i++){
cin>>dio[i];
}
bool flag=false;
for(int i=;i<n;i++){
int sum=;
for(int j=i;j<n;j++){
sum+=dio[j];
if(sum==m){
flag=true;
cout<<i+<<'-'<<j+<<'\n';break;
}
}
}
vector<int> vt;
if(!flag){//如果没有解。
int mini=;
for(int i=;i<n;i++){
int sum=;
for(int j=i;j<n;j++){
sum+=dio[j];
if(sum>m&&sum-m<mini){
vt.clear();
vt.push_back(i+);
vt.push_back(j+);
//cout<<i+1<<"="<<j+1<<" "<<sum<<'\n';
mini=sum-m;break;
}else if(sum>m&&sum-m==mini){
vt.push_back(i+);
vt.push_back(j+);
// cout<<i+1<<"+"<<j+1<<'\n';
break;
}
}
}
}
for(int i=;i<vt.size();i+=){
cout<<vt[i]<<'-'<<vt[i+]<<'\n';
}
return ;
}

//但是提交到牛客网上,

运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。
case通过率为40.00%

//顿时感觉凉凉。提交到pat上,2,3,5测试点运行超时。

代码来自:https://www.liuchuo.net/archives/2939

#include <iostream>
#include <vector>
using namespace std;
vector<int> sum, resultArr;
int n, m;
void Func(int i, int &j, int &tempsum) {//这个求的是从i到j的和。
//表面上是left,但是while里的if条件仍然只是>=m
int left = i, right = n;
while(left < right) {
int mid = (left + right) / ;
if(sum[mid] - sum[i-] >= m)
right = mid;//必须是=mid,如果是mid-1,那很有可能和就不够了。
else
left = mid + ;
}
j = right;//传引用的话,是不用return的,
tempsum = sum[j] - sum[i-];
}
int main() {
scanf("%d%d", &n, &m);
sum.resize(n+);
for(int i = ; i <= n; i++) {
scanf("%d", &sum[i]);
sum[i] += sum[i-];//这是前缀和?
}
int minans = sum[n];
for(int i = ; i <= n; i++) {
int j, tempsum;
Func(i, j, tempsum);//直接传引用。
if(tempsum > minans) continue;//感觉这一句不用吧,肯定不会大于的。
//明白了,这个在i=1第一次循环的时候是没用,但是minans更新之后就有用了。
//判断>当前的都不选择。
if(tempsum >= m) {
if(tempsum < minans) {
resultArr.clear();
minans = tempsum;
}
resultArr.push_back(i);
resultArr.push_back(j);
}
}
for(int i = ; i < resultArr.size(); i += )
printf("%d-%d\n", resultArr[i], resultArr[i+]);
return ;
}

//学习了,不用直接去找是否有=m,或者没有等于的,都一样的方式去遍历查找

1.当程序正常超时时,需要考虑二分法。

2.使用引用传值,减少函数调用时间。

3.这里的mid求的不是到left的和,而是到i的和!和往常的不一样。

//这个二分我感觉确实挺难的,相不到,

代码来自:https://www.cnblogs.com/chenxiwenruo/p/6677802.html

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std; const int maxn=+;
int n,m;
int diamond[maxn];
struct Ans{
int i,j;
}; vector<Ans> ans;
int main()
{
scanf("%d %d\n",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&diamond[i]);
int minlost=INF;
Ans tmp;
int sum=;
int start=;
for(int i=;i<=n;i++){
sum+=diamond[i];
//printf("%d %d %d %d %d\n",start,i,sum,sum-m,minlost);
if(sum<m)
continue;
if(sum-m<=minlost){
if(sum-m<minlost){
ans.clear();
minlost=sum-m;
}
tmp.i=start;
tmp.j=i;
ans.push_back(tmp);
}
if(sum>=m && start<i){//这里是在做什么?
sum-=diamond[i];//明白了,-钻石i的值,和钻石start的值,
i--;//如果减去diamond[i],并且减去start的值,那么总和肯定小于m,那么
sum-=diamond[start];//肯定要就着i往下加了。
start++;//目前的sum肯定达不到m,所以肯定需要往后加。
printf("%d\n",sum);
}
}
for(int i=;i<ans.size();i++){
printf("%d-%d\n",ans[i].i,ans[i].j);
}
return ;
}

//这个也写得很好,但是我有点不太理解。

// 但是接着说上述二分方法的话,也就是原来二分,求和之后二分的方法。

PAT 1044 Shopping in Mars[二分][难]的更多相关文章

  1. PAT 1044. Shopping in Mars

    #include <cstdio> #include <cstdlib> #include <vector> #include <climits> #i ...

  2. PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)

    1044 Shopping in Mars (25 分)   Shopping in Mars is quite a different experience. The Mars people pay ...

  3. 1044 Shopping in Mars (25 分)

    1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...

  4. PAT 甲级 1044 Shopping in Mars

    https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...

  5. PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]

    题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...

  6. 1044 Shopping in Mars (25分)(二分查找)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  7. 1044 Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  8. PTA(Advanced Level)1044.Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  9. 1044 Shopping in Mars (25 分)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

随机推荐

  1. C# 哈希加密

    1.方法一: [c-sharp] view plaincopy //适用于C#语言 //使用前需导入以下命名空间:using System.Web.Security; //第一个参数为需加密的字符串, ...

  2. Windows之Xmanager连接linux打开Oracle视图操作

    前提:安装Xmanager 能够百度Xmanager下载其破解版或者带注冊机的版本号,也能够官网下载.只是须要秘钥(建议下载企业版) 官网下载地址:http://www.netsarang.com/d ...

  3. 关于直播学习笔记-004-nginx-rtmp、srs、vlc、obs

    1.采集端:OBS RTMP推流地址:rtmp://192.168.198.21:1935/live 流密钥:livestream(任意-但播放地址与此一致) 2.播放端:nginx-rtmp-win ...

  4. osgEarth2.8加载矢量数据描边效果

    通过修改osgearth自带的agglite插件,实现矢量描边效果,可以自定义描边的颜色和宽度(单位像素) 测试文件osgearth_features.cpp #include <osg/Not ...

  5. linux系统socket通信编程2

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  6. 说说FATFS文件系统(转)

    FATFS是一个为小型嵌入式系统设计的通用FAT(File Allocation Table)文件系统模块.FatFs 的编写遵循ANSI C,并且完全与磁盘I/O层分开.因此,它独立(不依赖)于硬件 ...

  7. 《转》python学习(6)序列类型-字符串

    转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125502.html 二.序列类型 包含字符串.列表.元祖.模式都一样,举一反三即可.如 ...

  8. 【BZOJ4524】[Cqoi2016]伪光滑数 堆(模拟搜索)

    [BZOJ4524][Cqoi2016]伪光滑数 Description 若一个大于1的整数M的质因数分解有k项,其最大的质因子为Ak,并且满足Ak^K<=N,Ak<128,我们就称整数M ...

  9. [算法] N 皇后

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  10. iOS 根据经纬度计算与地理北极夹角

    http://www.aiuxian.com/article/p-2767848.html #define toDeg(X) (X*180.0/M_PI) /**  * @method 根据两点经纬度 ...