PAT A1044 Shopping in Mars [二分]

题目描述

链接
求一串的数字中连续的一段,使得这个连续的段内数字的和恰好等于所期望的值m。如果不能找到恰好等于,就找让自己付出最少的价格(总和必须大于等于所给值)的那段区间。求所有可能的结果

分析

  • 输出区间和等于指定值的方案,可以先统计前缀和,然后作差就可以得到区间和
  • 原本错误的做法:作差得到区间和,保存在node数组里面,然后再排序,然后再二分查找。实际没用,因为最大的复杂度是\(O(n^2)\)
  • 正解应该是:因为本来前缀和就是递增的,可以枚举区间左端点,再二分查找区间右端点,使得区间和=m,复杂度\(O(nlogn)\)
//从错误做法里面还学到了内存超限的解决方法,用resize精确控制
#include<bits/stdc++.h>
using namespace std;

int n,m;
int a, tmp;
struct node{
    int l,r;
    int sum;
};
vector<node> rec;
vector<int> b;
bool cmp(node a, node b){
    if(a.sum != b.sum) return a.sum < b.sum;
    return a.l < b.l;
}

int main(){
    cin>>n>>m;
    b.resize(n+1); //解决内存超限
    for(int i=1;i<=n;i++){
        cin>>a;
        tmp += a;
        b[i] = tmp;
    }
    for(int i=0;i<=n;i++){
        for(int j=i;j<=n;j++){
            node tmp = {i+1,j,b[j]-b[i]};
            rec.push_back(tmp);
        }
    }
    sort(rec.begin(),rec.end(),cmp);
    int l = 0,r = rec.size();
    while(l<r){
        int mid = (l+r)>>1;
        if(rec[mid].sum >= m) r = mid;
        else l = mid + 1;
    }
    if(rec[l].sum > m){
        int delta = rec[l].sum - m;
        while(rec[l].sum-m == delta){
            cout<<rec[l].l<<"-"<<rec[l].r<<endl;
            l++;
        }
    }else{
        while(rec[l].sum == m){
            cout<<rec[l].l<<"-"<<rec[l].r<<endl;
            l++;
        }
    }

}

原文地址:https://www.cnblogs.com/doragd/p/11311868.html

时间: 2024-11-09 02:45:52

PAT A1044 Shopping in Mars [二分]的相关文章

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

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

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<

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

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

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

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

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 A1027 Colors in Mars

PAT A1027 Colors in Mars 题目描述: People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and