There are NN children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

(1) Each child must have at least one candy.

(2) Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

Input:

The input consists of multiple test cases.

The first line of each test case has a number NN, which indicates the number of students.

Then there are NN students rating values, 1 \leq N \leq 300, 1 \leq values \leq 100001≤N≤300,1≤values≤10000.

Output:

The minimum number of candies you must give.

样例1

输入:

  1. 5
  2. 1 2 3 4 5
  3. 5
  4. 1 3 5 3 6

输出:

  1. 15
  2. 9

贪心。调了半天。扫两遍

  1. /* ***********************************************
  2. Created Time :2016/4/24 17:15:25
  3. File Name :1.cpp
  4. ************************************************ */
  5. #include <iostream>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <stdio.h>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <queue>
  12. #include <set>
  13. #include <map>
  14. #include <string>
  15. #include <math.h>
  16. #include <stdlib.h>
  17. #include <iomanip>
  18. #include <list>
  19. #include <deque>
  20. #include <stack>
  21. #define ull unsigned long long
  22. #define ll long long
  23. #define mod 90001
  24. #define INF 0x3f3f3f3f
  25. #define maxn 10010
  26. #define cle(a) memset(a,0,sizeof(a))
  27. const ull inf = 1LL << ;
  28. const double eps=1e-;
  29. using namespace std;
  30. priority_queue<int,vector<int>,greater<int> >pq;
  31. struct Node{
  32. int x,y;
  33. };
  34. struct cmp{
  35. bool operator()(Node a,Node b){
  36. if(a.x==b.x) return a.y> b.y;
  37. return a.x>b.x;
  38. }
  39. };
  40.  
  41. bool cmp(int a,int b){
  42. return a>b;
  43. }
  44. int a[maxn];
  45. int b[maxn];
  46. int main()
  47. {
  48. #ifndef ONLINE_JUDGE
  49. freopen("in.txt","r",stdin);
  50. #endif
  51. //freopen("out.txt","w",stdout);
  52. int n;
  53. while(cin>>n){
  54. for(int i=;i<n;i++)cin>>a[i];
  55. int tmp=;
  56. cle(b);
  57. for(int i=;i<n;i++){
  58. if(a[i]>a[i-])b[i]=max(tmp++,b[i]);
  59. else tmp=;
  60. }
  61. tmp=;
  62. for(int i=n-;i>=;i--){
  63. if(a[i]>a[i+])b[i]=max(tmp++,b[i]);
  64. else tmp=;
  65. }
  66. int sum=;
  67. for(int i=;i<=n;i++)sum+=b[i];
  68. cout<<sum+n<<endl;
  69. }
  70. return ;
  71. }

C.Candy的更多相关文章

  1. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  2. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  4. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  5. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  6. 【leetcode】Candy

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  8. [LintCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. POJ - 1666 Candy Sharing Game

    这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...

  10. Candy Store

    Candy Store Time Limit: 30000ms, Special Time Limit:75000ms, Memory Limit:65536KB Total submit users ...

随机推荐

  1. bzoj1065【Noi2008】奥运物流

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1065 给一棵基环树,每个点i的权值=ci+k*∑son[i],修改至多m个点的父亲使1号点权值 ...

  2. Machine Learning--决策树(一)

    决策树(decision tree):是机器学习常见的算法之一.是基于树形结构进行决策的. 讲决策树就要提到“信息熵”.“信息增益”.“增益率”和“基尼指数”的概念. 我们先来介绍一下这几个概念:(讲 ...

  3. KD-Tree 的笔记

    声明: 蒟蒻对于 KD-Tree 的一点理解,写在博客里面作为笔记. 1.KD-Tree 的定义 1)关于 K-D KD-Tree 中的 D 即为 Dimension ,意思也就是维度. 所以 KD- ...

  4. bzoj2286 (sdoi2011)消耗战(虚树)

    [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4052  Solved: 1463[Submit][Status][Dis ...

  5. Spring-IOC源码解读1-整体设计

    1. SpringIOC提供了一个基本的javabean容器,通过IOC模式管理依赖关系,并通过依赖注入和AOP增强了为javabean这样的pojo对象赋予事务管理,生命周期管理等基本功能.2. S ...

  6. angular中关于ng-repeat的性能问题

    首先,ng-repeat的渲染是改变则渲染的.而且是无法自动检测内容是否改变的. $scope作为一个对象,对象的特性就是两个对象是不相同的,因为我们比较的是两个对象的地址,即便两个对象的内容甚至排版 ...

  7. HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

    Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

  8. sqlplus 命令 错误

    SP2-1503: 无法初始化 Oracle 调用界面 用管理员运行就可以了

  9. Jetson TK1 五:移植工控机程序到板上

    1.gazebo xml 2.王 chmod 777 chmod 777 /home/robot2/bzrobot_ws/src/bzrobot/bzrobot_control/bzrobot_con ...

  10. python 获取时间 存入文件

    1读文件: file_path_name = '/home/robot/bzrobot_ws/src/bzrobot/bzrobot_comm/led_show_data/'+file_name+'. ...