计蒜客 11065 Candy
来源:互联网 发布:js修改input的value 编辑:程序博客网 时间:2024/06/15 13:08
原题
Descrition
n个数a[i]
再给这个 n个位置 一个数b[i]
如果 a[i] 比两边的大 那么b[i]也要比两边的大
求
Σb[i] 最小是多少
Algorithm
从a[i]最小的开始放 1
再放a[i]第二小的 如果两边都没放 就是 0 那么也可以放1
反正就是放 两边的 之中 最大的 + 1
这样结果肯定最小
应该算贪心把
Code
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int maxn = 300 + 9;int n;int a[maxn];struct V{ int x, i;};V b[maxn];bool cmp(const V &x, const V &y){ if (x.x < y.x) return true; return false;}int c[maxn];void solve(){ memset(c, 0, sizeof(c)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i].x = a[i]; b[i].i = i; } sort(b, b + n, cmp); c[b[0].i] = 1; for (int i = 1; i < n; i++) { int s = -1; if (b[i].i - 1 >= 0) s = c[b[i].i - 1]; if ((b[i].i + 1 < n) && c[b[i].i + 1] > s) s = c[b[i].i + 1]; c[b[i].i] = s + 1; } int ans = 0; for (int i = 0; i < n; i++) ans += c[i]; printf("%d\n", ans);}int main(){// freopen("input.txt", "r", stdin); while (scanf("%d", &n) != EOF) solve();}
0 0
- 计蒜客 11065 Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- candy
- Candy
- Candy
- Candy
- 是什么造就了如此庞大的PHP帝国?
- AppInventor专题:如何实现两个页面之间的跳转
- union的使用
- 50. Pow(x, n) 【M】【62】
- 227. Basic Calculator II
- 计蒜客 11065 Candy
- 利用AppInventor读取天气预报接口(完整版)
- 菜鸟第一次写博客
- Struts2:ClassNotFoundException: org...dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- 棋子染色问题(burnside引理或者polya定理)
- Git小记
- 4537: [Hnoi2016]最小公倍数|分块
- 一道关于位段及大小端的面试题
- HashMap源码注解 之 resize()方法(七)