京东编程题 爬山 求指导

京东编程题 爬山 求指导
全部评论
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main_mountain() {  long n = 0, m = 0;  vector<pair<long, long>> nums;  while (cin>>n>>m) {   nums.clear();   long d, h;   for (int i = 0; i < m; ++i) {    cin >> d >> h;    nums.push_back(make_pair(d, h));   }   sort(nums.begin(), nums.end(), [](const pair<long, long>& a, const pair<long, long>& b) {    return a.first < b.first;   });   long highest = 0;   bool imp = false;   long diffd = 0;   long diffh = 0;   long more = 0;   for (int i = 0; i < m - 1; ++i) {    diffd = nums[i + 1].first - nums[i].first;    diffh = abs(nums[i + 1].second - nums[i].second);    if (diffh> diffd) {     cout << "IMPOSSIBLE" << endl;     imp = true;     break;    }    more = max(nums[i + 1].second, nums[i].second);    highest = max(highest, (diffd - diffh) / 2 + more);   }   highest = max(highest, nums[nums.size() - 1].second + n - nums[nums.size() - 1].first);   highest = max(highest, nums[0].second - 1 + nums[0].first);   if (!imp) {    cout << highest << endl;   }  }  return 0; }
点赞 回复 分享
发布于 2016-09-05 21:46
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(cin.hasNextInt()) { int n , m; n = cin.nextInt(); m = cin.nextInt(); //list保存第几天 List<Integer> list = new ArrayList<Integer>(); //map保存第几天对应的高度 HashMap<Integer, Integer> map = new HashMap<Integer,Integer>(); for(int i = 0 ; i < m ; i ++){ int a = cin.nextInt(); int b = cin.nextInt(); map.put(a, b); list.add(a); } Collections.sort(list); int max = 0; //判断第一天最高高度 if(list.get(0) > 1){ max = map.get(list.get(0)) + list.get(0) - 1; }else{ max = map.get(list.get(0)); } int size = list.size(); boolean flag = false; //判断第i天 和第 i+1天间能达到的最高高度 for(int i = 0 ; i < size - 1 ; i ++){ int pre = list.get(i); int next = list.get(i+1); int prehigh = map.get(pre); int nexthigh = map.get(next); if(Math.abs(prehigh-nexthigh) > Math.abs(pre-next) ){ System.out.println("IMPOSSIBLE"); flag = true; break; }else{ if(prehigh > nexthigh){ next = next - (prehigh - nexthigh); max = Math.max(max, prehigh+(next-pre)/2); }else{ pre = pre + (nexthigh - prehigh); max = Math.max(max, nexthigh+(next-pre)/2); } } } if(flag){ continue; } //判断最后一天能达到的最高高度 if(list.get(size-1) < (size-2)){ max = Math.max(max, map.get(list.get(size-1)) + n - list.get(size-1)); }else{ max = Math.max(max,map.get(list.get(size-1))); } System.out.println(max); } cin.close(); } }
点赞 回复 分享
发布于 2016-09-05 22:02
有没有谁AC的java,能不能告诉我scanner怎么用?上次网易内推用python,总是出现格式问题,这次换成java写,这scanner又不会用
点赞 回复 分享
发布于 2016-09-05 22:01
京东的题太无语,理解题意我都费劲。。。
点赞 回复 分享
发布于 2016-09-05 21:50
挺蛋疼的,遇到2个比较难的题,选择题也很麻烦。有没有ac java的
点赞 回复 分享
发布于 2016-09-05 21:48

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务