HDUマルチ第十校フィールド1008コイン - プライオリティキュー貪欲+

トピックリンク:ポイント私はああ╭(╯^╰)╮

効果の件名:

    持っています n個 n個 コインのセット、各コインは、二つを有しています A B a_iを、b_i
    来るように頼ま K K コイン最大値 F K F(k)は
    コインのセットを制限します。 A a_iを 取らないために時間を取ることはありませんでした B b_i
    頼みます F 1 F 2 F K F(1)、F(2)、...、F(K)

問題解決のアイデア:

    そこにあるので B b_i 制限事項、そうではない局所最適大域的最適
    電流がかかったと仮定して、それについて考え、 K K コイン、および最適な
    コインを獲得する方法を検討します:
     ①: コイン最大電流を取ることができることを直接取り
     ②: 最初のテイクを交換してください K K コインをした後、現在は取ることができ、コインの最大のグループを取ります

    杜漏は何の三番目のケースを発見していない、第三の場合は、既に上で最適解に含まれている
    ので、それはコインの様々なを取ることができます記録し、いくつかの最初を得た
    プライオリティキューを維持するために何をすることができますと

コア:+貪欲ファックプライオリティキュー

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int T, n, ans[maxn], th2[maxn], th[maxn];
struct node1{
	int x, id, th;
	bool operator < (const node1 &A) const{
		return x < A.x;
	}
};
struct node2{
	int x, y, id;
	bool operator < (const node2 &A) const{
		return x + y < A.x + A.y;
	}
}; 
priority_queue <node1> q1;
priority_queue <node2> q2;

int main() {
	scanf("%d", &T);
	while(T--){
		scanf("%d", &n);
		while(!q1.empty()) q1.pop();
		while(!q2.empty()) q2.pop();
		for(int i=1, a, b; i<=n; i++){
			scanf("%d%d", &a, &b);
			th2[i] = b, th[i] = 0;
			q1.push({a, i, 0});
			q2.push({a, b, i});
		}
		int f = 0, prex, preid, a, b;
		for(int i=1; i<=n*2; i++){
			while(th[q1.top().id]!=q1.top().th && !q1.empty()) q1.pop();
			while(th[q2.top().id]!=0 && !q2.empty()) q2.pop();
			a = q1.top().x;
			b = (f&&!q2.empty()) ? q2.top().x + q2.top().y - prex : 0;
			if(a <= b){	//	取 2个 
				f = 0;
				ans[i] = ans[i-1] + b;
				th[q2.top().id] = 2;
				q1.push({prex, preid, --th[preid]});
				q2.pop();
			} else {	//	取 1个 
				f = 1;
				ans[i] = ans[i-1] + a;
				prex = q1.top().x, preid = q1.top().id;
				q1.pop();
				if(!th[preid]) q1.push({th2[preid], preid, 1});
				++th[preid];
			}
			printf("%d%c", ans[i], " \n"[i==n*2]);
		}
	}
}
公開された221元の記事 ウォンの賞賛220 ・は 20000 +を見て

おすすめ

転載: blog.csdn.net/Scar_Halo/article/details/103835040