[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 ...
随机推荐
- 公司管理系列--Facebook是如何营造企业文化的[转]
本文讲下硅谷创业公司的文化,去过硅谷公司或者是看过硅谷公司报道的人,都会惊讶硅谷创业公司里面有如此奇特且活力十足的文化.在中国,企业文化是一个被滥用但是却又缺乏解读的概念,很多国内企业对保持公司的 ...
- java实现Redis分布式锁
网上到处都是分布式锁的代码,基本都是通过setNX 和 expire 这两个不是原子操作,肯定会有问题,不乏好多人通过用setNX的value当做过期时间来弥补等等.但是好像都不太好,或者多少有点问题 ...
- 纯css面板插件,自适应,多样式
最近在做公司的系统后台,用的bootstrap,在设计布局的时候不喜欢他的面板,所以自己写了这个面板插件,分享给大家 先上预览图: 默认样式: 绿色: 黄色: 蓝: 红: 使用方法: 引用MoUi.c ...
- typeof的探讨
console.log(typeof 'abc') // "string" console.log(typeof true )// "boolean" cons ...
- css3+javascript旋转的3d盒子
今天写点css3,3d属性写的3d盒子,结合javascript让盒子随鼠标旋转起来 今天带了css3新属性3d <!DOCTYPE html> <html> <head ...
- <Android 应用 之路> 百度地图API使用(1)
简介 详情请看百度地图官方网站 http://lbsyun.baidu.com/index.php?title=androidsdk/guide/introduction 使用方式 申请密钥,针对移动 ...
- C语言输入语句scanf与fgets linux下
1.测试使用scanf的一个例子: #include "stdio.h" #include "string.h" int main() { char name[ ...
- 斐波那契数列(C++ 和 Python 实现)
(说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 1. 写一个函数,输入 n, 求斐波那契(Fibonacci ...
- mac crontab
1.前提工作 正常情况下mac普通用户是无法使用crontab的,需要做如下修改: 在~/.vimrc下添加这么一行: autocmd filetype crontab setlocal noback ...
- js 巧妙去除数组中的重复项
1.代码如下: var toObject = function(a) { var o = {}; for (var i=0, j=a.length; i<j; i=i+1) { // 这里我调整 ...