zoukankan      html  css  js  c++  java
  • Codeforces Round #693 (Div. 3)E. Correct Placement

    E. Correct Placement

    题意

    你有n个朋友,每个朋友具有(h_i,w_i)两个属性,让你在满足条件下,求第i个朋友是否具有能放在其前面的朋友,输出对应的id

    思路

    因为h,w可以交换,所以我们将所有的h,w中大的作为y,小的作为x,然后按一定的规则排序。

    排序后我们用双指针去寻找在i前面的最小的朋友,然后将其放到i的前面即可。注意要时刻更新最小值。

    #include<bits/stdc++.h>
    
    using namespace std;
    const int N = 2e5 + 100;
    
    #define int long long
    typedef long long LL;
    
    struct NODE {
    	int a, b, id;
    	bool operator < (const NODE& x) const {
    		if (a != x.a) return a < x.a;
    		return b < x.b;
    	}
    }a[N];
    
    int ans[N];
    void solve() {
    	int n; cin >> n;
    	for (int i = 1; i <= n; ++i) {
    		int x, y; cin >> x >> y;
    		if (x > y)swap(x,y);
    		a[i] = {x, y, i};
    	}
    	sort(a + 1, a + 1 + n);
    	for (int i = 1; i <= n; ++i)ans[i] = -1;
    	int j = 1, v = 1e9, id = -1;
    	for (int i = 1; i <= n; ++i) {
    		while (j < i && a[j].a < a[i].a) {
    			if (v > a[j].b) {
    				v = a[j].b;
    				id = a[j].id;
    			}
    			++j;
    		}
    		if (v < a[i].b)ans[a[i].id] = id;
    	}
    	for (int i = 1; i <= n; ++i) {
    		cout << ans[i] << " ";
    	}
    	cout << endl;
    
    }
    
    
    signed main() {
        int T = 1;
    	// scanf("%d",&T);
        cin >> T;
        while (T--) {
            solve();
        }
    
    }
    
  • 相关阅读:
    《构建之法》阅读笔记二
    《构建之法》阅读笔记一
    软件工程个人课程总结
    纯随机数生成器
    递归方法
    素数的输出
    字母统计|英语的26 个字母在一本小说中是如何分布的
    类的声明
    FileInputStream类与FileOutputStream类
    验证码|程序登录界面
  • 原文地址:https://www.cnblogs.com/waryan/p/14289892.html
Copyright © 2011-2022 走看看