上计会算法月赛2021年一月丙组

分割队伍

枚举所有的分组情况,打擂台找出前后两组总等待时间之差的最小值,用前缀和数组优化对求和的计算

#include <iostream>
#include <cmath>
using namespace std;

const int N = 1e5+10;
int n, a[N], s[N], sum, minn = 1e9;;

int main(){
    
    
	cin >> n;
	for (int i = 1; i <= n; i ++) {
    
    
		cin >> a[i];
		s[i] = s[i-1] + a[i];
		sum += a[i];
	}
	
	for (int i = 1; i <= n-1; i ++) {
    
    
		minn = min(minn, abs(sum - s[i] - s[i]));
	}

	cout << minn;

    return 0;
}

康托表

将康托表旋转45°后得到下面这个金字塔图形,再观察图形写出代码
( 1 , 1 ) ( 2 , 1 )   ( 1 , 2 ) ( 3 , 1 )   ( 2 , 2 )   ( 1 , 3 ) ( 4 , 1 )   ( 3 , 2 )   ( 2 , 3 )   ( 1 , 4 ) ( 5 , 1 )   ( 4 , 2 )   ( 3 , 3 )   ( 2 , 4 )   ( 1 , 5 ) \begin{gathered} (1,1)\\ (2,1) \ (1,2)\\ (3,1) \ (2,2) \ (1,3)\\ (4,1) \ (3,2) \ (2,3) \ (1,4)\\ (5,1) \ (4,2) \ (3,3) \ (2,4) \ (1,5)\\ \end{gathered} (1,1)(2,1) (1,2)(3,1) (2,2) (1,3)(4,1) (3,2) (2,3) (1,4)(5,1) (4,2) (3,3) (2,4) (1,5)

#include <iostream>
using namespace std;

int main(){
    
    
	int a, b;
	
	cin >> a >> b;
	int t = a+b-2, sum = 0;
	for (int i = 1; i <= t; i ++) {
    
    
		sum += i;
	}
	
	if (t%2 == 0) sum = sum + b;
	else sum = sum + a;
	
	cout << sum ;

    return 0;
}

寻找页码

#include <iostream>
using namespace std;

int a, k, cnt;

int main(){
    
    
	cin >> a >> k;
	
	int i = 0;
	while (true) {
    
    
		i ++;
		int t = i;
		
		while (t) {
    
    
			if (t%10 == a) {
    
    
				cnt ++;
				if (cnt == k) {
    
    
					cout << i << endl;
					return 0;
				}
			}
			t /= 10;
		}		
	}

    return 0;
}

三倍游戏

#include <iostream>
#include <cmath>
using namespace std;

int n, x;
int a, b, c;

int main(){
    
    
	cin >> n;
	for (int i = 1; i <= n; i ++) {
    
    
		cin >> x;
		if (x%3==0) a++;
		if (x%3==1) b++;
		if (x%3==2) c++;
	}
	
	cout << min(b,c) + a/2;
	
    return 0;
}

还原序列

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

const int N = 1e5+10;
int n, k, x, p[N], ans[N];

int main() {
    
    
	cin >> n >> k;
	for (int i =  1; i <= n; i ++) {
    
    
		cin >> x;
		p[x] = i;
	}

	for (int i = 1; i <= n; i ++) {
    
    
		int	t = p[i];
		for (int j = 1; j < k; j ++) {
    
    
			t = p[t];
		}
		ans[t] = i;
	}

	for (int i = 1; i <= n; i ++) {
    
    
		cout << ans[i] << ' ';
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/davidliule/article/details/113243216