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

Codeforces Round 873 (Div. 2)

时间:2023-05-16 22:56:05浏览次数:44  
标签:int 873 long Codeforces Div include define

Codeforces Round 873 (Div. 2)

链接

Codeforces Round 873 (Div. 2)

A题

打印2-n并且计算总和,然后找到严格大于sum的n的倍数记为x,然后用这个x减去sum得到a.

然后先打印a然后再打印2-n

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 100008;

void solve() {
	int n;
	scanf("%lld", &n);
	vector <int> ans;
	int sum = 0;
	for (int i = 2; i <= n; i++) {
		sum += i;
	}
	int x = ((sum / n) + 1) * n;//找到严格大于sum的n的倍数x
//	cout<<sum<<'\n';
	cout << x - sum << ' ';//打印x-sum
	for (int i = 2; i <= n; i++) {//打印2-n
		cout << i << ' ';
	}
	cout << '\n';






}
signed main () {
//	std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
	int t;
	cin >> t;
	while (t) {
		solve();
		t--;
	}


	return 0;
}

B题

计算a[i]和应该正确所在的位置x计算abs(a[i]-x),一直计算这个的gcd最后打印结果

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 100008;

int a[N];
void solve() {
	int n;
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
	}
	int gcd;//定义gcd
	int num = 0;
	for (int i = 1; i <= n; i++) {
		int cnt = abs(i - a[i]);
		if (cnt != 0) {
			if (num == 0) {
				gcd = cnt;//如果是第一次进入gcd=cnt
				num++;
			} else {//否则
//一直计算gcd
				gcd = __gcd(cnt, gcd);
			}
		}


	}
	cout << gcd << '\n';//打印最后的gcd



}
signed main () {
//	std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
	int t;
	cin >> t;
	while (t) {
		solve();
		t--;
	}


	return 0;
}

C题

先将a,b数组排序,然后使用a数组来二分查找b数组,并且用一个ans来记录答案,还需要一个num来记录前面有几个数填入了对应的位置.最后打印ans

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 200008;
int a[N];
int b[N];
void solve() {
	int n;
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
	}
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &b[i]);
	}
	sort(a + 1, a + n + 1);
	sort(b + 1, b + n + 1);//排序
//	for(int i=1;i<=n;i++){
//		printf("%lld ",a[i]);
//	}
//	printf("\n");
//	for(int i=1;i<=n;i++){
//		printf("%lld ",b[i]);
//	}
//	printf("\n");
	int ans = 1;
	int num = 0;
	for (int i = 1; i <= n; i++) {
		int l = 0, r = n + 1;
		while (l + 1 != r) {

			int mid = (l + r) / 2;
			if (b[mid] < a[i]) {//查找严格小于a[i]的那个数所在的下标
				l = mid;
			} else {
				r = mid;
			}

		}
//		if(l!=0){
//			cout<<l<<'\n';
		ans = ans * (l - num);//每次累乘更新答案
		ans %= 1000000007;//记得mod1e9+7
//			cout<<l-num<<'\n';
		num++;
//		}

	}
	ans %= 1000000007;
	cout << ans << '\n';//打印结果



}
signed main () {
//	std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
	int t;
	cin >> t;
	while (t) {
		solve();
		t--;
	}


	return 0;
}

标签:int,873,long,Codeforces,Div,include,define
From: https://www.cnblogs.com/harper886/p/17407133.html

相关文章

  • Codeforces Round 767 (Div. 1) E. Groceries in Meteor Town (Kruskal重构树 + 线段
    传送门  出现最大路径权值就应该联想到克鲁斯卡尔重构树,我们对于克鲁斯卡尔重构树求一遍dfs序,维护所有白色点的最大最小dfn(包括出发点),求出最大最小dfn的最近公共祖先既是答案。注意需要特判一下除了本身以外没有白色点情况。#include<bits/stdc++.h>intn,m;constintN......
  • Codeforces Round 873 A~E
    CodeforcesRound873(Div.1)A.CountingOrders对于每个\(a_i\),可以计算出\(c_i\)表示有多少个\(b_j\lta_i\)。那么第\(i\)个人就有\(c_i\)种可能的位置。注意到如果将\(a\)升序排序,则能放的位置集合从前往后是包含的关系。所以将\(a\)排序(等价于将\(c\)排序......
  • DIV 随着滚动条 移动
    <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>codeby:haiw......
  • Educational Codeforces Round 148 (Rated for Div. 2) A-D2
    EducationalCodeforcesRound148(RatedforDiv.2) A.NewPalindromemap<int,int>mp;voidsolve(){strings;mp.clear();cin>>s;for(inti=0;i<s.size()/2;i++){mp[s[i]]++;}puts(mp.size()>=2?"YES......
  • CodeForces 1827 D Two Centroids
    洛谷传送门CF传送门考虑固定一个重心,设\(k\)为重心最大子树大小,答案为\(n-2k\)。构造方法是往最大的子树塞叶子。树的重心有一个很好的性质,就是加一个叶子,重心最多移动一条边的距离。简单证一下,设重心为\(x\),往儿子\(u\)的子树中加叶子。如果\(sz_u>\left\lfloor......
  • CodeForces 1827 B Range Sorting
    洛谷传送门CF传送门考虑拆贡献\(i-1\simi\),发现当\([1,i-1]\)的最大值大于\([i,n]\)的最小值时\(i-1\simi\)产生\(1\)的贡献。考虑枚举左端点\(j\),设\(x=\max\limits_{k=j}^{i-1}a_k\)。设\(i\)及\(i\)以后第一个\(<x\)的数位置是\(p\),那么......
  • Codeforces 1158E - Strange device(交互)
    一个有点烦的\(8\logn\)的做法。大致想法大家都一样:以\(1\)为根,然后先问出每个点深度,再问出每个点的父亲。首先先用一个log的做法问出树高,具体做法是直接令根节点的\(f\)为二分出的\(mid\),看能否覆盖所有点即可,记最大深度为\(mxdep\)。可以在二分过程中顺带着求出深......
  • Codeforces 1423C - Dušan's Railway(树分块)
    首先\(k>3\)当\(k=3\)做,也就是说题目等价于找不超过\(10n\)条路径使得任意两点间的路径都可以表示为选定的这些路径中不相交的三者的并。先考虑链怎么做,关于这个\(3\),很自然的想法是取若干关键点,关键点之间两两连边,其余点再像相邻两关键点连边,因此考虑分块,每\(B\)个点设......
  • Codeforces Round 873 (Div. 2)
    CodeforcesRound873(Div.2)A-DivisibleArray思路:每个数为i时都为i的倍数,前n个数和为Sn=n*(n+1)/2,可知每个数再乘n,Sn必为n的倍数#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=3e5+5,INF=0x3f3f3f3......
  • Codeforces Round 873 (Div. 2)(A,B,C)
    A//由求和公式(n*(n-1))/2知道当n为偶数就行,我们将每个序号乘以2就满足了#include<iostream>usingnamespacestd;constintN=210;intt;intn;intmain(){cin>>t;while(t--){cin>>n;for(inti=1;i<......