列表

详情


NC52866. Parentheses

描述

Bobo has a very long sequence divided into n consecutive groups.
The i-th group consists of l_i copies of character c_i where c_i is either "`(`" or "`)`".

As the sequence may not be valid parentheses sequence, Bobo can change a character in the i-th group from "`(`" to "`)`" (and vice versa) with cost d_i.
He would like to know the minimum cost to transform the sequence into a valid one.

Note:

- An empty string is valid.
- If S is valid, (S) is valid.
- If U, V are valid, UV is valid.

输入描述

The input contains zero or more test cases and is terminated by end-of-file. For each test case:

The first line contains an integer n.
The i-th of the following n lines contains l_i, c_i, d_i.

*
*
* is even.
*
* The sum of n does not exceed .

输出描述

For each case, output an integer which denotes the result.

示例1

输入:

4
1 ( 1
1 ( 2
1 ( 3
1 ) 4
2
500000000 ) 1000000000
500000000 ( 1000000000

输出:

2
500000000000000000

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 577ms, 内存消耗: 3296K, 提交时间: 2019-10-04 12:27:52

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100000+10;
int n;
ll l[N],cost[N];
char c;
struct node
{
	ll first,second;
}p;
bool operator<(node a,node b)
{
	if(a.second==b.second)return a.first<b.first;
	return a.second>b.second;
}
priority_queue<node>que;
int main()
{
	while(~scanf("%d",&n))
	{
		ll ans = 0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld %c %lld",&l[i],&c,&cost[i]);
			if(c=='(')
			{
				ans+=l[i]*cost[i];
				cost[i]=-cost[i];
			}
		}
		while(!que.empty())
		{
			que.pop();
		}
		ll k = 0;
		ll tot = 0;	
		ll ret = 0;
		for(int i=1;i<=n;i++)
		{
			p.first = l[i];
			p.second = cost[i];
			que.push(p);
			tot += l[i];
			ret = (tot + 1) /2 - k;
			k = ( tot + 1) /2 ;
			while(ret > 0 && !que.empty())
			{
				p=que.top();
				que.pop();
				
				ll ch_right_num= min(p.first,ret);
				ret -= ch_right_num;
				
				ans += ch_right_num * p.second;
				p.first  -= ch_right_num;
				
				if(p.first > 0 )
					que.push(p);
			}
		}
		
		printf("%lld\n",ans);
	}
	return 0; 
}

C++11(clang++ 3.9) 解法, 执行用时: 622ms, 内存消耗: 18060K, 提交时间: 2019-10-04 20:39:37

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int mod=1e9+7;
int n;
struct node
{
	int l,d;
	bool operator<(const node v)const
	{
		if(d==v.d)
			return l>v.l;
		return d>v.d;
	}
};

int main()
{
	#ifndef ONLINE_JUDGE
		int startTime=clock();
	#endif	
	while(scanf("%d",&n)!=EOF)
	{
		ll ans=0,now=0;
		int l,d;
		char ch;
		priority_queue<node> pq;
		for(int i=0;i<n;i++)
		{
			scanf("%d %c %d",&l,&ch,&d);
			if(ch=='(')
			{
				ans+=1LL*l*d;
				d*=-1;
			}
			pq.push({l,d});
			int res=(now+l+1)/2-(now+1)/2;
			node t;
			while(!pq.empty())
			{
				if(!res) break;
				t=pq.top(),pq.pop();
				int num=min(t.l,res);
				ans+=1LL*t.d*num;
				res-=num;
				if(num<t.l)
					pq.push({t.l-num,t.d});
			}
			now+=l;
		}
		printf("%lld\n",ans);
	}
	#ifndef ONLINE_JUDGE
		printf("Time = %dms\n",(int)clock()-startTime);
	#endif
	return 0;
}

上一题