codeforces 727F. Polycarp's problems
题目链接: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的更多相关文章
- Polycarp's problems
Polycarp's problems time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 659F Polycarp and Hay 并查集
链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...
- Codeforces 727 F. Polycarp's problems
Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslan ...
- Codeforces 723C. Polycarp at the Radio 模拟
C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- codeforces 723C : Polycarp at the Radio
Description Polycarp is a music editor at the radio station. He received a playlist for tomorrow, th ...
- [Codeforces 864B]Polycarp and Letters
Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s con ...
- Codeforces 913D - Too Easy Problems
913D - Too Easy Problems 思路:二分check k 代码: #include<bits/stdc++.h> using namespace std; #define ...
- Codeforces 861D - Polycarp's phone book
861D - Polycarp's phone book 思路:用map做的话,只能出现一次循环,否则会超时. 代码: #include<bits/stdc++.h> using name ...
- CodeForces 81D.Polycarp's Picture Gallery 乱搞
D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- LRU页面置换算法
本文以序列长度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及页面4:为例: #include <stdio.h> #define Init ...
- python走起之第六话
面向过程 VS 面向对象 编程范式 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大 ...
- React之JSX
0.对于学习React,我们先来熟悉下JSX的语法, 下面的这些语法仅用于构建一个组件的标签模块,定义完成之后如果需要做演示,请附加以下代码: ReactDOM.render( element1, d ...
- django_cms安装技巧
首先python的版本要高一些,否则安装django-cms会报错 安装cmsinstaller不能够正常下载 利用virtualenv进行安装配置 注意中文的配置 djangocms配置中文 dja ...
- 这是一个比较全的Android UI 组件
Android组件及UI框架大全 原文地址:http://blog.csdn.net/smallnest/article/details/38658593 Android 是目前最流行的移动操作系统 ...
- java面向对象的语言
对象:真实存在唯一的事物. 类: 实际就是对某种类型事物的共性属性与行为的抽取. 抽象的概念.... 车 我们从小的时候就不断的接触很多的对象,我们的大脑就会把 这些具备相同属性与行为的事物进行分类. ...
- Scope 安装和使用
Scope 安装和使用 一.安装 1. 软件下载 https://sourceforge.net/projects/cscope/files/ 2. 解压 3. 安装 ./configure --pr ...
- jsp中jstl标签的类似 if - else 语句 的语法
在jsp中引入jstl的标签库和函数库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c&q ...
- oracle 隐藏过长字段
case when length(m.topic)>20 then substr(m.topic,0,20)||'...' ...
- 0.读书笔记之The major advancements in Deep Learning in 2016
The major advancements in Deep Learning in 2016 地址:https://tryolabs.com/blog/2016/12/06/major-advanc ...