今日头条笔试第一题解题思路

先按照y降序排序,然后循环遍历x坐标,当x为当前遍历坐标的x最大值时,即为所要的坐标,以x为键加入加入到TreeMap中,最后,遍历TreeMap输出坐标,时间负责度为nlgn+n。今天早上刚想出来的,没有实际测试,还请大神指正
所有符合要求点的情况一定是按照下图的梯度方式的,坐标为红点

代码如下,仅供参考
import java.util.*;

/**
 * 先按照y降序排序,然后循环遍历x坐标,当x为当前遍历坐标的x最大值时,即为所要的坐标,
 * 以x为键加入加入到TreeMap中,最后,遍历TreeMap输出坐标,时间负责度为nlgn+n。
 */

public class Main00 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x =0;
        int y = 0;
        TreeMap<Integer,Integer> map = new TreeMap<>();
        //定义TreeMap按照K降序排列
        TreeMap<Integer,Integer> map01 = new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int i=0;i<n;i++){
            x = sc.nextInt();
            y = sc.nextInt();
            map01.put(y,x);
        }

        //遍历map01,将x大于前面所以值的最大值所在的坐标加入map
        Iterator<Map.Entry<Integer, Integer>> it01 = map01.entrySet().iterator();
        int max = -1;
        while (it01.hasNext()) {
            Map.Entry<Integer, Integer> entry = it01.next();
            if(entry.getValue()>max){
                max = entry.getValue();
                map.put(entry.getValue(),entry.getKey());
            }
        }

        //遍历输出
        Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, Integer> entry = it.next();
            System.out.println( entry.getKey() + " " + entry.getValue());
        }
    }

}

全部评论
#include<iostream> #include<map> #include<stdio.h> #include<algorithm> using namespace std; int main() { long long N; while (cin>>N) { //cin >> N; map<long long, long long> X; for (size_t i = 0; i < N; i++) { long long x, y; cin >> x >> y; X.insert(make_pair(x, y)); } long long max_x = 0; long long max_y = 0; map<long long, long long> maptest; map<long long, long long>::iterator ittest; map<long long, long long>::iterator it; for (it = X.begin(), ittest = X.begin();it != X.end();++it) { maptest.insert(make_pair(it->first, it->second)); if (maptest.size() > 1) { if ((it->first > max_x) && (it->second > max_y)) { maptest.erase(max_x); //it++; } } max_x = it->first; max_y = it->second; } for (it = maptest.begin();it != maptest.end();++it) { cout << it->first << " " << it->second << endl; } } return 0; }
点赞
送花
回复
分享
发布于 2017-08-23 09:52
昨天晚上睡不着也想出来了,一样的思路
点赞
送花
回复
分享
发布于 2017-08-23 09:54
滴滴
校招火热招聘中
官网直投
for(int i=0;i<n;i++){ x = sc.nextInt(); y = sc.nextInt(); map01.put(y,x); } 似乎是笔误, map01.put(x,y)
点赞
送花
回复
分享
发布于 2017-08-23 10:30
1.先对所有点集按y的降序排列,存放到vector变量中 x1, x2, ... , xn y1>y2>...>yn 2.判断(xi,yi)(其中i=1,...,n)是否满足题目要求的“最大的”点 满足要求为:xi>xj,j=1,...,i-1; 因为点集已经按照y的降序进行排列,判断(xi,yi)时,只有y值比yi大的点(即该点在(xi,yi)的左上方或右上方)可能导致(xi,yi)不是“最大的点”,接着排除右上方的可能性,即满足上述要求 #include <iostream> #include <vector> #include <algorithm> using namespace std; bool cmp(pair<int,int> point1, pair<int, int> point2) { if(point1.second > point2.second) return true; else return false; } int main(int argc, char** argv) { int n; cin>>n; vector<pair<int,int> > points; for(int i=0; i<n; i++) { int x,y; cin>>x>>y; points.push_back(make_pair(x,y)); } sort(points.begin(), points.end(), cmp); int max_x = 0; vector<pair<int, int> >::iterator it; for(it=points.begin(); it!=points.end(); it++) { if(it->first > max_x) { cout<<it->first<<" "<<it->second<<endl; max_x = it->first; } } return 0; }
点赞
送花
回复
分享
发布于 2017-08-23 12:29
#include<iostream> #include<stdio.h> #include<vector> #include<algorithm> using namespace std; bool cmp(const pair<int, int> p1, const pair<int, int> p2){ return p1.second > p2.second; //这样是从大到小排序了; } int main(){ int n; //cin >> n; scanf("%d", &n); vector<pair<int, int>> vec; int x, y; for (int i = 0; i < n; i++){ //cin >> x >> y; scanf("%d %d", &x, &y); vec.push_back(make_pair(x, y)); } sort(vec.begin(), vec.end(), cmp); vector<pair<int, int>> res; res.push_back(make_pair(vec[0].first, vec[0].second)); //cout << res[0].first << " " << res[0].second << endl; int temp = 0; for (int i = 1; i<n; i++){ if (vec[i].first > res[temp].first){ res.push_back(make_pair(vec[i].first, vec[i].second)); temp++; } } int len = res.size(); for (int i = 0; i < len; i++){ //cout << res[i].first << " " << res[i].second << endl; printf("%d %d\n", res[i].first, res[i].second); } system("pause"); return 0; }
点赞
送花
回复
分享
发布于 2017-08-23 17:24
#include<iostream> #include<map> using namespace std; class MyCmp { public: bool operator() (int a, int b) const { return a > b; } }; template<typename T> void PrintMap(T &container) { for(auto it = container.begin(); it != container.end(); it++) { cout<<it->first<<" "<<it->second<<endl; } } void PrintOkPoint(map<int,int, MyCmp> &map0) { map<int,int> map1; auto it = map0.begin(); int maxX = it->second; map1[it->second] = it->first; for(it++; it != map0.end(); it++) { if(it->second > maxX) { maxX = it->second; map1[it->second] = it->first; } } PrintMap(map1); } int main() { int n,x,y; map<int,int,MyCmp> map0; cin>>n; if(n == 0) return 0; while(n--) { cin>>x>>y; map0[y] = x; } PrintOkPoint(map0); return 0; }
点赞
送花
回复
分享
发布于 2017-08-24 13:09

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务