Problem Candy

Problem Description:

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:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

Solution:

 1     public int candy(int[] ratings) {
 2          int[] candies = new int[ratings.length];
 3         for (int i = 1; i < ratings.length ; i++) {
 4             if (ratings[i]  > ratings[i-1]) {
 5                 candies[i] = candies[i-1] + 1;
 6             }
 7         }
 8
 9         for (int i = ratings.length - 2; i >= 0; i--) {
10             if (ratings[i] > ratings[i+1] && candies[i] <= candies[i+1]) {
11                 candies[i] = candies[i+1] + 1;
12             }
13         }
14
15         int sum = 0;
16         for (int i = 0; i < candies.length; i++) {
17             sum += candies[i];
18         }
19
20         return sum + candies.length;
21     }

Problem Candy

时间: 2024-09-21 02:38:04

Problem Candy的相关文章

SPOJ Problem 2123:Candy I

#include<cstdio> #include<cstring> int n,i,j,p,s; int a[10001]; int abs(int p){return p>0?p:-p;} int main(){ while(scanf("%d",&n)&&n!=-1){ p=0; for (i=1;i<=n;i++){ scanf("%d",&a[i]); p+=a[i]; } if (p%n

SPOJ Problem 2148:Candy III

和I一样水,只不过要注意的是输入数据会特别大,所以应该先取模再相加. #include<cstdio> long long n,s,t,x,i; int main(){ scanf("%lld",&n); while(n--){ s=0; scanf("%lld",&t); for (i=1;i<=t;i++){ scanf("%lld",&x); s=(s+x%t)%t; } if (s)printf(

hdu 4465 Candy (快速排列组合 )

Candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2115    Accepted Submission(s): 910 Special Judge Problem Description LazyChild is a lazy child who likes candy very much. Despite being ve

POJ 3083 Children of the Candy Corn

Children of the Candy Corn Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 308364-bit integer IO format: %lld      Java class name: Main The cornfield maze is a popular Halloween treat. Visitors are shown the

K - Children of the Candy Corn

K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze fa

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

zoj3326An Awful Problem

题目链接: 点我点我 题目: An Awful Problem Time Limit: 1 Second      Memory Limit: 32768 KB In order to encourage Hiqivenfin to study math, his mother gave him a sweet candy when the day of the month was a prime number. Hiqivenfin was happy with that. But sever

hdu 1034 Candy Sharing Game

Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4942    Accepted Submission(s): 3020 Problem Description A number of students sit in a circle facing their teacher in the cente

Candy 解答

Question 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: Each child must have at least one candy. Children with a higher rating get more