有N个孩子站成一排,每个孩子有一个分值。给这些孩子派发糖果,需要满足如下需求:
1、每个孩子至少分到一个糖果
2、分值更高的孩子比他相邻位的孩子获得更多的糖果
求至少需要分发多少糖果?
score = list(map(int, input().split(','))) candy = [1]*len(score) # 往左看调整糖果数 for i in range(1, len(score)): if candy[i] <= candy[i - 1] and score[i] > score[i - 1]: candy[i] = candy[i - 1] + 1 # 往右看调整糖果数 for i in range(len(score) - 2, 0, -1): if candy[i] <= candy[i + 1] and score[i] > score[i + 1]: candy[i] = candy[i + 1] + 1 print(sum(candy))
/* 左右两次遍历,当 分数较高时,要为他加上糖果 类似于小米的厨师奖金分配题 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] str = br.readLine().split(","); int[] score = new int[str.length]; int[] sweet = new int[str.length]; for(int i = 0;i<str.length;i++){ score[i] = Integer.parseInt(str[i]); sweet[i]=1; } int sum = 0; //左遍历,分数高,糖果比你多;右边与左边比 for(int i = 1;i<str.length;i++){ if(score[i]>score[i-1]&& sweet[i]<=sweet[i-1]) sweet[i] = sweet[i-1]+1; } //右遍历,分数高,糖果取最大的加1;左边与右边比 for(int i = str.length -2;i>=0;i--){ if(score[i]>score[i+1] && sweet[i]<=sweet[i+1]) sweet[i] = Math.max(sweet[i],sweet[i+1])+1; sum+=sweet[i]; } System.out.println(sum+sweet[str.length-1]); } }
const readline = require('readline') const rl = readline.createInterface({ input: process.stdin, ouput: process.stdout }) let inArr = [] rl.on('line',line=>{ if(!line) return inArr.push(line.trim()) if(inArr.length === 1){ let arr = inArr[0].split(',').map(e=>+e) let res = new Array(arr.length).fill(1) let len = arr.length let cnt = 0 for(let i = 1 ; i < len ; i++){ if(arr[i] > arr[i-1]){ res[i] = res[i-1] + 1; } } for(let i = len -1 ; i > 0 ; i--){ if(arr[i-1] > arr[i] && res[i-1] <= res[i]){ res[i-1] = res[i] + 1; } } for(var i=0;i<len;i++){ cnt += res[i]; } // console.log(res) console.log(cnt) } })
#include <bits/stdc++.h> using namespace std; int main(){ int n, x, s = 0; vector<int> a; while(cin>>x){ a.push_back(x); if(cin.get()=='\n') break; } n = a.size(); vector<int> b(n,1); for(int i=1;i<n;i++) if(a[i]>a[i-1]) b[i] = b[i-1] + 1; for(int i=n-1;i>0;i--) if(a[i]<a[i-1] && b[i]>=b[i-1]) b[i-1] = b[i] + 1; s = 0; for(int i=0;i<n;i++) s += b[i]; cout<<s<<endl; return 0; }
score = list(map(int, input().split(','))) res = [1]*len(score) for i in range(1,len(score)): if score[i]>score[i-1]: res[i]=res[i-1]+1 for i in range(len(score)-2, -1,-1): if score[i]>score[i+1]: res[i] = max(res[i], res[i+1]+1) print(sum(res))
#include <bits/stdc++.h> using namespace std; int main() { int n,sum=0; vector<int>num; while(cin>>n) { num.push_back(n); if(cin.get()=='\n') break; } vector<int>res(num.size(),1); for(int i=1;i<num.size();i++) if(num[i]>num[i-1]) res[i]=res[i-1]+1; for(int i=num.size()-1;i>0;i--) if(num[i]<num[i-1]&&res[i]>=res[i-1]) res[i-1]=res[i]+1; for(int i=0;i<num.size();i++) sum+=res[i]; cout<<sum<<endl; return 0; }
import java.util.Arrays;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串数字");
int sum = 0;
String line = sc.nextLine();
String[] sArr = line.split(",");
}
"""" 从左至右,看自己左侧的分数,若比自己小,在他的基础上加一 从右至左,看自己右侧的分数,若比自己小,max(自己,右侧+1) """ if __name__ == "__main__": a = list(map(int, input().strip().split(','))) n = len(a) ans = [1] * n for i in range(1, n): if a[i] > a[i - 1]: ans[i] = ans[i - 1] + 1 for i in range(n - 2, -1, -1): if a[i] > a[i + 1]: ans[i] = max(ans[i], ans[i + 1] + 1) print(sum(ans))
class Solution { public int candy(int[] ratings) { int[] left = new int[ratings.length]; int[] right = new int[ratings.length]; Arrays.fill(left, 1); Arrays.fill(right, 1); for(int i = 1; i < ratings.length; i++) if(ratings[i] > ratings[i - 1]) left[i] = left[i - 1] + 1; int count = left[ratings.length - 1]; for(int i = ratings.length - 2; i >= 0; i--) { if(ratings[i] > ratings[i + 1]) right[i] = right[i + 1] + 1; count += Math.max(left[i], right[i]); } return count; } }
//双向遍历法 //同 2019网易校招,厨艺大赛奖金 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <limits.h> using namespace std; void DeliverSugar() { int num; char ch; vector<int> vec; while (cin >> num) { vec.push_back(num); cin >> ch; } int len = vec.size(); vector<int> dp(len, 1); //从左向右 for (int i = 1; i < len; i++) { if (vec[i] > vec[i - 1]) { dp[i] = dp[i-1] + 1; } } //从右向左 for (int i = len - 1; i > 0; i--) { if (vec[i - 1] > vec[i]) { dp[i - 1] = max(dp[i - 1], dp[i] + 1); } } int res = 0; for (int i = 0; i < len; i++) { res += dp[i]; } cout << res << endl; } int main(){ DeliverSugar(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int num; vector<int> arr; while(cin >> num){ arr.push_back(num); if(cin.get()=='\n') break; } int n = arr.size(); vector<int> left(n, 1), right(n, 1); int pre = arr[0], add = 1; for(int i=1; i<n; i++){ if(arr[i] > arr[i-1]){ left[i] += add; add++; }else add = 1; } pre = arr[n-1]; add = 1; num = 0; for(int i=n-2; i>=0; i--){ if(arr[i] > arr[i+1]){ right[i] += add; add++; }else add = 1; num += max(left[i], right[i]); } cout << num+left[n-1]; return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] str = in.nextLine().split(","); int[] candy = new int[str.length]; for(int i = 0; i < str.length; i ++) candy[i] = Integer.valueOf(str[i]); int res = 1; int pre = 1; int dis = 0; for(int i = 1; i < candy.length; i ++) { if(candy[i] >= candy[i - 1]) { if(dis > 0) { res += dis * (dis + 1) / 2; if(dis >= pre) res += dis - pre + 1; dis = 0; pre = 1; } pre = candy[i] == candy[i - 1] ? 1 : pre + 1; res += pre; } else dis += 1; } if(dis > 0) { res += dis * (dis + 1) / 2; if(pre <= dis) res += dis - pre + 1; } System.out.println(res); } } //贪心,补糖是重点。实际上是等差数列求和的应用,但是要注意给降序之前的小朋友补糖。一次遍历。
#include <bits/stdc++.h> using namespace std; int main(){ vector<int> arr; while(1){ int t; cin>>t; arr.push_back(t); if(cin.get()=='\n') break; } vector<int> dp(arr.size(),1); dp[0]=1; for(int i=1;i<arr.size();i++){ if(arr[i]>arr[i-1]) dp[i]=dp[i-1]+1; } for(int i=arr.size()-2;i>=0;i--){ if(arr[i]>arr[i+1]) dp[i]=max(dp[i],dp[i+1]+1); } int sum=0; for(int i=0;i<arr.size();i++) sum+=dp[i]; cout<<sum; return 0; }
nums = list(map(int, input().split(','))) candy = [1 for _ in range(len(nums))] for i in range(1, len(nums)): if nums[i] >nums[i-1]: candy[i] = candy[i-1] +1 for i in range(len(nums)-2, -1, -1): if nums[i] > nums[i+1]: candy[i] = max(candy[i], candy[i+1]+1) print(sum(candy))
#include <iostream> #include <vector> #include <numeric> using namespace std; int main(void){ vector<int> score; vector<int> candy; int x; while(cin>>x){ score.push_back(x); candy.push_back(1); if(cin.get() == '\n') break; } for(int i = 1; i < score.size(); ++i) if(score[i] > score[i-1] && candy[i] <= candy[i-1]) candy[i] = candy[i-1]+1; for(int j = score.size()-1; j > 0; --j) if(score[j] < score[j-1] && candy[j] >= candy[j-1]) candy[j-1] = candy[j]+1; cout<<accumulate(candy.begin(), candy.end(), 0)<<endl; return 0; }从左到右,从右到左两次遍历,遇到升序,糖果数目要比前面一个加1
import java.util.Arrays; import java.util.Scanner; /** * @Author: kunrong * @Date: 2019/8/16 16:27 * @Description: **/ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str[] = sc.nextLine().split(","); int n = str.length; int a[] = new int[n]; int l = 0; for (String i : str) { a[l++]= Integer.parseInt(i); } int dp[] = new int[n]; Arrays.fill(dp,1); for (int i = 1; i < n; i++) { if (a[i]>a[i-1]&&dp[i]<=dp[i-1]){ dp[i]= dp[i-1]+1; } } for (int i = n-2;i >= 0; i--) { if(a[i]>a[i+1]&&dp[i]<=dp[i+1]) { dp[i] = dp[i+1]+1; } } int res = 0; for (int i = 0; i < n; i++) { res+=dp[i]; } System.out.println(res); } }
#include<iostream> #include<vector> #include<string> #include <numeric> #include <algorithm> using namespace std; int main() { vector<int>zq;//每人分一个 string temp; cin >> temp; for (int i = 0; i < temp.size(); i = i + 2) zq.push_back(temp[i]-'0'); vector<int>candy(zq.size(), 1); for (int i = 1; i < zq.size(); ++i) if (zq[i] > zq[i - 1]) candy[i]= candy[i-1]+1;//全员向左看 for (int i = zq.size() - 2; i >= 0; --i) if (zq[i] > zq[i + 1]) candy[i] = max(candy[i],candy[i + 1] + 1);//全员向右看 cout << accumulate(candy.begin(), candy.end(), 0) << endl; return 0; }
function DispatchApple() { var arr = Array.prototype.slice.call(arguments, 0).sort(); var data = 1; var total = 0; for (var i=0;i<arr.length;i++) { if (i > 0 && arr[i] > arr[i-1]) data++; total += data; } return total; } alert(DispatchApple(0,1,0)); // 4 alert(DispatchApple(5,4,1,1)); // 7