京东编程题 爬山 求指导

京东编程题 爬山 求指导
全部评论
#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

相关推荐

07-11 22:27
中南大学 Java
程序员牛肉:学历的话没问题。但是没问题的也就只有学历了。 其实你的整体架构是正确的,博客接着干。但是项目有点过于简单了。从后端的角度上讲,你这也就是刚入门的水平,所以肯定约面试够呛。 如果你要应聘后端岗位,那你第一个项目竟然是仿写操作系统。这个你要面试官咋问你。你一定要记住一点,你简历上写的所有的东西,都是为了证明你有能力胜任当前的岗位,而不是为了证明你自己会什么。 如果你只是浅浅的做几个项目,描述也都是烂大街。技术点也都是各种混水类的配置类需求,那你就不要幻想自己能走多远。一定要保持思考,保持学习。
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
球球别拷打俺了:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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