Codeforces 994F Compute Power 二分+DP

题意:给n个任务 每个任务有两个值$a,b$ 现有许多机器 每台最多可以执行两次任务 若存在第二次任务则满足$a_{second}<a_{first}$ 定义代价$val = \frac { \sum_{i \in S } a[i]} { \sum_{i \in S} b[i] }$ 其中$S$为当做第一次来执行的任务的集合 求$val$的最小值
$n \leq 60,a_i \leq 10^8,b_i \leq 100$

很容易想到二分最小值并且联想到经典的$01$分数规划 即
$\frac {\sum_{i=1}^{n}a[i]}{\sum_{i=1}^{n}b[i]} \leq x$
${\sum_{i=1}^{n}a[i]} \leq x \times {\sum_{i=1}^{n}b[i]}$
${\sum_{i=1}^{n}a[i]}-x \times {\sum_{i=1}^{n}b[i]} \leq 0$
${\sum_{i=1}^{n} (a[i]-x \times b[i]) \leq 0}$

顺着这个思路 我们来设计$dp$方程
先将任务按$a$降序排序
$f[i][j][k]$表示做到第$i$个有$j$个大于$a_i$的任务是当做第一次来做且没有被安排第二次有$k$个等于$a_i$的任务是当做第一次来做且没有被安排第二次
转移式为
$f[i][j][k]=min(f[i+1][j-1][k]),f[i+1][j+1][k]+a_i-x \times b_i (a_i==a_{i+1})$
$f[i][j][k]=min(f[i+1][j+k-1][0]),f[i+1][j+k-1][0]+a_i-x \times b_i (a_i!=a_{i+1})$
$dp$之后只需要判定$f[0][0][0] \leq 0$是否满足来二分就好了

有个坑点是题目要求输出$ceil(ans \times 1000)$ 我用$google$翻译出来却是把$ans \times 1000$四舍五入 以后还是读英文题面好了

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define cl(x) memset(x,0,sizeof x)
#ifdef Poi
#define bug(x) cout<<(#x)<<" "<<(x)<<endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif
const int INF = 0x7fffffff;
const int N=55;
inline int read(){
int x=0,rev=0,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')rev=1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return rev?-x:x;
}
struct data{
double a,b;
}t[N];
double X,f[N][N][N];
bool vis[N][N][N];
int n;
ll ans;

bool cmp(data i,data j){
return i.a>j.a;
}
double dfs(int pos,int d,int g){
if(pos==n) return 0;
if(vis[pos][d][g]) return f[pos][d][g];
vis[pos][d][g]=1;
double mn=INF;
if(pos<n-1&&t[pos].a==t[pos+1].a) {
if(d) mn=min(mn,dfs(pos+1,d-1,g));
mn=min(mn,dfs(pos+1,d,g+1)+t[pos].a-X*t[pos].b);
}
else if(t[pos].a!=t[pos+1].a||pos==n-1){
if(d) mn=min(mn,dfs(pos+1,d+g-1,0));
mn=min(mn,dfs(pos+1,d+g+1,0)+t[pos].a-X*t[pos].b);
}
return f[pos][d][g]=mn;
}
bool judge(double k){
X=k,cl(vis),cl(f);
return dfs(0,0,0)<=0;
}

int main(){
#ifdef Poi
freopen("in.txt","r",stdin);
#endif
n=read();
for(int i=0;i<n;i++) t[i].a=read();
//  bug(t[n-1].a);
for(int i=0;i<n;i++) t[i].b=read();
if(t[0].a==99999991) {
//  for(int i=0;i<n;i++) cout<<t[i].b<<endl;
}
sort(t,t+n,cmp);
double l=0,r=1e8;
for(int T=1;T<=100;T++){
double mid=(l+r)/2.0;
//  bug(mid);
if(judge(mid)) r=mid;
else l=mid;
}
//  bug(l);
ans=(ceil)(l*1000);
//  printf("%.6lf\n",l);
cout<<ans<<endl;
}

原文地址:https://www.cnblogs.com/devil-gary/p/9194109.html

时间: 2024-07-30 16:56:58

Codeforces 994F Compute Power 二分+DP的相关文章

codeforces 289B - Polo the Penguin and Matrix 二分+dp

题意:给你一个序列,每一次可以对序列里面任意数+d 或者 -d 问你最少多少步能够使得数列里面所有的数相等 解题思路:从 1 - 10000 枚举这个数,二分找数列中小于等于它的最大的那个数,然后求前缀和以后刻意快速求出差值和的绝对值,差值和/d 就是我们所求数. 解题代码: 1 // File Name: 289b.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月29日 星期二 22时33分11秒 4 5 #include<vecto

二分+DP HDU 3433 A Task Process

HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1368    Accepted Submission(s): 684 Problem Description There are two kinds of tasks, namely A and B. There are N workers

Codeforces 360C Levko and Strings dp

题目链接:点击打开链接 题意: 给定长度为n的字符串s,常数k 显然s的子串一共有 n(n-1)/2 个 要求找到一个长度为n的字符串t,使得t对应位置的k个子串字典序>s #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #

【bzoj1044】[HAOI2008]木棍分割 二分+dp

题目描述 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长度最小, 并且输出有多少种砍的方法使得总长度最大的一段长度最小. 并将结果mod 10007... 输入 输入文件第一行有2个数n,m.接下来n行每行一个正整数Li,表示第i根木棍的长度.n<=50000,0<=m<=min(n-1,1000),1<=Li<=1000. 输出 输出有2个数,

CodeForces 30C Shooting Gallery 简单dp

题目链接:点击打开链接 给定n个气球 下面n行 x y t val 表示气球出现的坐标(x,y) 出现的时刻t,气球的价值val 枪每秒移动1个单位的距离 问: 射击的最大价值,开始时枪瞄准的位置任意. 思路: dp一下.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3

codeforces 149D - Coloring Brackets (区间dp)

题目大意: 给出一组合法的括号. 括号要么不涂颜色,要么就涂上红色或者绿色. 匹配的括号只能有一个有颜色. 两个相邻的括号不能有相同的颜色. 思路分析: 因为是一个合法的括号序列. 所以每个括号与之匹配的位置是一定的. 那么就可以将这个序列分成两个区间. (L - match[L] )  (match[L]+1, R) 用递归先处理小区间,再转移大区间. 因为条件的限制,所以记录区间的同时,还要记录区间端点的颜色. 然后就是一个递归的过程. #include <cstdio> #include

hdu1025 Constructing Roads In JGShining&amp;#39;s Kingdom(二分+dp)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities a

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,