Codeforces 639E - Bear and Paradox(二分+贪心)
原来 jxd 作业里也有我会做的题 i 了 i 了
首先这种题目的套路就是先考虑对于一个固定的 \(c\),怎样求出得分最高的策略,而类似于这样的问题都考虑贪心求解,手玩几组数据就能发现最优方案是将所有题目按照 \(\dfrac{p_i}{t_i}\) 从大到小排列。简单证明一下,考虑按照 P4437 排列的套路,假设有两道题 \(i,j\) 满足 \(\dfrac{p_i}{t_i}>\dfrac{p_j}{t_j}\),那么将 \(i\) 放在 \(j\) 前面的得分为 \(W_1=p_i(1-\dfrac{ct_i}{T})+p_j(1-\dfrac{c(t_i+t_j)}{T})\),将 \(i\) 放在 \(j\) 后面的得分为 \(W_2=p_j(1-\dfrac{ct_j}{T})+p_i(1-\dfrac{c(t_i+t_j)}{T})\),做差可得 \(\Delta=W_1-W_2=\dfrac{c(p_it_j-p_jt_i)}{T}\),而由 \(\dfrac{p_i}{t_i}>\dfrac{p_j}{t_j}\) 知 \(p_it_j-p_jt_i>0\),故 \(\Delta>0\),也就是说将 \(i\) 放在 \(j\) 前面最优,至于 \(\dfrac{p_i}{t_i}\) 相同的 \(i\),一定有 \(\Delta=0\),也就是说 \(\dfrac{p_i}{t_i}\) 相同的 \(i\) 可以随意交换位置。据说这套路还有个专门名字叫什么 Exchange arguments?不过名字啥的不重要辣,MO 里一般叫它调整法,反正这东西在 OI 和 MO 里都挺有用的就对了(
接下来考虑怎样求答案。首先这个 \(c\) 满足单调性,故考虑二分答案,这是题目中疯狂暗示的,再想不到就有些 sb 了罢(别打我)。考虑检验某个 \(c\) 是否合法,我们显然可以确定每道题可能被完成的最靠前的时间,以及每道题可能被完成的最靠后的时间。具体来说,我们将 \((p_i,t_i)\) 按从大到小顺序排序,记 \(sum_i\) 为对于排好序的 \(t_i\),\(\sum\limits_{j=1}^it_j\) 的值。考虑对于一段极长的区间 \([l,r]\) 满足 \(\forall x,y\in [l,r]\) 都有 \(\dfrac{p_x}{t_x}=\dfrac{p_y}{t_y}\),那么显然对于 \(i\in [l,r]\),问题 \(i\) 可能被完成的最靠前的时间为 \(sum_{l-1}+t_i\),最靠后的时间为 \(sum_r\)。因此我们只需检验是否 \(\exist i,j\) 满足 \(p_i>p_j\),完成 \(i\) 最少可能的得分 \(<\) 完成 \(j\) 最大可能的得分,这个可以通过再将所有题目按 \(p_i\) 排序并用 two pointers 维护 \(mx=\max\limits_{p_j<p_i}p_j(1-\dfrac{c·mxt_j}{T})\),其中 \(mxt_j\) 即为上文中所说的问题 \(i\) 可能的最靠后的完成时间。并与 \(i\) 在可能的最靠前的完成时间的得分比较即可。
时间复杂度 \(\mathcal O(n\log n)\)。
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define fill0(a) memset(a,0,sizeof(a))
#define fill1(a) memset(a,-1,sizeof(a))
#define fillbig(a) memset(a,63,sizeof(a))
#define pb push_back
#define ppb pop_back
#define mp make_pair
template<typename T1,typename T2> void chkmin(T1 &x,T2 y){if(x>y) x=y;}
template<typename T1,typename T2> void chkmax(T1 &x,T2 y){if(x<y) x=y;}
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned int u32;
typedef unsigned long long u64;
namespace fastio{
#define FILE_SIZE 1<<23
char rbuf[FILE_SIZE],*p1=rbuf,*p2=rbuf,wbuf[FILE_SIZE],*p3=wbuf;
inline char getc(){return p1==p2&&(p2=(p1=rbuf)+fread(rbuf,1,FILE_SIZE,stdin),p1==p2)?-1:*p1++;}
inline void putc(char x){(*p3++=x);}
template<typename T> void read(T &x){
x=0;char c=getchar();T neg=0;
while(!isdigit(c)) neg|=!(c^'-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(neg) x=(~x)+1;
}
template<typename T> void recursive_print(T x){if(!x) return;recursive_print(x/10);putc(x%10^48);}
template<typename T> void print(T x){if(!x) putc('0');if(x<0) putc('-'),x=~x+1;recursive_print(x);}
void print_final(){fwrite(wbuf,1,p3-wbuf,stdout);}
}
const int MAXN=1.5e5;
const double EPS=1e-9;
int n;ll T,sum[MAXN+5];
struct data{ll p,t,mn,mx;} a[MAXN+5];
bool cmp1(data lhs,data rhs){return lhs.p*rhs.t>rhs.p*lhs.t;}
bool cmp2(data lhs,data rhs){return lhs.p<rhs.p;}
bool check(double x){
double mx=0;
for(int i=1,j=1;i<=n;i++){
while(a[j].p!=a[i].p) chkmax(mx,1.0*a[j].p*(1-x*a[j].mn/T)),j++;
if(1.0*a[i].p*(1-x*a[i].mx/T)<mx) return 0;
} return 1;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i].p);
for(int i=1;i<=n;i++) scanf("%lld",&a[i].t),T+=a[i].t;
sort(a+1,a+n+1,cmp1);
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i].t;
for(int l=1,r;l<=n;l=r+1){
r=l;while(r<n&&a[r].p*a[r+1].t==a[r].t*a[r+1].p) r++;
for(int i=l;i<=r;i++) a[i].mx=sum[r],a[i].mn=sum[l-1]+a[i].t;
} sort(a+1,a+n+1,cmp2);
double l=0,r=1.0,x=-114514.1919810;
while(fabs(r-l)>EPS){
double mid=(l+r)/2.0;
if(check(mid)) x=l=mid;
else r=mid;
} printf("%.10lf\n",x);
return 0;
}
Codeforces 639E - Bear and Paradox(二分+贪心)的更多相关文章
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces 377B---Preparing for the Contest(二分+贪心)
C - Preparing for the Contest Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces C Match Points(二分贪心)
题目描述: Match Points time limit per test 2 seconds memory limit per test 256 mega bytes input standard ...
- codeforces 1251D Salary Changing (二分+贪心)
(点击此处查看原题) 题意分析 一共有s元钱,要用这些钱给n个人发工资,发给每个人的工资si有最少和最多限制 si ∈[li,ri],在发给n个人的总工资小于s的情况下,要求发给n个人中的工资的中位数 ...
- Codeforces Round #262 (Div. 2) 二分+贪心
题目链接 B Little Dima and Equation 题意:给a, b,c 给一个公式,s(x)为x的各个位上的数字和,求有多少个x. 分析:直接枚举x肯定超时,会发现s(x)范围只有只有1 ...
- CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
随机推荐
- linux:桌面切换
永久更改 字符模式:multi-user.target 图形模式:graphical.target systemctl get-default #查看默认模式 systemctl set-defaul ...
- kivy Label触发事件
kivy label也可以触发事件,为什么只有我这么无聊学垃圾kivy """ 在通过ref标记一段文本后点击这段文本就可以触发'on_ref_press'事件,在该事 ...
- Spring 5 MVC 中的 Router Function 使用
Spring 5 发行已经好几年了,里面提出了好几个新点子.其中一个就是 RouterFunction,这是个什么东西呢? Spring框架给我们提供了两种http端点暴露方式来隐藏servlet原理 ...
- Charles的简单用法
Charles的简单用法 一.抓电脑上 http 包 二.显示请求的 Request 和 Response 三.抓取电脑上 https 包 1.安装根证书 2.在钥匙串中启用根证书 3.配置哪些需要抓 ...
- elasticsearch的bulk(批量)操作
在es中我们可能会有这么一种需求,即有时需要批量向es中插入或更新或删除数据,如果一条一条数据的操作,那么速度必然很慢,那么es的bulk api就可以派上用场. delete 删除操作,只需要写一个 ...
- RAW RGB格式
RAW RGB格式 10bit Raw RGB, 就是说用10bit去表示一个R, G, 或者B, 通常的都是用8bit的. 所以你后面处理时要把它转换为8bit的, 比较简单的方法就是将低两位去掉, ...
- Python环境配置详细步骤以及第一个程序
打开python官网:https://www.python.org/ 在官网找与自己电脑系统匹配的版本路径 这里以python3.7.2版本为例: 下载完成后,使用管理员身份进行安装: 打开命令提 ...
- C++ string类型小结
目录 构造函数 string.append() string.assign() string.at() string.back() string.begin() string.capasity() s ...
- hdu 1502 Regular Words(DP)
题意: 一个单词X由{A,B,C}三种字母构成. A(X):单词X中A的个数.B(X),C(X)同理. 一个单词X如果是regular word必须满足A(X)=B(X)=C(X)且对于X的任意前缀有 ...
- 检查redis是否正常运行
[XX@XXX]$ ps -ef | grep redisXX 8047 1 0 10:06 ? 00:00:03 redis-server *:6379XX 9983 9802 0 11:2 ...