题目链接http://codeforces.com/contest/727/problem/F 
题目大意:有n个问题,每个问题有一个价值ai,一开始的心情值为q,每当读到一个问题时,心情值将会加上该问题的价值。问题只能按顺序读。有m个询问,求当q=bi时,至少要删去多少个问题才能使得在任何时候心情值都>=0

参考这篇:http://blog.csdn.net/aufeas/article/details/53031439

解法一: 贪心

分别求出最多删去i个问题需要的初始心情值的最小值f[i],最后在f数组上二分 求解答案。

利用贪心暴力计算f[i], 即如果当前心情小于0,就去掉 价值最小的问题。

时间复杂度感人。。O(n^2*logn*log10^12+m*logn)

我一开始用了set,一直TLE, 改成priority_queue就过了。

代码:

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std; #define N 800
typedef long long ll; int n,m;
int a[N];
ll b[N];
set<int> st; bool Check(ll lim,int k)
{
priority_queue<int,vector<int>,greater<int> > q;
int cnt=;
for (int i=;i<=n;i++)
{
if (a[i]>=) lim+=a[i];
else
{
q.push(a[i]); lim+=a[i];
if (lim<)
{
lim-=q.top(); cnt++;
q.pop();
if (cnt>k) return false;
}
}
}
return true;
} ll Calc(int k)
{
ll L=,R=1e12,Mid;
while (L<R)
{
Mid=(L+R)>>;
if (Check(Mid,k)) R=Mid;
else L=Mid+;
}
return L;
} int Solve(ll lim)
{
int L=,R=n,Mid;
while (L<R)
{
Mid=(L+R)>>;
if (b[Mid]<=lim) R=Mid;
else L=Mid+;
}
return L;
} int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]); for (int i=;i<=n;i++) b[i]=Calc(i); ll lim;
for (int i=;i<=m;i++)
{
scanf("%I64d",&lim);
printf("%d\n",Solve(lim));
} return ;
}

解法二: DP

这个dp有点巧妙,是从后往前的顺序。

dp[i][j]表示考虑i-n这些问题,最多只能去掉j个问题, 初始心情至少要多少。   如果求出dp数组,最后只要在dp[1]上二分求答案就好了。

如果a[i]>=0, 那么a[i]这个问题肯定没必要删掉, dp[i][j]=dp[i+1][j]-a[i];

如果a[i]<0 , dp[i][j]=min(dp[i+1][j]-a[i] ,  dp[i+1][j-1]);  分别是删掉a[i]和不删的情况。

如果算出来dp[i][j]<0, 那么dp[i][j]=0.    因为要保证中间过程不会心情有负的情况。

代码:

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std; #define N 800
typedef long long ll; int n,m;
int a[N];
ll dp[N][N]; int Solve(ll lim)
{
int L=,R=n,Mid;
while (L<R)
{
Mid=(L+R)>>;
if (dp[][Mid]<=lim) R=Mid;
else L=Mid+;
}
return L;
} int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]); for (int i=n;i>=;i--)
{
for (int j=;j<=n-i+;j++)
{
if (a[i]<)
{
if (j) dp[i][j]=min(dp[i+][j]-a[i],dp[i+][j-]);
else dp[i][j]=dp[i+][j]-a[i];
}
else dp[i][j]=dp[i+][j]-a[i];
if (dp[i][j]<) dp[i][j]=; }
} ll lim;
for (int i=;i<=m;i++)
{
scanf("%I64d",&lim);
printf("%d\n",Solve(lim));
} return ;
}

 

codeforces 727F. Polycarp's problems的更多相关文章

  1. Polycarp's problems

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

  2. Codeforces 659F Polycarp and Hay 并查集

    链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...

  3. Codeforces 727 F. Polycarp's problems

    Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslan ...

  4. Codeforces 723C. Polycarp at the Radio 模拟

    C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  5. codeforces 723C : Polycarp at the Radio

    Description Polycarp is a music editor at the radio station. He received a playlist for tomorrow, th ...

  6. [Codeforces 864B]Polycarp and Letters

    Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s con ...

  7. Codeforces 913D - Too Easy Problems

    913D - Too Easy Problems 思路:二分check k 代码: #include<bits/stdc++.h> using namespace std; #define ...

  8. Codeforces 861D - Polycarp's phone book

    861D - Polycarp's phone book 思路:用map做的话,只能出现一次循环,否则会超时. 代码: #include<bits/stdc++.h> using name ...

  9. CodeForces 81D.Polycarp's Picture Gallery 乱搞

    D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

随机推荐

  1. Git命令行下解决冲突

    使用Git时,在pull.merge.rebase的过程中,经常会遇到conflict的情况. 遇到conflict时,以上处理过程会终端,并且命令行中显示(xxx|MERGING)的状态(Windo ...

  2. test lab ~ triangle test by using junit and coverage

    first set up a new folder as your test class place, and then let your package in test class folder b ...

  3. miniprofiler的对数据库的监测使用。以nancy,petapoco为例

    miniprofiler的使用 miniprofiler的详细介绍请看这里http://miniprofiler.com/.(可以对数据库和页面等监控如 ado ef mvc mongodb) 本文以 ...

  4. LaTeX用dvi编译,Yap浏览器弹出对话框,决解办法(傻瓜教程)

      1,打开windows-----所有运用,找到CTEX的文件目录 2,打开Previewer对话框 打开后如图: 2,选择view – option 打开后如下图: 3,选择Display---- ...

  5. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  6. Modernizr.js:为HTML5和CSS3而生!

    原文链接:http://caibaojian.com/modernizr-js.html modernizr这个JS,在国外的主题里面很多地方都看到,就只记得是为html补充的,有点类似与respon ...

  7. EasyUI的combobox控件使用onchange 问题

    在项目中几次都遇到了同样的问题,现在都不知道怎样解决了! 路过的朋友们帮我看看嘛!谢谢了! 最后我想要实现的效果是这样的.   在下拉列表中不存在值.(这里的是下拉列表中存在值的!)  但是在我输入值 ...

  8. javascript正则表达式

    引用:http://www.jb51.net/article/72192.htm

  9. VB检测按键CTRL+C的次数

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As IntegerPriv ...

  10. IP首部校验和的计算

    ip抓包结果:0000: 00 e0 0f 7d 1e ba 00 13 8f 54 3b 70 08 00 45 00 0010: 00 2e be 55 00 00 7a 11 51 ac de ...