计蒜网 Candy (模拟&技巧)
来源:互联网 发布:人工智能股票有哪些 编辑:程序博客网 时间:2024/06/13 05:51
Candy
There are N 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 N, which indicates the number of students.
Then there are N students rating values, 1≤N≤300,1≤values≤10000.
Output:
The minimum number of candies you must give.
样例1
输入:
51 2 3 4 55 1 3 5 3 6
输出:
159
//题意:输入n,接下来有n个数
表示n个人的身高,现在规定高的人得到的蜡烛数要比他旁边矮的人的蜡烛数多,问最少的蜡烛数是多少?
//思路:
可以从头开始判断,一直找连续的递减的序列,找到后就将这个递减序列赋值,表示他获得的蜡烛数,然后再继续向后找,直到找完为止。
最后累加求和即可。
赋值的时候有技巧,得注意,看代码
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;int a[310];int dp[310];int main(){int n,i,j,k;while(scanf("%d",&n)!=EOF){memset(dp,0,sizeof(dp));dp[0]=1;int x,y;int sum=0;for(i=0;i<n;i++)scanf("%d",&a[i]);k=a[0];int cnt=1,m=0,s=0,e,kk;for(i=1;i<n;i++){if(k>=a[i]){cnt++;if(k==a[i])m++;}else{e=i;if(cnt==1)dp[i-1]=dp[i-2]+1;else{if(cnt-m<dp[s-1]+1)sum+=dp[s-1]+1-(cnt-m);dp[s]=cnt-m;kk=a[s];for(j=s+1;j<e;j++){if(a[j]==kk)dp[j]=dp[j-1];elsedp[j]=dp[j-1]-1;kk=a[j];}}s=e;cnt=1;m=0;}k=a[i];}if(cnt==1)dp[n-1]=dp[n-2]+1;else{if(cnt-m<dp[s-1]+1)sum+=dp[s-1]+1-(cnt-m);dp[s]=cnt-m;kk=a[s];for(j=s+1;j<n;j++){if(a[j]==kk)dp[j]=dp[j-1];elsedp[j]=dp[j-1]-1;kk=a[j];}}for(i=0;i<n;i++)sum+=dp[i];printf("%d\n",sum);}return 0;}
0 0
- 计蒜网 Candy (模拟&技巧)
- poj 1666 : Candy Sharing Game (模拟)
- J - Candy Sharing Game(模拟题)
- HDU1034 - Candy Sharing Game (简单模拟)
- POJ 1666 Candy Sharing Game(模拟)
- Codeforces 400C Inna and Huge Candy Matrix(模拟)
- 文章标题 POJ :1666 Candy Sharing Game(模拟)
- 杭电1034(模拟法) 之 Candy Sharing Game
- 模拟:HDU1034-Candy Sharing Game
- 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)
- Codeforces Round #278 (Div. 2) B. Candy Boxes (模拟,数学)
- #NOIP模拟赛#吃糖果candy(缩小选边范围--mod区间)
- pku3083Children of the Candy Corn-模拟+bfs
- 模拟_题目1145:Candy Sharing Game
- HDU 1034 Candy Sharing Game 模拟题
- hdu 1034 Candy Sharing Game(暴力模拟)
- hdu 1034 Candy Sharing Game【水题 模拟】
- hdoj 1034 Candy Sharing Game 【模拟】
- at org.gradle.wrapper.Download.downloadInternal(Download.java:58)
- SoundPool--声音池
- 列表内置方法
- PRML Ch 1: Introduction 从概率论、决策论和信息论的角度来看机器学习
- Dynamics CRM2016 查询数据的三种方式的性能对比
- 计蒜网 Candy (模拟&技巧)
- 剑指offer(12):数值的整数次方
- C语言switch语句
- Max Script|灯光篇
- POJ_1091 跳蚤
- 有关二分查找的STL
- QML 中的屏幕适配问题
- HDOJ2037(贪心)
- 八.VIM文本编辑器