斜率优化

想骂人了,马格吉最后调了半小时

TMD造数据的人是SB吧?

我写  while(scanf("%d%d",&n,&m)!=EOF&&n)

然后就TMD无限WA...WA...WA...

尼玛 改成while(scanf("%d%d",&n,&m),n)

就过了,就过了!!!

沃日,浪费我时间是吧,坑爹是吧

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<cstring>
#define Clear(x,i) memset(x,i,sizeof(x))
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
template <typename Q>
void inin(Q &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
ret=f?-ret:ret;
}
int n,m,a[],sum[],cost[],x[][],y[][],num[];
int f[][],c;
int cross(int a,int b,int xx,int yy)
{
return (x[b][c]-x[a][c])*(yy-y[a][c])-
(xx-x[a][c])*(y[b][c]-y[a][c]);
}
int main()
{
while(scanf("%d%d",&n,&m),n)
{
m++;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
cost[i]=cost[i-]+sum[i-]*a[i];
}
num[]=;
c=;
x[][]=y[][]=;
for(int j=;j<=m;j++)
{
c^=;
num[c]=;
for(int i=,k=;i<=n;i++)
{
while(k<num[!c]&&(x[k+][!c]-x[k][!c])*sum[i]>y[k+][!c]-y[k][!c])k++;
f[i][c]=-sum[i]*x[k][!c]+y[k][!c]+cost[i];
int xx=sum[i],yy=f[i][c]+xx*xx-cost[i];
while(<num[c]&&cross(num[c]-,num[c],xx,yy)<=)num[c]--;
x[++num[c]][c]=xx;
y[num[c]][c]=yy;
}
}
printf("%d\n",f[n][c]);
}
return ;
}

UValive4195 Heroes of Money and Magic的更多相关文章

  1. 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) H Heroes Of Might And Magic (隐含dp)

    问题是求一个方案,实际隐含一个dp.法力是递减的,所以状态是DAG,对于一个确定的状态,我们贪心地希望英雄的血量尽量大. 分析:定义状态dp[i][p][h]表示是已经用了i的法力值,怪兽的位置在p, ...

  2. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  3. 黑科技项目:英雄无敌III Mod <<Fallen Angel>>介绍

    英雄无敌三简介(Heroes of Might and Magic III) 英3是1999年由New World Computing在Windows平台上开发的回合制策略魔幻游戏,其出版商是3DO. ...

  4. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  5. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  6. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  7. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  8. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. js 零散知识总结

    网页播放声音 这个非常简单,我们只需要在html和js设置即可.首先看html代码 html代码 <audio id="sound" autoplay="autop ...

  2. saltstack在jianja模板中,执行salt函数来获取某些变量的值,并且将配置写入到配置文件中?

    问题描述: 通过saltstack的jinja模板方式,可以将变量的值写入到配置文件,即动态获取的方式.这里介绍,通过执行salt函数来获取值的方式. 演示: 1.通过在sls中,增加jinja的模板 ...

  3. A股滚动净利润增速最高排名

    最近2年(共8个季度)的滚动净利润都在增长,且平均增速超过10%. 计算举例:滚动净利润增速 = ((2018Q1 到 2018Q4的净利润之和) / (2017Q4 到 2018Q3的净利润之和) ...

  4. 发送消息-配置app_id

    $user_id = $curr_workitem["creater_id"]; $user_name = g('dao_user') -> get_by_id($user_ ...

  5. 遍历文件,读取.wxss文件,在头部添加一条注释

    change.pl #!/usr/bin/perl use autodie; use utf8; use Encode qw(decode encode); use v5.26; my $path = ...

  6. c++ __declspec

    dllimport 和dllexport 用__declspec(dllexport),__declspec(dllimport)显式的定义dll接口给调用它的exe或dll文件,用 dllexpor ...

  7. asp.net session锁导致ajax请求阻塞

    问:为了可以顺序访问Session的状态值,Session是否提供了锁定机制?答:Session实现了Reader/Writer的锁机制:当页面对Session具有可写功能(即页面有<%@Pag ...

  8. php验证18位身份证,准到必须输入正确的身份证号,

    /** * 验证18位身份证(计算方式在百度百科有) * @param string $id 身份证 * return boolean */ function check_identity($id=' ...

  9. switch(值){ 开始case 值: 闭合break; }

    switch ($goods['leixing']) { case 1: $data['type'] = 1; $data['orderid'] = 'PT' . rand(000000, 99999 ...

  10. 怎么在Centos7 下让我的mariadb开机启动?(已解决)

    以前我经常使用syscemctl工具在开机后执行 systemctl start mariadb (哈哈,打得可6,只是有点儿麻烦), 如果能开机自启动mariadb就好了. 所以,我想百度下看什么命 ...