计蒜客 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
原创粉丝点击