这个题目难度非常大,首先对于老师的一种方案,应用分数规划的一般做法,求出所有的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. cocos2d-x游戏开发系列教程-超级玛丽05-CMMenuScene

    代码下载链接 http://download.csdn.net/detail/yincheng01/6864893 解压密码:c.itcast.cn 背景 上一篇博文提到appDelegate,在该类 ...

  2. Java class文件分析工具 -- Classpy

    Classpy Classpy是一个图形化的class文件分析工具,功能和javap类似,界面主要參考了Java Class Viewer: 为什么要又一次创造轮子? 写这个工具花了将近一周的时间.那 ...

  3. Android Touch事件传递机制具体解释 上

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/37961997 近期总是遇到关于Android Touch事件的问题,如:滑动冲突的问题,曾经 ...

  4. N皇后( DFS,推荐)

    N皇后问题 Description   在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.          你的任 ...

  5. 非root不能gdb attach的限制

    Could not attach to process.  If your uid matches the uid of the targetprocess, check the setting of ...

  6. Hadoop集群的安装与配置(centos 6.5)

    一.Hadoop搭建准备(centOs6.5  且每个系统都要有同一个用户,如:hadoop)     1.IP的配置 包括Master和Slaves的IP配置,之间能够相互ping通:  例如:   ...

  7. 2014 HDU多校弟八场H题 【找规律把】

    看了解题报告,发现看不懂 QAQ 比较简单的解释是这样的: 可以先暴力下达标,然后会发现当前数 和 上一个数 的差值是一个 固定值, 而且等于当前数与i(第i个数)的商, 于是没有规律的部分暴力解决, ...

  8. (Problem 2)Even Fibonacci numbers

    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w ...

  9. Chapter 4.开放-封闭原则

    开放-封闭原则:是说软件实体应该可以扩展,但不可修改. 设计人员必须对于他设计的模块应该对哪种变化封闭做出选择,先猜测出最有可能发生的变化种类,然后构造抽象来隔离那些变化. 面对需求,对程序的改动是通 ...

  10. 基于visual Studio2013解决C语言竞赛题之0411公约数和公倍数

       题目 解决代码及点评 求最大公约数和最小公倍数,方法已经在题目中有提示,分析代码实现如下: /* 题目: 输入两个正整数 m和 n,求其最大公约数和最小公倍数. */ #includ ...