[luoguP2885] [USACO07NOV]电话线Telephone Wire(DP + 贪心)

传送门

真是诡异。

首先 O(n * 100 * 100)

三重循环

f[i][j] 表示到第 i 个柱子,高度是 j 的最小花费

f[i][j] = min(f[i - 1][k] + abs(k - j) * c + (j - a[j]) * (j - a[j]) (1 <= k <= 100)

然而肯定超时

对于f[i][j]的值,既可以从f[i-1][j+]更新,又可以从f[i-1][j]更新,还可以从f[i-1][j-]更新。

所以可以从后往前扫,从前往后扫,都记录一个前缀最小值,然后用这个更新就行。

——代码

 1 #include <cstdio>
 2
 3 const int MAXN = 100001, INF = 100000001;
 4 int n, c, temp, ans = INF;
 5 int a[MAXN], f[MAXN][101];
 6
 7 inline int min(int x, int y)
 8 {
 9     return x < y ? x : y;
10 }
11
12 int main()
13 {
14     int i, j;
15     scanf("%d %d", &n, &c);
16     for(i = 1; i <= n; i++) scanf("%d", &a[i]);
17     for(i = 1; i <= n; i++)
18     {
19         temp = INF;
20         for(j = 100; j >= a[i]; j--)
21         {
22             temp = min(temp + c, f[i - 1][j]);
23             f[i][j] = temp + (j - a[i]) * (j - a[i]);
24         }
25         temp = INF;
26         for(j = 1; j <= 100; j++)
27         {
28             temp = min(temp + c, f[i - 1][j]);
29             f[i][j] = min(f[i][j], temp + (j - a[i]) * (j - a[i]));
30             if(j < a[i]) f[i][j] = INF;
31         }
32     }
33     for(i = a[n]; i <= 100; i++) ans = min(ans, f[n][i]);
34     printf("%d\n", ans);
35     return 0;
36 }

时间: 2024-10-15 02:04:28

[luoguP2885] [USACO07NOV]电话线Telephone Wire(DP + 贪心)的相关文章

P2885 [USACO07NOV]电话线Telephone Wire

题目描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each wit

POJ3612——Telephone Wire

Telephone Wire Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2662   Accepted: 970 Description Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more eff

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J

UVALive 4731 dp+贪心

这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i+1每次添加一个数,就要考虑这个新数应该和谁放在一组,枚举他放在哪一组即可 dp[i][j]代表当前第i个数有j个分组时候的最小值 dp[i][j]=dp[k][j-1]+i(prefix[i]-prefix[k-1]),k代表枚举第几个数开始和当前新添加的数为一组,prefix为前缀和,为了迅速得

HDU 5303(Delicious Apples- 环上折半dp+贪心)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1585    Accepted Submission(s): 514 Problem Description There are n apple trees planted along a cyclic road, which is L metres

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3

洛谷 P1948 [USACO08JAN]电话线Telephone Lines

P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system. There a

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc