PAT-1044. Shopping in Mars (25)

这道题木意思是给你一个连续的序列,要求找出其中连续和为给定值的一段序列,如果不存在这样的序列,那么就输出值大于给定值且最接近的这样值的一个序列。

题目数据量是10^5,如果用N*2复杂度肯定会超时,所以我采用的是On算法。但是在求和少于给定值的过程中所需的步骤不确定。总体效果还算不错,最大时间是39ms。

自己挖了一坑。。。

#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;

struct Pair
{
int x;
int y;
Pair(int x,int y)
{
this->x=x;
this->y=y;
}
};
vector<Pair> v;
int main()
{
int n,m;
int start=1;
int data[100100];
int sum=0;
freopen("1044.txt","r",stdin);
int dis=0xffffffff>>1;
while(scanf("%d%d",&n,&m)!=EOF)
{
bool flag=false;
for(int i=1;i<=n;i++)
{
scanf("%d",&data[i]);
sum+=data[i];
while(sum>m)
{
if(sum>m&&sum-m<dis)
{
dis=sum-m;
v.clear();
v.push_back(Pair(start,i));
}
else if(sum>m&&sum-m==dis)
{
v.push_back(Pair(start,i));
}
sum-=data[start];  //先进行减法,然后再判断,导致两个数据不正确。
start=start+1;
}
if(sum==m)
{
printf("%d-%d\n",start,i);
sum-=data[start];
start=start+1;
flag=true;
}
}
if(!flag)
{
for(int i=0;i<v.size();i++)
{
printf("%d-%d\n",v[i].x,v[i].y);
}
}
}
return 0;
}
时间: 2024-11-09 06:51:28

PAT-1044. Shopping in Mars (25)的相关文章

1044. Shopping in Mars (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 cu

【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)

题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[100007];int sum[100007];vector<pair<int,int> >ans;int m

PAT 1044. Shopping in Mars

#include <cstdio> #include <cstdlib> #include <vector> #include <climits> #include <algorithm> using namespace std; int main() { int N, M; scanf("%d%d", &N, &M); vector<int> nums(N); vector<pair<

pat1044. Shopping in Mars (25)

1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

A1044. 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 o

1044. 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 o

PAT A1044 Shopping in Mars [二分]

题目描述 链接 求一串的数字中连续的一段,使得这个连续的段内数字的和恰好等于所期望的值m.如果不能找到恰好等于,就找让自己付出最少的价格(总和必须大于等于所给值)的那段区间.求所有可能的结果 分析 输出区间和等于指定值的方案,可以先统计前缀和,然后作差就可以得到区间和 原本错误的做法:作差得到区间和,保存在node数组里面,然后再排序,然后再二分查找.实际没用,因为最大的复杂度是\(O(n^2)\) 正解应该是:因为本来前缀和就是递增的,可以枚举区间左端点,再二分查找区间右端点,使得区间和=m,

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: 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 diam

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 o