题目链接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. PHP调用webservice遇到 Soap WSDL Error - "failed to load external entity

    本人遇到的原因是服务器上没有安装php-soap centos安装方式: yum install php-soap 安装完成后重启 php-fpm 服务 service php-fpm restart ...

  2. js跳转

    window.location.reload(); //刷新当前页,参数保留

  3. 2.用vs2015创建Dotnet Core的mvc项目

    如果你正确安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2,将会看到 就是第二个选项 ASP.NET Core Web Application 选择第三个,第一次 ...

  4. Cheatsheet: 2016 11.01 ~ 11.30

    Web Getting Started With Vapor: A Swift Web Framework Front-end vs Back-end vs Network Performance S ...

  5. background-sizi (转)

    http://www.cnblogs.com/greenteaone/archive/2012/08/28/2659878.html  (原创作者链接地址  ) Background-Size:[ & ...

  6. 命令行用sublime打开当前目录

    执行    ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" subl 就可以在命令行用 ...

  7. Date Picker和UITool Bar的使用

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Verdana } span.s1 { } span.s2 { background-colo ...

  8. nslookup

    检查DNS http://arch.pconline.com.cn//pcedu/soft/wl/assist/10307/193330.html

  9. windows apache开启url rewrite

    加载Rewrite模块: 在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”, ...

  10. 使用Angularjs的ng-cloak指令避免页面乱码

    在使用Anguarjs进行web开发或者进行SPA(single page application)开发时,往往会遇到下面这样的问题. 刷新页面时,页面会出现一些乱码,这里的乱码具体是指`{{expr ...