C.Candy
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
输入:
- 5
- 1 2 3 4 5
- 5
- 1 3 5 3 6
输出:
- 15
- 9
贪心。调了半天。扫两遍
- /* ***********************************************
- Created Time :2016/4/24 17:15:25
- File Name :1.cpp
- ************************************************ */
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <stdio.h>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- #include <string>
- #include <math.h>
- #include <stdlib.h>
- #include <iomanip>
- #include <list>
- #include <deque>
- #include <stack>
- #define ull unsigned long long
- #define ll long long
- #define mod 90001
- #define INF 0x3f3f3f3f
- #define maxn 10010
- #define cle(a) memset(a,0,sizeof(a))
- const ull inf = 1LL << ;
- const double eps=1e-;
- using namespace std;
- priority_queue<int,vector<int>,greater<int> >pq;
- struct Node{
- int x,y;
- };
- struct cmp{
- bool operator()(Node a,Node b){
- if(a.x==b.x) return a.y> b.y;
- return a.x>b.x;
- }
- };
- bool cmp(int a,int b){
- return a>b;
- }
- int a[maxn];
- int b[maxn];
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- #endif
- //freopen("out.txt","w",stdout);
- int n;
- while(cin>>n){
- for(int i=;i<n;i++)cin>>a[i];
- int tmp=;
- cle(b);
- for(int i=;i<n;i++){
- if(a[i]>a[i-])b[i]=max(tmp++,b[i]);
- else tmp=;
- }
- tmp=;
- for(int i=n-;i>=;i--){
- if(a[i]>a[i+])b[i]=max(tmp++,b[i]);
- else tmp=;
- }
- int sum=;
- for(int i=;i<=n;i++)sum+=b[i];
- cout<<sum+n<<endl;
- }
- return ;
- }
C.Candy的更多相关文章
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 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 ...
- [LintCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- POJ - 1666 Candy Sharing Game
这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...
- Candy Store
Candy Store Time Limit: 30000ms, Special Time Limit:75000ms, Memory Limit:65536KB Total submit users ...
随机推荐
- bzoj1065【Noi2008】奥运物流
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1065 给一棵基环树,每个点i的权值=ci+k*∑son[i],修改至多m个点的父亲使1号点权值 ...
- Machine Learning--决策树(一)
决策树(decision tree):是机器学习常见的算法之一.是基于树形结构进行决策的. 讲决策树就要提到“信息熵”.“信息增益”.“增益率”和“基尼指数”的概念. 我们先来介绍一下这几个概念:(讲 ...
- KD-Tree 的笔记
声明: 蒟蒻对于 KD-Tree 的一点理解,写在博客里面作为笔记. 1.KD-Tree 的定义 1)关于 K-D KD-Tree 中的 D 即为 Dimension ,意思也就是维度. 所以 KD- ...
- bzoj2286 (sdoi2011)消耗战(虚树)
[Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4052 Solved: 1463[Submit][Status][Dis ...
- Spring-IOC源码解读1-整体设计
1. SpringIOC提供了一个基本的javabean容器,通过IOC模式管理依赖关系,并通过依赖注入和AOP增强了为javabean这样的pojo对象赋予事务管理,生命周期管理等基本功能.2. S ...
- angular中关于ng-repeat的性能问题
首先,ng-repeat的渲染是改变则渲染的.而且是无法自动检测内容是否改变的. $scope作为一个对象,对象的特性就是两个对象是不相同的,因为我们比较的是两个对象的地址,即便两个对象的内容甚至排版 ...
- 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 ...
- sqlplus 命令 错误
SP2-1503: 无法初始化 Oracle 调用界面 用管理员运行就可以了
- Jetson TK1 五:移植工控机程序到板上
1.gazebo xml 2.王 chmod 777 chmod 777 /home/robot2/bzrobot_ws/src/bzrobot/bzrobot_control/bzrobot_con ...
- python 获取时间 存入文件
1读文件: file_path_name = '/home/robot/bzrobot_ws/src/bzrobot/bzrobot_comm/led_show_data/'+file_name+'. ...