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<int, int> > cuts;

    for (int i=0; i<N; i++) {
        scanf("%d", &nums[i]);
    }
    int p = 0;
    int q = 0;

    int csum = nums[0];
    int lsum = INT_MAX;

    while (p<=q && q < N) {
        if (csum < M) {
            if (q+1 >= N) {
                break;
            }
            csum += nums[++q];
        } else if (csum == M) {
            if (lsum > csum) {
                cuts.clear();
            }
            lsum = csum;
            cuts.push_back(make_pair(p, q));
            if (q+1 >= N) {
                break;
            }
            csum -= nums[p++];
            csum += nums[++q];
        } else {
            if (csum < lsum) {
                cuts.clear();
                lsum = csum;
                cuts.push_back(make_pair(p, q));
            } else if (csum == lsum) {
                cuts.push_back(make_pair(p, q));
            }
            if (p == q && (q + 1 < N)) {
                csum += nums[++q];
            }
            csum -= nums[p++];
        }
    }
    sort(cuts.begin(), cuts.end());
    int len = cuts.size();

    for (int i=0; i<len; i++) {
        printf("%d-%d\n", cuts[i].first + 1, cuts[i].second + 1);
    }
    return 0;
}

一根爬虫

时间: 2024-10-17 18:27:58

PAT 1044. Shopping in Mars的相关文章

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

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甲级】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 A1044 Shopping in Mars [二分]

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

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

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

PAT1044. 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 take

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