2019.1.17训练上午场

本文详细解析了CodeForces平台上的五道经典算法题目,包括电梯运行优化、酒桶倒酒策略、字符串构造、树形DP求解路径及字典树节点最大化问题,提供了源代码与思路讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - The Fair Nut and Elevator

 CodeForces - 1084A 

坚果住在n层的房子里,每层有ai个人,每个人上下班都需要乘电梯。电梯的第一层是1楼。选择一层楼X作为这栋楼的长期停靠点,最后电梯都需要回到选择停靠的楼层。问最少每天电梯要运行多少步。

暴力枚举即可

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<utility>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<functional>
using namespace std;
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define PI 3.14159265359
typedef long long LL;
const int MAXN = 1e3;
LL a[120];
int main(){
	int n=0;
	for (int i = 2; i <= 101; i++)
		a[i] = 2 * 2 * (i - 1);
	cin >> n;
	LL ans = 0;
	int num;
	for (int i = 1; i <= n; i++) {
		cin >> num;
		ans += a[i] * num;
	}
	cout << ans << endl;
	return 0;
}

B - Kvass and the Fair Nut

 CodeForces - 1084B 

给你N个酒桶,一个S升酒杯,N个酒桶里Vi升的酒,问怎么倒到S升酒杯中,酒桶中至少还有多少升的酒。(中文理解题)

多种方法:①二分 ②简单计算

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<utility>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<functional>
using namespace std;
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define PI 3.14159265359
typedef long long LL;
const int MAXN = 1e3;
LL a[12000];
int main(){
	LL n,minn=INF;
	LL sum = 0,num=0;
	cin >> n >> num;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (minn > a[i])
			minn=a[i];
		sum += a[i];
	}
	if (sum < num)
		cout << "-1" << endl;
	else {
		sum -= minn*n;
		if (sum < num){
			num -= sum;
			int k = num / n;
			int t = num%n;
			if (t == 0)
				cout << minn - k<<endl;
			else
				cout << minn - k - 1<<endl;
		}
		else
			cout << minn << endl;
	}
	
	return 0;
}

C - The Fair Nut and String

 CodeForces - 1084C 

构成两种字符串:①全是a的;②两个a之间至少包含一个b的。

#include<bits/stdc++.h>
#define ll long long
const int mod = 1e9 + 7;
using namespace std;
 
int main()
{
	string str;
	ll ans = 0;
	cin>>str;
	str += 'b';
	int len = str.length();
	ans = 1;
	ll num1 = 1;
	for(int i = 0; i < len; i++){
		if(str[i] == 'a') num1++;
		else if(str[i] == 'b'){
			ans = ans * num1 % mod;
			num1 = 1;
		}
	}
	ans -= 1;
	cout << ans << endl;
}

D - The Fair Nut and the Best Path

 CodeForces - 1084D 

坚果在树上旅游,每个节点可以加油,起点和终点都可以加,路上的距离会消耗等数量的油,坚果要在树上选择一条路径,使得最后到达终点的油的总量是最大的,路上油不能消耗完,同时也不能反复走同一条路。可以只选一个点。

方法:树形dp

#include <bits/stdc++.h>
const int maxn = int(3e5) + 7;
std::vector<std::pair<int, long long>> edge[maxn];
long long ans = 0, f[maxn];
int n, w[maxn];
void dfs(int u, int pre) {
    f[u] = w[u];
    long long max[2] = {0, 0};
    for (auto cur : edge[u]) {
        int v = cur.first;
        long long cost = cur.second;
        if (v != pre) {
            dfs(v, u);
            long long tmp = f[v] - cost;
            if (tmp > max[0]) std::swap(max[0], tmp);
            max[1] = std::max(max[1], tmp);
            f[u] = std::max(f[u], f[v] - cost + w[u]);
        }
    }
    ans = std::max({f[u], ans, max[0] + max[1] + w[u]});
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", w + i);
    for (int i = 1, u, v, cost; i < n; i++) {
        scanf("%d%d%d", &u, &v, &cost);
        edge[u].emplace_back(v, cost);
        edge[v].emplace_back(u, cost);
    }
    dfs(1, 1);
    printf("%lld\n", ans);
    return 0;
}

E - The Fair Nut and Strings

 CodeForces - 1084E 

在所有长度为n的,字典序介于s和t逐渐的,只有由a,b组成的字符串中,选取k个,然后让它们前缀组成的集合最大。其实就是给你一个有左右边界且叶子数量必须小于等于 k个的字典树,最多能有多少个节点,模拟一下即可。

题解:

https://blog.youkuaiyun.com/BUAA_Alchemist/article/details/85058945

F - Max Mex

 CodeForces - 1084F 

一棵n个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~n-1的排列),要求找到一条路径,使得路径的mex最大。

题解:

http://www.cnblogs.com/mjtcn/p/10101996.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值