[USACO11DEC]Umbrellas for Cows
我dp真是太弱了,这么简单dp都不会。
令dp[i]表示前 i 头牛头被遮住了的最低成本。则dp[i] = min{dp[i], dp[j - 1] + c[a[i] - a[j] + 1]} (1 <= j <= i)
然后别忘了预处理后缀最小值。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const ll INF = 1e12;
const db eps = 1e-;
const int maxn = 5e3 + ;
const int maxm = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << ) + (ans << ) + ch - '', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, a[maxn], c[maxm];
ll dp[maxn]; int main()
{
n = read(); m = read();
for(int i = ; i <= n; ++i) a[i] = read();
sort(a + , a + n + );
for(int i = ; i <= m; ++i) c[i] = read();
for(int i = m - ; i; --i) c[i] = min(c[i], c[i + ]);
for(int i = ; i <= n; ++i) dp[i] = INF;
for(int i = ; i <= n; ++i)
for(int j = ; j <= i; ++j)
dp[i] = min(dp[i], dp[j - ] + c[a[i] - a[j] + ]);
write(dp[n]), enter;
return ;
}
[USACO11DEC]Umbrellas for Cows的更多相关文章
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- Poj2186Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31533 Accepted: 12817 De ...
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...
随机推荐
- springboot定时任务,去掉指定日期
今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日. 网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html ...
- android 生成随机数
/** * 随机数.字母 工具类 * Created by admin on 2017/2/20. */ public class RandomUntil { /** * 生成 ...
- Layer UI 模块化的用法(转)
此文章适合入门的同学查看,之前因为项目的原因,在网上找了一套Layer UI做的后台管理系统模板,完全不懂LayUI里面的JS用法,看了官方文档和其它资料后才明白怎么去实现模块化这个例子,但是还是感觉 ...
- 理解Xaml标记语言
理解XAML XAML基于XAML,因而具有与XAML相似的特性.在XAMl中,同样必须区分大小写,但是Xaml以.xaml作为扩展名,表示这是一个应用程序的标记扩展文件.WPF中的XAML主要用于创 ...
- 在 Web 应用中创建 Node.js 应用程序
本分步指南将通过 Azure Web 应用帮助您启动并运行示例 Node.JS 应用程序.除 Node.JS 外,Azure Web 应用还支持其他语言,如 PHP..NET.Node.JS.Pyth ...
- 建造者(生成器)模式C++、Java实现
1.建造者模式UML 图1. 建造者模式UML 2.C++实现 C++实现类视图: 图2. 建造者模式C++实现的类视图 其中,Product的实现代码是(ProductA和ProductB的代码不再 ...
- git 永久性设置密码
git 设置不需要输入密码 https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --g ...
- css-布局定位
position: ** 属性值 - absolute: *** 将对象从文档流中拖出 *** 可以是top.bottom等属性进行定位 - relative: *** 不会将对象从文档流中拖出 * ...
- 云计算的三种模式:IaaS、PaaS和SaaS
云计算主要分为三种服务模式,而且这个三层的分法重要是从用户体验的角度出发的: 1. Software as a Service,软件即服务,简称SaaS,这层的作用是将应用作为服务提供给客户. 2. ...
- Python-约束和异常处理
今天我们来说一说类的规范以及程序出现错误后我们要怎么进行处理 一.类的约束 首先,你要清楚,约束是对类的约束,比如,现在你是一个项目经理,然后呢,你给手下的人分活,张三你处理一下普通用户登录,李四你处 ...