这个题目难度非常大,首先对于老师的一种方案,应用分数规划的一般做法,求出所有的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. 基于visual Studio2013解决C语言竞赛题之0203格式化输出

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

  2. Shiro入门(1)

    =============基本概念=================== 什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是 ...

  3. JSON XML IO数据操作

    一.XML解析 通过继承org.xml.sax.helpers.DefaultHandler类,覆写characters(),startDocument(),startElement(),endEle ...

  4. 比赛--找丢失的数--解题报告T

    找丢失的数 题目大意: There is a permutation without two numbers in it, and now you know what numbers the perm ...

  5. ViewPager+Fragment实现支持左右滑动的Tab

    主要思想:顶部标题栏top.xml,中间ViewPager(4个Fragment),底部导航 top.xml和bottom.xml在我之前的两个随笔里有,此处不再赘述. activity_main.x ...

  6. Android线程和handler

    根据视频仿照着写了个demo: package com.wyl.wylthreadtest; import android.graphics.Color; import android.os.Bund ...

  7. Python监控网站运行状况

    利用python便捷的类库,可以方便快速实现对网站运行状况的监控,主要包括对80端口(即网站运行端口),其它tcp服务等端口的监控就可以了解服务器大概的一个运行状况,使用的库主要为urllib2及so ...

  8. media_root以及static_root配置

    # At the top of settings/base.pyfrom os.path import join, abspath, dirnamehere = lambda *x: join(abs ...

  9. java基础之junit测试框架

    1.导入junit包, 2.测试方法格式 public void test_*(){} 继承  TestCase  包(keep the bar green to keep the code clea ...

  10. javascript 入门之简单换肤效果

    大家好,我是小强老师,这里简单入门 做一个换肤效果 效果如图所示: 这个案例思路分为两部分: 获取元素对象. var pic1 = document.getElementById('pic1'); v ...