poj 3266 Cow School 分数规划
这个题目难度非常大,首先对于老师的一种方案,应用分数规划的一般做法,求出所有的c=t-rate*p,如果没有选择的c值中的最大值比选择了的c值中的最小值大,那么这个解是可以改进的。
那么问题就转化成了怎么求最小的c和最大的c。
t-rate*p 求这种类型的最值,并且rate是单调的,那么就可以考虑利用斜率优化的那种办法来维护决策点。
考虑两个决策点,得到ti-tj>rate(pi-pj) 但是这个pi pj的大小不能确定,我们知道可以利用斜率优化的问题不仅仅要rate单调,还需要pi 单调 这个时候我们需要利用题目中的条件,题目中保证了t/p单调,根据这个条件,可以推出求两种最值的时候都只有单调的p才是有可能成为决策点的。那么就可以按照斜率优化步骤来解题了。一个是用单调栈维护,另外一个利用单调队列维护。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=5e4+9;
double high[maxn],low[maxn];
long long sumt[maxn],sump[maxn];
struct D
{
long long t,p;
bool operator <(const D & xx) const
{
return t*xx.p>xx.t*p;
}
}test[maxn];
int que[maxn]; bool chk(int i,int j,int t,int s)
{
long long a=(test[i].t-test[j].t)*(test[t].p-test[s].p);
long long b=(test[t].t-test[s].t)*(test[i].p-test[j].p);
return a>b;
} bool chk2(int i,int j,long long t,long long p)
{
long long a=(test[i].t-test[j].t)*p;
long long b=t*(test[i].p-test[j].p);
return a>b;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%lld %lld",&test[i].t,&test[i].p);
sort(test+1,test+1+n); for(int i=1;i<=n;i++)
{
sumt[i]=sumt[i-1]+test[i].t;
sump[i]=sump[i-1]+test[i].p;
} int front=1,end=0;
for(int i=1;i<=n;i++)
{
while(end>=front&&test[i].p>=test[que[end]].p)
end--;
while(end>front&&chk(que[end],i,que[end-1],que[end]))
end--;
que[++end]=i;
while(front<end&&chk2(que[front],que[front+1],sumt[i],sump[i])==1)
front++;
int u=que[front];
low[i]=test[u].t-(double)sumt[i]/sump[i]*test[u].p;
}
int top=0;
for(int i=n;i>=1;i--)
{
while(top>0&&test[i].p<=test[que[top]].p)
top--;
while(top>1&&chk(i,que[top],que[top],que[top-1]))
top--;
que[++top]=i;
while(top>1&&chk2(que[top],que[top-1],sumt[i-1],sump[i-1])==0)
top--;
int u=que[top];
high[i]=test[u].t-(double)sumt[i-1]/sump[i-1]*test[u].p;
}
int ans=0;
for(int i=1;i<n;i++)
if(high[i+1]>low[i])
ans++;
cout<<ans<<endl;
for(int i=n-1;i>=1;i--)
if(high[i+1]>low[i])
printf("%d\n",n-i);
}
return 0;
}
poj 3266 Cow School 分数规划的更多相关文章
- Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...
- POJ 2728 JZYZOJ 1636 分数规划 最小生成树 二分 prim
http://172.20.6.3/Problem_Show.asp?id=1636 复习了prim,分数规划大概就是把一个求最小值或最大值的分式移项变成一个可二分求解的式子. #include< ...
- poj 3621 0/1分数规划求最优比率生成环
思路:以val[u]-ans*edge[i].len最为边权,判断是否有正环存在,若有,那么就是ans小了.否则就是大了. 在spfa判环时,先将所有点进队列. #include<iostrea ...
- poj Dropping tests 01分数规划---Dinkelbach算法
果然比二分要快将近一倍.63MS.二分94MS. #include <iostream> #include <algorithm> #include <cstdio> ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- POJ 2976 Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions:17069 Accepted: 5925 De ...
- P2877 [USACO07JAN]牛校Cow School(01分数规划+决策单调性分治)
P2877 [USACO07JAN]牛校Cow School 01分数规划是啥(转) 决策单调性分治,可以解决(不限于)一些你知道要用斜率优化却不会写的问题 怎么证明?可以暴力打表 我们用$ask(l ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
随机推荐
- Code one 码
Code one是一种用成像设备识别的矩阵式二维条码.Code one符号中包含可由快速性线性探测器识别的识别图案.每一模块的宽和高的尺寸为X. Code one符号共有10种版本及时14种尺寸.最 ...
- Windows SDK笔记(经典--一定要看)
Windows SDK笔记(一):Windows程序基本结构 一.概述 Windows程序具有相对固定的结构,对编写者而言,不需要书写整个过程,大部分过程由系统完成.程序中只要按一定的格式填写系统留给 ...
- HDoj-1527-取石子游戏
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- stm32之GPIO
stm32有5组GPIO口,GPIOA GPIOB GPIOC GPIOD GPIOE 每个GPIO端口有: 2个配置寄存器GPIOx_CRL, GPIOx_CRH(32位): 2个数据寄存器GPIO ...
- tomcat 系统服务 outofmemory
TOMCAT内存溢出outofmemory的问题: http://hi.baidu.com/mefeng47/item/3b247af74ce4e24e922af2e5 注:双击tomcat6w.ex ...
- git 使用过程(一、准备工作)
没有多余的废话,只是纯粹的捞干的记录下一自己使用Git的过程.(正在学习中,也是刚接触,记录过程可能有点乱.等熟悉之后在重新整理一下) 一.先下载git客户端 网址:http://msysgit.gi ...
- AsyncQueryHandler处理数据
参考:http://blog.csdn.net/hfreeman2011/article/details/8555474和http://blog.csdn.net/dragondog/article/ ...
- 用QT创建WINDOWS服务程序
恩, qtservice挺好的http://www.qtsoftware.com/products/appdev/add-on-products/catalog/4/Utilities/qtservi ...
- 执行Git命令时出现 SSL certificate problem 的解决办法
比如我在windows下用git clone gitURL 就提示 SSL certificate problem: self signed certificate 这种问题,在windows下出现 ...
- mongoose 数据库操作 - 分页
使用mongoose 加入分页方法,临时还没发现什么更好的方法,我使用的方法是,直接在源代码中加入 找到 node_modules/mongoose/lib/model.js打开这个文件.里面加入这段 ...