[Luogu] CF557C Arthur and Table

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Luogu] CF557C Arthur and Table相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/tab' title='tab'>tab   max   limits   define   ++   getch   澶т簬   

(Link)

Description

鏈変竴寮犳瀛愶紝鏈?span class="math inline">(n)涓吙銆傜(i)鏍硅吙鐨勯暱搴︽槸(l_i)?銆?/p>

鐜板湪瑕佹嬁鎺変竴浜涜吙锛屼娇寰楁瀛愮ǔ瀹氾紝鎷挎帀绗?span class="math inline">(i)鏍硅吙闇€瑕?span class="math inline">(d_i)鐨勮兘閲忋€?/p>

绋冲畾鐨勬潯浠舵槸锛屽亣濡傛嬁鎺夎嫢骞叉潯鑵夸箣鍚庯紝妗屽瓙杩樻湁(k)涓吙锛岄偅涔堥暱搴︽渶闀跨殑鑵跨殑鏁扮洰瑕佽秴杩囦竴鍗娿€傛瘮濡傛瀛愭湁(5)鏍硅吙锛岄偅涔堣嚦灏戣鏈?span class="math inline">(3)鏍硅吙鏄渶闀跨殑銆傚彟澶栵紝鍙湁涓€鏍硅吙鐨勬瀛愭槸绋冲畾鐨勶紝涓や釜鑵跨殑妗屽瓙鎯宠绋冲畾锛屽繀闇€闀垮害鏄竴鏍风殑銆?/p>

浣犵殑浠诲姟鏄嬁鎺夎嫢骞茶吙锛屼娇寰楁瀛愮ǔ瀹氾紝骞朵笖鎵€娑堣€楃殑鑳介噺瑕佹渶灏戙€?/p>

Solution

鎴戜滑鍙互鏋氫妇妗岃吙鏈€闀跨殑闀垮害鏄粈涔堬紝璁惧叾涓?span class="math inline">(l_{mx})锛岀劧鍚庢湁(t)鏍圭殑(l_i=l_{mx})锛岄偅涔堜繚鐣欑殑鏈€澶ц兘閲忓氨鏄?span class="math inline">(sumlimits_{l_i=l_{mx}}d_i)锛屽姞涓婇暱搴﹀皬浜庡畠鐨勬鑵夸腑锛岄€夋渶澶х殑(t-1)鏍圭殑鑳介噺涔嬪拰锛堝洜涓鸿鏈€澶у寲淇濈暀鐨勶紝鎵嶈兘鏈€灏忓寲娑堣€楃殑锛夈€?/p>

鑰冭檻鎴戜滑浠庡皬鍒板ぇ鏋氫妇(l_{mx})锛屽鐞嗚繖涓暱搴﹀悗鍐嶆妸鎵€鏈夋弧瓒?span class="math inline">(l_i=l_{mx})鐨?span class="math inline">(d_i)鍔犲叆褰撳墠搴忓垪涓紝閭d箞鐜板湪瑕佽В鍐崇殑闂灏辨槸瑕佸姩鎬佺淮鎶ゅ簭鍒楀墠(t-1)澶т箣鍜屻€傝€屽椤跺爢鍙互寰堟柟渚胯В鍐宠繖绫诲姩鎬佹眰绗?span class="math inline">(k)澶ф垨鍓?span class="math inline">(k)澶х殑闂銆?/p>

瀵归《鍫嗗氨鏄淮鎶や袱涓爢锛屼竴涓ぇ鏍瑰爢(q_1)锛屼竴涓皬鏍瑰爢(q_2)锛岀劧鍚庝繚璇?span class="math inline">(q_2)閲岀殑鎵€鏈夊厓绱犻兘澶т簬(q_1)閲岀殑锛屼笉婊¤冻灏变笉鏂竴涓?span class="math inline">(pop())锛屼竴涓?span class="math inline">(push(top()))銆?/p>

鎴戜滑鍐嶄繚璇?span class="math inline">(q_2.size()=t-1)锛岄偅杩欓亾棰樼殑鍓?span class="math inline">(t-1)澶т箣鍜屽氨鏄?span class="math inline">(q_2)鍐呭厓绱犱箣鍜屼簡銆?/p>

杩欎釜鏄緢闅捐鍗$殑锛屽洜涓哄崟娆℃搷浣滄鏁板彇鍐充簬(|t_i-t_{i-1}|)锛屾渶澶т篃灏辨槸(n)锛屽鏉傚害鍏跺疄鍜岀嚎娈垫爲涓€鏍蜂篃鏄?span class="math inline">(nlog(n))銆?/p>

Code

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m, sz[100005], l[100005], d[100005];

vector < int > g[100005];

ll mx, s0, sum, s[100005];

priority_queue < int > q1;
priority_queue < int, vector < int > , greater < int > > q2; 

int read()
{
	int x = 0, fl = 1; char ch = getchar();
	while (ch < 鈥?鈥?|| ch > 鈥?鈥? { if (ch == 鈥?鈥? fl = -1; ch = getchar();}
	while (ch >= 鈥?鈥?&& ch <= 鈥?鈥? {x = (x << 1) + (x << 3) + ch - 鈥?鈥? ch = getchar();}
	return x * fl;
}

int main()
{
	n = read();
	for (int i = 1; i <= n; i ++ )
		l[i] = read(), m = max(m, l[i]);
	for (int i = 1; i <= n; i ++ )
		d[i] = read(), sum += (ll)d[i];
	for (int i = 1; i <= n; i ++ )
	{
		g[l[i]].push_back(d[i]);
		s[l[i]] += (ll)d[i];
		sz[l[i]] ++ ;
	}
	for (int i = 1; i <= m; i ++ )
	{
		if (!sz[i]) continue;
		ll cnt = s[i];
		while ((int)q2.size() != sz[i] - 1)
		{
			if ((!q1.size()) && (!q2.size())) break;
			while (q2.size() < sz[i] - 1)
			{
				int x = q1.top();
				q1.pop();
				q2.push(x);
				s0 += x;
			}
			while (q2.size() > sz[i] - 1)
			{
				int x = q2.top();
				q2.pop();
				q1.push(x);
				s0 -= x;
			}
		}
		mx = max(mx, cnt + s0);
		for (int p = 0; p < (int)g[i].size(); p ++ )
		{
			int x = g[i][p];
			if (q1.size() && x > q1.top()) q2.push(x), s0 += x;
			else q1.push(x);
		}
	}
	printf("%lld
", sum - mx);
	return 0;
}

以上是关于[Luogu] CF557C Arthur and Table的主要内容,如果未能解决你的问题,请参考以下文章

C. Arthur and Table

CodeForces 518E Arthur and Questions(贪心 + 思维)题解

CF559E Gerald and Path

CF802CHeidi and Library

Codeforces508 E. Arthur and Brackets(括号匹配,贪心)

CF191C Fools and Roads