做题记录2021.12.10

CF909E Coprocessor

题目大意

给定一张 DAG,其中含 n 个点和 m 条有向边。

给定两种处理器,主处理器和副处理器以及对应的点(主处理器一次只可处理一个点,副处理器一次可处理多个点)

求副处理器最少运行次数

思路

因为是 DAG ,所以想到拓扑排序。

题目要求副处理器运行次数最少,相当于要主处理器运行次数尽可能多。所以我们可以先把能处理的主处理器的任务一次性处理完,然后再处理副处理器的。

实现中,开两个队列维护两个处理器就OK了。

Code

// Problem: CF909E Coprocessor
// URL: https://www.luogu.com.cn/problem/CF909E
// Memory Limit: 250 MB
// Time Limit: 1500 ms
// Author:arlenWKX

# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, M  = 1e5 + 5;

int n, m, a [ N ];
int had [ N ], nxt [ M ], vct [ M ], edgenum;
int ind [ N ];
queue < int > q [ 2 ];
int tot, ans;

void adddoge ( int u, int v )
{
	
	edgenum ++;
	
	nxt [ edgenum ] = had [ u ];
	vct [ edgenum ] = v;
	had [ u ] = edgenum;
	
}

int main ( )
{
	
	scanf ( "%d%d", & n, & m );
	
	for ( int i = 1; i <= n; i ++ ) scanf ( "%d", & a [ i ] );
		
	for ( int i = 1, x, y; i <= m; i ++ )
	{
		scanf ( "%d%d", & x, & y );
		adddoge ( ++ y, ++ x );
		ind [ x ] ++;
		
	}
	
	for ( int i = 1; i <= n; i ++ )
		if ( ! ind [ i ] ) q [ a [ i ] ].push ( i );
	
	while ( tot < n )
	{
		
		if ( ! q [ 0 ].empty ( ) )
		{
			
			while ( ! q [ 0 ].empty ( ) )
			{
				
				int u = q [ 0 ].front ( ); q [ 0 ].pop ( );
				tot ++;
				for ( int e = had [ u ]; e; e = nxt [ e ] )
				{
					int v = vct [ e ]; ind [ v ] --;
					if ( ! ind [ v ] ) q [ a [ v ] ].push ( v );
				}
				
			}
			
		}
		else
		{
			
			ans ++;
			
			while ( ! q [ 1 ].empty ( ) )
			{
				int u = q [ 1 ].front ( ); q [ 1 ].pop ( );
				tot ++;
				for ( int e = had [ u ]; e; e = nxt [ e ] )
				{
					int v = vct [ e ]; ind [ v ] --;
					if ( ! ind [ v ] ) q [ a [ v ] ].push ( v );
				}
				
			}
			
		}
		
	}
	
	printf ( "%d", ans );
	
	return 0;
	
}

CF725D Contest Balloons

题目大意

ACM比赛,大家都知道。AC一题会有一个气球。 现在有n(2<=n<=300000)n(2<=n<=300000) 支队伍,每支队伍的重量是 wi ,拥有 ti 个气球(wi,ti<=10^18),当一支队伍的气球个数比它的重量都要大时,这个队伍就会飘起来,从而被取消比赛资格。 现在你带领的是1号队,你希望你队伍的名次尽可能靠前,你是个有原则的人,不会偷气球,但你可以把气球送给别的队伍,让他们飞起来。 求最终你的队伍所获得的最好名次

思路

显然,这又㕛叒叕是个贪心。为了尽可能多的迫害帮助其他队伍,我们应该优先将自己的气球分给弱势群体离飘起来所需要的气球数小气球数量比我们多的队伍,这用排序+扫描+优先队列可以实现。

某队伍:我可谢谢您了

Code

// Problem: CF725D Contest Balloons
// URL: https://www.luogu.com.cn/problem/CF725D
// Memory Limit: 250 MB
// Time Limit: 3000 ms
// Author: arlenWKX

//oi十年一场空,不开longlong见祖宗
//一般不建议用#define int long long ... signed main()这种东西,除非你真的搞不清哪个要开哪个不要开
//STL真香!!!提高+/省选-->普及/提高-

# include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 300005;

int n;
ll t, w, a, b;
pair < ll, ll > doge [ N ];
priority_queue < ll, vector < ll >, greater < ll > > pige;

int main ( )
{
	
	scanf ( "%d%lld%lld", & n, & t, & w );
	
	for ( int i = 2; i <= n; i ++ )
	{
		scanf ( "%lld%lld", & a, & b );
		doge [ i ] = pair < ll, ll > ( a, b );
	}
	
	sort ( doge + 2, doge + 1 + n, greater < pair < ll, ll > > ( ) );
	
	int pos = 2, rank = 0x7fffffff;
	
	while ( 1 )
	{
		
		for ( ; pos <= n && doge [ pos ].first > t; pos ++ )
			pige.push ( doge [ pos ].second - doge [ pos ].first + 1 );
			
		rank = min ( rank, ( int ) pige.size ( ) + 1 );
		
		if ( pige.size ( ) && pige.top ( ) <= t )
			t -= pige.top ( ), pige.pop ( );
		else break;
		
	}
	
	printf ( "%d", rank );
	
	return 0;
	
}"   style="width: 926px; height: 1108px; border:0; transform: scale(1); overflow:hidden;"   sandbox="allow-scripts allow-same-origin"> </iframe>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值