C. Alice and the Cake_思维-堆

2022/4/28 23:17:42

本文主要是介绍C. Alice and the Cake_思维-堆,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C. Alice and the Cake_思维-堆

1400

题目大意:

一个蛋糕每次可以切一半,如12切成6+6,13切成6+7。现在给一堆无序的数,问是否可能由一个蛋糕切割得来。

思路和代码:

模拟切蛋糕的过程,并且每次切完贪心的去比较切出来的最大蛋糕和给出的最大蛋糕是否匹配。若不匹配就再去切即可。

 
string solve2(){
	int n ; 
	cin >> n ;
	vct<ll> a(n + 1 , 0) ;
	priority_queue<ll , vct<ll> , less<ll> > now , q ;
	
	rep(i , 1 , n) cin >> a[i] , q.push(a[i]) ;
	
	ll sum = accumulate(a.begin() + 1 , a.end() , 0LL) ;
	
	now.push(sum) ;
	ll cnt = 0 ;
	while(q.size() && now.size()){
		ll ma = remain.top() ;
		now.pop() ;
//		cout << ma << " " << q.top() << "\n";
		if(ma == q.top()){
			q.pop() ;
		}else{
//			cout << cnt + 1 << "<\n" ;
			if(++ cnt > n - 1) return "NO\n" ;
			now.push(ma / 2) ;
			now.push(ma / 2 + (ma % 2 ? 1 : 0)) ;
			
		}
	}
	return "YES\n" ;
}

小结:

因为每次都是对半切,所以可以去模拟切的过程。



这篇关于C. Alice and the Cake_思维-堆的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程