zoukankan      html  css  js  c++  java
  • Polycarp's problems

    Polycarp's problems
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Polycarp is an experienced participant in Codehorses programming contests. Now he wants to become a problemsetter.

    He sent to the coordinator a set of n problems. Each problem has it's quality, the quality of the i-th problem is ai (ai can be positive, negative or equal to zero). The problems are ordered by expected difficulty, but the difficulty is not related to the quality in any way. The easiest problem has index 1, the hardest problem has index n.

    The coordinator's mood is equal to q now. After reading a problem, the mood changes by it's quality. It means that after the coordinator reads a problem with quality b, the value b is added to his mood. The coordinator always reads problems one by one from the easiest to the hardest, it's impossible to change the order of the problems.

    If after reading some problem the coordinator's mood becomes negative, he immediately stops reading and rejects the problemset.

    Polycarp wants to remove the minimum number of problems from his problemset to make the coordinator's mood non-negative at any moment of time. Polycarp is not sure about the current coordinator's mood, but he has m guesses "the current coordinator's moodq = bi".

    For each of m guesses, find the minimum number of problems Polycarp needs to remove so that the coordinator's mood will always be greater or equal to 0 while he reads problems from the easiest of the remaining problems to the hardest.

    Input

    The first line of input contains two integers n and m (1 ≤ n ≤ 750, 1 ≤ m ≤ 200 000) — the number of problems in the problemset and the number of guesses about the current coordinator's mood.

    The second line of input contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the qualities of the problems in order of increasing difficulty.

    The third line of input contains m integers b1, b2, ..., bm (0 ≤ bi ≤ 1015) — the guesses of the current coordinator's mood q.

    Output

    Print m lines, in i-th line print single integer — the answer to the problem with q = bi.

    Example
    input
    6 3
    8 -5 -4 1 -7 4
    0 7 3
    output
    2
    0
    1
    分析:dp,考虑从后往前转移;
       dp[i][j]表示从后往前i个数去掉了j个数时所需的最小初始b值;
          dp1[i]表示若去掉i个数,则所需最小的初始b值;
       转移方程为:
       dp[i][j]=min(dp[i][j],max(0LL,dp[i-1][j]-a[i]));//当前数保留;
       dp[i][j+1]=min(dp[i][j+1],dp[i-1][j]);//当前数删除;
       最后二分处理询问;

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define llinf 0x3f3f3f3f3f3f3f3fLL
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<ll,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn],ans;
    ll dp[maxn][maxn],dp1[maxn];
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)scanf("%d",&a[i]);
        memset(dp,llinf,sizeof dp);
        dp[0][0]=0LL;
        for(i=n;i>=1;i--)
        {
            for(j=0;j<n-i+1;j++)
            {
                dp[n-i+1][j]=min(dp[n-i+1][j],max(0LL,dp[n-i][j]-a[i]));
                dp[n-i+1][j+1]=min(dp[n-i+1][j+1],dp[n-i][j]);
            }
        }
        dp1[n]=0;
        for(i=n-1;i>=0;i--)dp1[i]=max(dp1[i+1],dp[n][i]);
        rep(i,1,m)
        {
            ll op;
            scanf("%lld",&op);
            ans=lower_bound(dp1,dp1+n+1,op,greater<ll>())-dp1;
            printf("%d
    ",ans);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    XML传输数据导致的安全问题
    XML的学习
    docker的笔记
    对于glog中ShutdownGoogleLogging后不能再次InitGoogleLogging问题的解决办法
    游戏分类英文缩写
    带属性的向前声明:warning: type attributes are honored only at type definition
    linux GCC编译错误:CPU you selected does not support x8664 instruction set
    电商数据分析
    日常电脑软件推荐清单
    优秀开源组合:助你快速研发
  • 原文地址:https://www.cnblogs.com/dyzll/p/5965672.html
Copyright © 2011-2022 走看看