这个题目难度非常大,首先对于老师的一种方案,应用分数规划的一般做法,求出所有的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. 引用 字库编码Unicode相关知识

    引用 weifeng.shen 的 字库编码Unicode相关知识 1.      各地编码 首先说明一下现在常用的一些编码方案: 1.         在中国,大陆最常用的就是GBK18030编码, ...

  2. thinkphp 常用的查询

    php 常用的数据库查询方式: //根据where 条件查询,使用select()方法 访问:http://localhost/thinkphp2/index.php/Machine/search_i ...

  3. poj 1458 Common Subsequence(区间dp)

    题目链接:http://poj.org/problem?id=1458 思路分析:经典的最长公共子序列问题(longest-common-subsequence proble),使用动态规划解题. 1 ...

  4. 物流追踪 - -GPS和GPRS应用

    源码1: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<termios.h ...

  5. hadoop的WordCount样例

    package cn.lmj.mapreduce; import java.io.IOException; import java.util.Iterator; import org.apache.h ...

  6. 密钥登录linux

    一.linux 主机A登录linux主机B 在/etc/hosts文件下加入:(做硬解析) 192.168.1.60 u60 #设置u60为主机名 在节点A上创建RSA秘钥:(A上生成A主机密钥) # ...

  7. ViewPager实现启动引导页面(个人认为很详细)

    效果如图: 启动页面是一张图片+延时效果,这里就不给出布局文件了. WelcomeActivity分析:在启动页面检测是否是第一次运行程序,如果是,则先跳转到引导界面的Activity——AndyVi ...

  8. 一种用javascript实现的比较兼容的回到顶部demo + 阻止事件冒泡

    回到页面顶部demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. ELK 之三:Kibana 使用与Tomcat、Nginx 日志格式处理

    一:kibana安装: kibana主要是搜索elasticsearch的数据,并进行数据可视化的展现,新版使用nodejs. 1.下载地址: https://www.elastic.co/downl ...

  10. BufferedInputStream 源码分析

    一.简介 BufferedInputStream会缓存一部分数据(默认8K),这个函数的作用就是读取更多的数据到缓存,必要的时候会扩大缓存的内容. 在该类中有几个重要的标志位:markpos,pos, ...