A - The Fair Nut and Elevator
坚果住在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
给你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
构成两种字符串:①全是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
坚果在树上旅游,每个节点可以加油,起点和终点都可以加,路上的距离会消耗等数量的油,坚果要在树上选择一条路径,使得最后到达终点的油的总量是最大的,路上油不能消耗完,同时也不能反复走同一条路。可以只选一个点。
方法:树形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
在所有长度为n的,字典序介于s和t逐渐的,只有由a,b组成的字符串中,选取k个,然后让它们前缀组成的集合最大。其实就是给你一个有左右边界且叶子数量必须小于等于 k个的字典树,最多能有多少个节点,模拟一下即可。
题解:
https://blog.youkuaiyun.com/BUAA_Alchemist/article/details/85058945
F - Max Mex
一棵n个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~n-1的排列),要求找到一条路径,使得路径的mex最大。
题解: