P3669 [USACO17OPEN]Paired Up S 贪心+双指针

P3669 [USACO17OPEN]Paired Up S


 

有M(M为偶数)头奶牛,每头奶牛有一个产奶量,将这些奶牛两两配对,每对奶牛的产奶的时间为两头奶牛产奶量的总和。现在这M/2对奶牛同时产奶,问所需的最短时间是多少 M保证为偶数

最短时间也就是m/2对中的最长时间,贪心很好想,肯定是时间最短的牛和时间最长的牛匹配才会使最长时间最短。这题比较有意思的是配对的双指针操作。如果直接给出每个牛的时间,直接排序,然后正反相加取最值即可。但是这题对时间进行分组,而且数据量很大,展开也会超时。所以这里的双指针设定一个头尾指针,直接判断这个时间的牛的数量然后相减可以剩下大量时间,而不能一个一个的匹配。

#include <bits/stdc++.h>
#include<iostream>
#include<thread>
using namespace std;
#pragma warning(disable:4996);
#define ll long long
#define	int ll
#define endl "\n"
#define rep(j,b,e) for(int j=b;j<=e;j++)
#define eb(arr) arr.begin(),arr.end()
#define mm(arr) memset(arr,0,sizeof(arr))
#define get(a,node) get<a-1>(node)
int T;
const int N = 2e5 + 10;
int n, m, k, q;

template <class T, class ...Arg>
void inline pln(T t, Arg ...args);//打印一行参数
template<typename T>
void inline pln(vector<T> a, string s = " ");
template<class T, class ...Arg>
void inline  inln(T& t, Arg&...args);//输入一行数据
//----------------------------------

struct node {
	int x, y;
};
signed main()
{
#ifndef ONLINE_JUDGE
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	cin >> n;
	vector<node>arr(1);
	rep(j, 1, n) {
		int x, y;
		inln(x, y);
		arr.push_back({ x,y });
	}
	int ans = 0;

	sort(arr.begin() + 1, arr.end(), [](node a, node b) {
		return a.y < b.y;
		});
	int i = 1, j = n;
	while (i <= j) {
		auto t1 = arr[i];
		auto t2 = arr[j];
		ans = max(ans, arr[i].y + arr[j].y);
		if (t1.x > t2.x) {
			arr[i].x -= arr[j].x;
			j--;
		}
		else if (t1.x < t2.x) {
			arr[j].x -= arr[i].x;
			i++;
		}
		else {
			i++;;
			j--;
		}
	}
	pln(ans);
	return 0;
}

//-------------------------------------
void inline  inln() {}
template<class T, class ...Arg>
void inline  inln(T& t, Arg&...args) {
	cin >> t;
	inln(args...);
}
void inline pln() {
	cout << "\n";
}
template <class T, class ...Arg>
void inline pln(T t, Arg ...args) {
	cout << t << " ";
	pln(args...);
}
template<typename T>
void inline pln(vector<T> a, string s) {
	for (T& x : a) {
		cout << x << s;
	}
	cout << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值