这个题目难度非常大,首先对于老师的一种方案,应用分数规划的一般做法,求出所有的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 分数规划的更多相关文章

  1. Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...

  2. POJ 2728 JZYZOJ 1636 分数规划 最小生成树 二分 prim

    http://172.20.6.3/Problem_Show.asp?id=1636 复习了prim,分数规划大概就是把一个求最小值或最大值的分式移项变成一个可二分求解的式子. #include< ...

  3. poj 3621 0/1分数规划求最优比率生成环

    思路:以val[u]-ans*edge[i].len最为边权,判断是否有正环存在,若有,那么就是ans小了.否则就是大了. 在spfa判环时,先将所有点进队列. #include<iostrea ...

  4. poj Dropping tests 01分数规划---Dinkelbach算法

    果然比二分要快将近一倍.63MS.二分94MS. #include <iostream> #include <algorithm> #include <cstdio> ...

  5. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  6. POJ 2728 Desert King (01分数规划)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:29775   Accepted: 8192 Descr ...

  7. POJ 2976 Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:17069   Accepted: 5925 De ...

  8. P2877 [USACO07JAN]牛校Cow School(01分数规划+决策单调性分治)

    P2877 [USACO07JAN]牛校Cow School 01分数规划是啥(转) 决策单调性分治,可以解决(不限于)一些你知道要用斜率优化却不会写的问题 怎么证明?可以暴力打表 我们用$ask(l ...

  9. POJ 2728 Desert King(最优比率生成树 01分数规划)

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

随机推荐

  1. VC程序快速删除自己(可能做升级程序的时候有用)

    项目一般都会带有卸载程序,如果这个程序是自己来做的话,在调用完卸载程序后需要删除自己的所有文件,在Google了好久终于找到一些相关信息,一般只能删除一个文件,经过自己的处理,可以删除文件夹下面所有内 ...

  2. C++堆和栈的比较(7个区别)

    基础知识: 堆 栈是一种简单的数据结构,是一种只允许在其一端进行插入或删除的线性表.允许插入或删除操作的一端称为栈顶,另一端称为栈底,对堆栈的插入和删除操作被称 为入栈和出栈.有一组CPU指令可以实现 ...

  3. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(六)-马里奥跳跃

    当w键按下时,马里奥执行跳跃动作 执行跳跃动作也是在MarioMove函数中调用的

  4. 基于visual Studio2013解决C语言竞赛题之0203格式化输出

     题目 解决代码及点评 #include <stdio.h> #include <stdlib.h> void main() { // print是输出函数,参数%s表示输 ...

  5. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  6. 【剑指Offer学习】【面试题18 :树的子结构】

    题目:输入两棵二叉树A 和B.推断B 是不是A 的子结构. 二叉树结点的定义: /** * 二叉树的树结点 */ public static class BinaryTreeNode { int va ...

  7. ThinkPHP - 连贯操作 - 【实现机制】

    <?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 ...

  8. 【Tomcat】本地域名访问设置

    原路径:localhost:8080/tidyko 1.去掉8080端口 打开%TOMCAT_HOME%/conf/server.xml 修改里面的 <Connector connectionT ...

  9. ActionBar开启Overlay Mode(覆盖模式)

    以下内容参考自Android官网http://developer.android.com/training/basics/actionbar/overlaying.html#EnableOverlay ...

  10. MFC逆向-消息响应函数的定位

    MFC  ==   Microsoft Foundation Class,微软基础类库,他封装了Windows API以便用户更快速的开发界面功能程序然而该库及其庞大而复杂,需要有C++的功底否则很难 ...