今日头条笔试最后一道题有毒啊

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(),k = sc.nextInt(),h = sc.nextInt();
int a[] = new int[n];
int h1 = 0,m = 0; //当前,前一次跳板位置
for(int i = 0;i<n;i++){
a[i] = sc.nextInt();
}
for(int j = 0;j<k;){
for(int i = a.length-1;i>=m;i--){
if(Math.abs(a[i]-h1)<=h){
h1 = h1+(a[i]-h1)*2;
j++;
m=i;
}
}
}
System.out.println(h1);

}
}

全部评论
你这是贪心最远的跳板?这个贪心策略很容易就举反例了……比如1 3 4 6,h=2,贪心只能0-2-6,而实际上可以0-2-4-8
点赞 回复 分享
发布于 2018-03-24 21:31
渣渣,我全部AC
点赞 回复 分享
发布于 2018-03-24 21:20
我第五题浪费了几十分钟,线下可以,线上就是过不了
点赞 回复 分享
发布于 2018-03-24 21:32

相关推荐

来个offer吧求求求了:校园经历和要找到工作有半毛钱关系?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务