1 条题解

  • 0
    @ 2021-6-15 14:22:06

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    const int MAXN = 100000 + 1000;
    
    typedef pair<int, int> pii;
    typedef pair<long long, int> pli;
    
    int n;
    long long m;
    int px[MAXN], py[MAXN], xx[MAXN];
    int s[MAXN];
    
    int lowbit(int x)
    {
    	return x & (-x);
    }
    void clear()
    {
    	memset(s, 0, sizeof(s));
    	return ;
    }
    void modify(int x, int v)
    {
    	for(int i = x; i; i -= lowbit(i))
    		s[i] += v;
    	return ;
    }
    int query(int x)
    {
    	int ret = 0;
    	for(int i = x; i <= n; i += lowbit(i))
    		ret += s[i];
    	return ret;
    }
    
    void init()
    {
    	static pii arr[MAXN];
    	for(int i = 1; i <= n; i++)
    		arr[i] = make_pair(px[i], i);
    	sort(arr + 1, arr + n + 1);
    	int tot = 1;
    	xx[arr[1].second] = tot;
    	for(int i = 2; i <= n; i++)
    		tot += (arr[i].first != arr[i - 1].first), xx[arr[i].second] = tot;
    	return ;
    }
    
    long long calc(int k)
    {
    	static pli arr[MAXN];
    	long long ret = 0;
    	for(int i = 1; i <= n; i++)
    		arr[i] = make_pair(py[i] - (long long)k * px[i], xx[i]);
    	sort(arr + 1, arr + n + 1, greater<pli>());
    	clear();
    	for(int i = 1; i <= n; i++)
    		ret += query(arr[i].second + 1), modify(arr[i].second, 1);
    	return ret;
    }
    
    int main()
    {
    	ios::sync_with_stdio(false);
    //	freopen("slope.in", "r", stdin);
    //	freopen("slope.out", "w", stdout);
    	cin >> n >> m;
    	for(int i = 1; i <= n; i++)
    		cin >> px[i] >> py[i];
    	init();
    	int l = -210000000, r = 210000000;
    	while(l < r)
    	{
    		int mid = (l + r + 1) / 2;
    		if(calc(mid) >= m)
    			l = mid;
    		else
    			r = mid - 1;
    	}
    	int ans = l;
    	cout << ans << endl;
    
    	return 0;
    }
    
    
    • 1

    信息

    ID
    991
    时间
    4000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者