题目链接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. Unity仪表盘显示UGUI制作小心得

    最近在做设备仪表参数参数显示,由于模型摆放位置经常修改,加之要求不能在模型的下面添加东西,显示界面的位置也不得不跟着修改,一来二去就烦了,想了解决办法,现在总结如下: 1.仍然在模型下面新建Panel ...

  2. Zabbix3.x安装图解教程

    准备知识: Zabbix3.x比较之前的2.0界面有了很大的变化,但是安装部署过程与2.x基本完全一样. 1.Zabbix2.x安装图解教程 http://www.osyunwei.com/archi ...

  3. 存储过程里面使用in变量列表异常的处理

    在写一个存储过程的时候,由于需要用到类似:select id,name from tablename where id in(id1,id2,id3...)的查询语句,同时括号里面的变量是拼接得到的, ...

  4. [OC] autoLayout 博客文档

    tip :记录几个博客文档 iOS 8 Auto Layout界面自动布局系列5-自身内容尺寸约束.修改约束.布局动画 iOS AutoLayout自动布局中级开发教程(5)-修改约束的值,延迟加载 ...

  5. ElasticSearch与Spring Boot集成问题

    1.None of the configured nodes are available 或者 org.elasticsearch.transport.RemoteTransportException ...

  6. Myeclipse出现 java文件中文乱码问题

    一.将整个project设置编码UTF-8(UTF-8可以最大的支持国际化) windows->Preferences->general->Workspace->Text fi ...

  7. img的空白符

    在div里面嵌套了一个img出来问题,div的高度超过了img的高度,大约3px ,查看了样式没什么问题,后来百度了一下,找到了2种的方法都有效: 第一种:img{display:block} 第二种 ...

  8. Predicate接口和Consumer接口

    Predicate和Consumer接口是集合操作中常用到的俩个接口: 1.Predicate public interface Predicate<T>{ boolean test(T ...

  9. JQuery_DOM 节点操作之包裹节点

    jQuery 提供了一系列方法用于包裹节点,那包裹节点是什么意思呢?其实就是使用字符串代码将指定元素的代码包含着的意思. <script type="text/javascript&q ...

  10. Goppa code

    上面的公式定义了长度为n的Goppa码[1].n=2^m, 其维度 k≥n- t·m. 最小距离d≥ 2t+1. 存在运行时间与 n·t 成正比的快速译码算法. 从形式上看,右边是分式,相当于线性分组 ...