首页 > 其他分享 >Codeforces Round 973 (Div. 2)

Codeforces Round 973 (Div. 2)

时间:2024-09-22 11:45:44浏览次数:1  
标签:__ cnt 973 int sum 元素 Codeforces solve Div

Codeforces Round 973 (Div. 2)

A - Zhan's Blender

由于总量固定 因此只用计算两个处理时间 最大值即是所求

#include<bits/stdc++.h>
using namespace std;
int n;
double x, y;
 
void solve(){
	cin >> n;
	cin >> x >> y;
	int tim1 = ceil(n * 1.0 / x);
	int tim2 = ceil(n * 1.0 / y);
	cout<< max(tim1, tim2) << endl;
	
}
 
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	cin >> _;
	while(_--){
		solve();	
	}
	return 0;
} 

B - Battle for Survive

因为每次操作都是后一个减去前一个,因此末尾数字总是存在

所以我们只需要维持倒数第二个数字最小,

所以我们让前n - 2个数字去和n - 1数字进行操作

得到最小的n - 1数字,最后用n - 1和n进行最后一次操作就能得到答案

#include<bits/stdc++.h>
using namespace std;
int n;
long long num;
vector<int> fit(200000, 0);
void solve(){
	cin >> n;
	num = 0;
	for(int i = 0; i < n; ++i){
		cin >> fit[i];
	}
	if(n == 2){
		cout << fit[1] - fit[0] << endl;
		return;
	}else{
		for(int i = 0; i < n - 2; ++i){
			num += fit[i];
		}
		cout << fit[n - 1] - (fit[n - 2] - num) << endl;
		return;
	}
}
 
int main(){
	int _;
	cin >> _;
	while(_--){
		solve();	
	}
	return 0;
} 

D - Minimize the Difference

因为若a_i > a_i + 1,那么我们进行转移,因为从前向后转移即便对当前无益,但后面可能有元素需要进行添加,而当前操作不会对整个队列产生负面影响,因此,我们总是进行转移。

建立一个栈,每次都从外部取新的元素b放进来,然后从队列尾部取出元素a开始判断,如果a大于等于b。

将a取出,乘上a的个数,于b乘b的个数加到一起,计算平均数。

将b更新为这个新的平均数,数量也进行更新,继续从队列中取出元素,重复上述操作

最后,用得到的总和和元素数量计算出新的数,放入栈中。

用栈中的头元素,减去尾元素,就能得到答案

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200200];
int n;

void solve(){
	cin >> n;
	for(int i = 1; i <= n; ++i){
		cin >> a[i];//读入原始数组 
	}
	stack<pair<ll, int>> s;
	for(int i = 1; i <= n; ++i){
		ll sum = a[i], cnt = 1;//取出一个新元素,将其添加到栈中 
		//如果栈不为空,并且栈顶的元素的值大于均值(用取出的所有数的和/位置) 
		while(s.size() && s.top().first >= sum / cnt){
			sum += s.top().first * s.top().second;//将其取出 
			cnt += s.top().second;
			s.pop();
		}
		//将新的数和位置放入栈中
		s.push({sum / cnt, cnt - sum % cnt}); 
		if(sum % cnt != 0){
			s.push({sum / cnt + 1, sum % cnt});
		}
	}//头元素和尾元素 相减求出答案 
	ll mx = s.top().first;
	while(s.size() > 1){
		s.pop();
	}
	cout << mx - s.top().first << '\n';
}
int main(){
	int _;
	cin >> _;
	while(_--){
		solve();
	}
	return 0;
} 

E - Prefix GCD

1、先求出所有元素的gcd--g并将所有元素都除以g

2、对剩下的所有数据进行暴力查找,如果出现1, 剩余的全为1

#include<bits/stdc++.h>
using namespace std;

int n, k;
int a[200200];

void solve(){
	cin >> n;
	int g = 0, cur = 0;
	long long ans = 0;
	for(int i = 0; i < n; ++i){//求全体共同的最大公因数 
		cin >> a[i];
		g = __gcd(g, a[i]);
	}
	for(int i = 0; i < n; ++i){
		a[i] /= g;
	}
	//循环已经找到元素的个数 
	for(int t = 0; t < n; ++t){
		int nc = 1e9;
		for(int i = 0; i < n; ++i){//查找剩下的元素, 
			nc = min(nc, __gcd(cur, a[i]));
		}
		cur = nc;//下一轮找比cur更小的 
		ans += cur;//将最小的加进去 
		if(cur == 1){//如果为1,剩下的就不用判断了 
			ans += n - t - 1;
			break;
		}
	}
	//输出答案乘g 
	cout << ans * g << "\n";
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	cin >> _;
	while(_--){
		solve();	
	}
	return 0;
}

数组的 GCD 在最多 10 次迭代后最终会达到 1。

为什么是 10 次迭代?:从数论来看,数字序列的 GCD 是一个非递增函数。这意味着 GCD 要么保持不变,要么随着添加到数组中的每个新元素而减少。在实践中,已经观察到 GCD 可以在非常少的迭代次数(最多 10 次)内减少到 1。这是因为一旦 GCD 开始变小,可以添加的数字集就会变得更加有限,并且达到 GCD 1 所需的步骤更少。

                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         I have a dream! An AC dream!!

标签:__,cnt,973,int,sum,元素,Codeforces,solve,Div
From: https://www.cnblogs.com/lyx9785/p/18425121

相关文章

  • Codeforces Round 974 (Div. 3)
    A.RobinHelps题意:Robin一开始的钱为0,每个人都有ai个金币,如果ai>=k则Robin会拿着它的金币, 如果ai==0且手上有金币,Robin会送出1金币,输出Robin送了几次思路:按照题意Code:#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;usingi6......
  • Codeforces Round 973 (Div. 2)
    SolCF2013A每次最多操作\(\min(x,y)\),故答案为\(\lceil\frac{n}{\min(x,y)}\rceil\)。#include<bits/stdc++.h>usingnamespacestd;usingu32=unsigned;usingi64=longlong;usingu64=unsignedlonglong;usingi128=__int128;#defineIOS#de......
  • Codeforces Round 973 (Div. 2) B. Battle for Survive
    题目链接:题目大意:把题目的操作翻译一下就是拿一个数去减后面的一个数,然后前面这个数会消掉。最小化最后剩下的数。思路:容易看出,最后剩下的一定是最后一个数,因为最后一个数一定不会被消去,又已知最后只剩下一个数,那么就是最后一个数。前面的所有数都要被消去,最差的情况就......
  • for循环—不同div显示不同样式
    for出来的div想要显示不同的样式,可以通过动态class,根据需要的条件指示控制样式,例如用index第一个div显示first-card的样式,第二个div显示second-card的样式<divclass="meal"><el-cardclass="meal_details"v-for="(item,index)inm......
  • POLIR-Society-Sociology-Individual-Social Identity Theory: 社会身份理论
    POLIR-Society-Sociology:社会学Individual:SocialIdentityTheory:社会身份理论InSociology,WehavethisSocialIdentifyTheory,Whichisabouthowwedefineourselvesasindividualperson,ShowcasingWhowewanttobeandHowotherpeopleseeusasaperso......
  • Educational Codeforces Round 135 (Rated for Div. 2)D. Letter Picking
    注意读题,每次拿完之后是放在开头。所以先手不败,因为最后剩下两个的时候,先手一定可以取较小值。考虑怎样会出现平局?首先已经知道了先手不败,那么对于后手来说,他追求的就是平局,也就是尽可能的保证每一步都都与先手相同。所以,如果是回文串,或者两两相同,或者回文串包两两相同的情况,才......
  • Educational Codeforces Round 136 (Rated for Div. 2) D. Reset K Edges
    这道题目我们可以考虑二分做,二分出最终的深度,然后尝试是否能使用不超过\(k\)次操作使得深度符合条件。考虑如何和判断,我们可以从根节点开始搜索,如果当前点的深度为\(mid+1\),就对当前点进行操作。但很可惜,这种贪心方法可以很容易的举出反例,比如深度为\(mid\)的点下面有很多个叶......
  • OpenCV(cv::divide())
    目录1.函数定义2.工作原理3.示例3.1矩阵除法3.2矩阵和标量的除法3.3使用缩放因子4.注意事项5.应用场景cv::divide()是OpenCV中用于执行数组或标量的逐元素除法操作的函数。它允许对矩阵进行元素级的除法操作,支持两种使用方式:矩阵与矩阵之间的除法,或矩阵与标量之间的......
  • Codeforces Round 972 (Div. 2)
    A.SimplePalindrome考虑到对于同一种字母无论怎么摆放,对答案的影响是相同的。所以我们可以直接把同一种字母放在一起,考虑不同中字母间为了消除回文串,必须是的同一种字母不会出现在另一种字母的两侧。因此我们只要尽可能的均分五种字母就好了。#include<bits/stdc++.h>using......
  • error: rpmdb, failed: BDB1507 Thread died in Berkeley DB library,error(-30973) fro
    rpm数据库错误,一般原因:yum更新等rpm软件安装进程被异常终止[root@49bdfccd7f61~]#yuminstall-yxxxerror:rpmdb:BDB0113Thread/process22858/140222685267712failed:BDB1507ThreaddiedinBerkeleyDBlibraryerror:db5error(-30973)fromdbenv->failchk:BDB0......