Codeforces Educational Codeforces Round 22

A. The Contest

div2A题,大家都懂,水题一道,没啥可说的

#include<bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
const int maxn = 1000 + 5;

int main() {
	int n, sum = 0, x;
	scanf("%d", &n);
	for(int i=0; i<n; i++) {
		scanf("%d", &x);
		sum += x;
	}
	
	int m, maxs = -1;
	bool flag = true;
	scanf("%d", &m);
	while(m--) {
		int be, ed;
		scanf("%d%d", &be, &ed);
		if(be<=sum&&ed>=sum&&flag) printf("%d\n", sum), flag = false;
		else if(be>sum&&flag) printf("%d\n", be), flag = false;
		maxs = ed;
	}
	if(maxs < sum) printf("-1\n");
}

                                                                         B. The Golden Age
可以知道,一个大于2的数开64次方就大得不行了,所以这题直接枚举a, b的高次就行。
时间复杂度是lg^2 n, 中间有些地方可能可读性不是很好,建议大家独立完成此题,然后再来参考。
#include<bits/stdc++.h>
using namespace std;
const __int64 inf = 1e18 + 10;
typedef __int64 ll;
const int maxn = 64*64 + 5;

ll a[maxn];

int main() {
	ll x, y, l, r;
	scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
	
	ll xa[66], ya[66];
	xa[0] = ya[0] = 1;
	for(int i = 1; i<=64; i++) {
		xa[i] = xa[i-1]*x;
		ya[i] = ya[i-1]*y;
		if(xa[i]<xa[i-1]||log10(xa[i-1])+log10(x)>18) xa[i] = -1;
		if(ya[i]<ya[i-1]||log10(ya[i-1])+log10(y)>18) ya[i] = -1;
	}
	
	int cnt = 0;
	for(int i=0; i<65; i++) {
		for(int j=0; j<65; j++) {
			if(xa[i]==-1||ya[j]==-1) continue;
			ll xx = xa[i] + ya[j];
			if(xx>=l && xx<=r) a[cnt++] = xx;
		}
	}
	sort(a, a+cnt);
	int nn = unique(a, a+cnt) - a;
	
	if(cnt == 0) nn = 0;
	a[nn] = r+1;
	ll maxs = a[0]-l+1;
	for(int i=nn; i>=1; i--) {
		maxs = max(maxs, a[i]-a[i-1]);
	}

	if(!nn) printf("%I64d\n", r-l+1);else
	printf("%I64d\n", maxs-1);
}

                                                               C. The Tag Game
简单的图论题,A要追B,而A第n步可以走到深度为n(设A初始深度为0)的任意节点处
B只有进入可达的最深叶节点才达到其目地,用dfs算出B的深度deep(代码里用cnt表示),则B能达的最高节点为[deep/2]+1 ,然后再bfs算出此节点的最深子树长度deep2,答案就是(deep2+[deep/2])*2,为了方便,我们不妨在计算B深度时顺便算出deep2,节省时间

#include<bits/stdc++.h>
#define clr(x, y) memset(x, y, sizeof(x))
using namespace std;
const int maxn = 200000 + 5;

vector<int> nei[maxn];
int n, b, wr;
int ways[maxn];
int fa, dp[maxn];

int getway(int v, int cnt) {
	int l = nei[v].size();
	dp[v] = 1;
	ways[cnt] = v;
	if(v==b) fa = ways[cnt+2>>1], wr = cnt+1;
	if(l==1&&v!=1) return 1;
	for(int i=0; i<l; i++) {
		int u = nei[v][i];
		if(dp[u] == -1) dp[v] = max(dp[v], getway(u, cnt+1)+1);
	}
	return dp[v];
}

int main() {
	scanf("%d%d", &n, &b);
	n --;
	while(n--) {
		int v, u;
		scanf("%d%d", &v, &u);
		nei[v].push_back(u);
		nei[u].push_back(v);
	}
	clr(dp, -1);
	getway(1, 0);
	printf("%d\n", 2*(dp[fa]+(wr+1>>1)-1));
}


比赛时就写了这3题,后几道估计写不完就算了,有时间再写吧




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值