1453: [蓝桥杯2019初赛]数列求值
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[30000000];
int main() {
a[1] = a[2] = a[3] = 1;
for(int i = 4; i <= 20190324; i ++) {
a[i] = (a[i-1]%10000 + a[i-2]%10000 + a[i-3]%10000)%10000;
}
cout << a[20190324] << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "4659" << endl;
}
1455: [蓝桥杯2019初赛]迷宫
#include <bits/stdc++.h>
using namespace std;
string ans;
bool vis[40][60];
int a[40][60], dp[40][60], vv[99999];
int minn = 999999;
int add[4][2] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};
char s[5] = {'D', 'D', 'L', 'R', 'U'};
void dfs(int x, int y, int steps){
if(steps > minn) return;
if(x == 30 && y == 50) {
if(steps < minn) {
minn = steps;
string tmp;
for(int i = 1; i < steps; i++){
tmp += s[vv[i]];
}
ans = tmp;
}
return;
}
for(int i = 1; i <= 4; i ++) {
int tx = x + add[i-1][0];
int ty = y + add[i-1][1];
if(tx < 1 || ty < 1 || tx > 30 || ty > 50) continue;
if(vis[tx][ty] || a[tx][ty] == 1) continue;
if(steps + 1 > dp[tx][ty]) return ;
dp[tx][ty] = steps + 1;
vis[tx][ty] = 1; vv[steps] = i;
dfs(tx, ty, steps + 1);
vis[tx][ty] = 0;
}
}
int main() {
for(int i = 1; i <= 30; i ++) {
for(int j = 1; j <= 50; j ++) {
dp[i][j] = 999999;
}
}
for(int i = 1; i <= 30; i ++) {
for(int j = 1; j <= 50; j ++) {
a[i][j] = (getchar() - '0');
}
getchar();
}
vis[1][1] = 1;
dfs(1, 1, 1); cout << ans;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR" << endl;
}
1457: [蓝桥杯2019初赛]完全二叉树的权值
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL N, ans, deep = 1, adp;
LL a[500010];
int main() {
cin >> N;
for(int i = 1; i <= N; i ++) {
cin >> a[i];
}
LL step = 2; ans = a[1];
for(int i = 1; i <= N; i += step / 2) {
LL re = 0; deep ++;
for(int j = i + 1; j <= i + step; j ++) {
re += a[j];
}
step *= 2;
if(ans < re) {
ans = re; adp = deep;
}
}
cout << adp << endl;
return 0;
}
1462: [蓝桥杯2019初赛]组队
#include <bits/stdc++.h>
using namespace std;
int mp[20][10], ans = -10000;
int main() {
for(int i = 1; i <= 20; i ++) {
for(int j = 0; j <= 5; j ++){
cin >> mp[i][j];
}
}
for(int a = 1; a <= 20; a ++) {
for(int b = a+1; b <= 20; b ++) {
for(int c = b+1; c <= 20; c ++) {
for(int d = c+1; d <= 20; d ++) {
for(int e = d+1; e <= 20; e ++) {
ans = max(ans, mp[a][1]+mp[b][2]+mp[c][3]+mp[d][4]+mp[e][5]);
}
}
}
}
}
cout << ans << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << 490 << endl;
}
1463: [蓝桥杯2019初赛]年号字串
#include <bits/stdc++.h>
using namespace std;
int a[10], k = 1;
int main() {
int n = 2019;
while(n != 0) {
a[k ++] = n % 26;
n /= 26;
}
for(int i = k-1; i >= 1; i --) {
char ch = 'A' + a[i] - 1;
cout << ch;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "BYQ" << endl;
}
1464: [蓝桥杯2019初赛]数的分解
#include <bits/stdc++.h>
using namespace std;
bool judge(int n) {
while(n) {
int temp = n%10;
if(temp==2 || temp==4) return false;
n = n/10;
}
return true;
}
int main() {
int ans = 0;
for(int i = 1; i <= 2019; i ++) {
for(int j = 1; j < i; j ++) {
for(int k = 1; k < j; k ++) {
if(judge(i) && judge(j) && judge(k)) {
if(i!=j && i!=k && j!=k){
if(i+j+k == 2019) ans ++;
}
}
}
}
}
cout << ans << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << 40785 << endl;
}
1465: [蓝桥杯2019初赛]特别数的和
#include <bits/stdc++.h>
using namespace std;
bool check(int x) {
int t = x;
while(t != 0) {
int ct = t % 10;
t /= 10;
if(ct==2 || ct==0 || ct==1 || ct==9)
return true;
}
return false;
}
int main() {
int n, ans = 0; cin >> n;
for(int i = 1; i <= n; i ++) {
if(check(i)) {
ans += i;
}
}
cout << ans << endl;
return 0;
}
1466: [蓝桥杯2019初赛]等差数列
#include <bits/stdc++.h>
using namespace std;
const int N = 1000100;
int a[N];
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
int main() {
int n; cin >> n;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
}
sort(a+1, a+1+n);
for(int i = 2; i <= n; i ++) {
a[i] -= a[1];
}
int d = a[2];
for(int i = 3; i <= n; i ++) {
d = gcd(d, a[i]);
}
if(d) cout << a[n]/d+1 << endl;
else cout << n << endl;
return 0;
}
1467: [蓝桥杯2019初赛]后缀表达式
题目的主要意思是,给定N 个加号、M 个减号以及N + M + 1 个整数A1,A2,…,AN+M+1 ,小明想知道在所有由这N 个加号、M 个减号以及N + M +1 个整数凑出的合法的后缀表达式中,结果最大的是哪一个?并且需要特别注意以下-109<=Ai<=109,即Ai的范围中包括负数。
主要判断:
没有负号时:最大值为sum。有负号时:需判断:没负数时:最大值就是所有数的绝对值相加减去最小值,有负数时:需判断:全为负数时:最大值就是所有数的绝对值相加减去绝对值最先的值(思路和没负数时相似)不全为负数时:最大值为所有数的绝对值相加。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
LL a[N];
int n, m;
int main() {
LL sum = 0, f = 0;
cin >> n >> m;
for(int i = 0; i < n+m+1; i ++) {
cin >> a[i];
sum += a[i];
if(a[i] < 0) f ++;
}
sort(a, a+n+m+1);
if(m) {
if(f == 0) {
sum -= (a[0]*2);
}
else {
if((m+n+1) != f) {
for(int i = 0; i < f; i ++) {
sum -= (a[i]*2);
}
}
else {
sum =- sum;
sum += (a[n+m]*2);
}
}
}
cout << sum;
return 0;
}
1468: [蓝桥杯2019初赛]灵能传输
由题意可知,你控制着 n 名高阶圣堂武士,方便起见标为 1,2,··· ,n。每名高阶圣堂武士需要一定的灵能来战斗,每个人有一个灵能值 a i 表示其拥有的灵能的多少。现在系统赋予了你的高阶圣堂武士一个能力,传递灵能,每次你可以选择一个 i ∈ [2,n − 1],若ai ≥ 0 则其两旁的高阶圣堂武士,也就是 i − 1、i + 1 这两名高阶圣堂武士会从
i 这名高阶圣堂武士这里各抽取 ai 点灵能;若 ai < 0 则其两旁的高阶圣堂武士,也就是 i−1,i+1 这两名高阶圣堂武士会给 i 这名高阶圣堂武士 − ai 点灵能。形式化来讲就是 ai−1 += ai, ai+1 += ai, ai −= 2ai。灵能是非常高效的作战工具,同时也非常危险且不稳定,一位高阶圣堂武士拥有的灵能过多或者过少都不好,定义一组高阶圣堂武士的不稳定度为 max ni=1 |ai |,请你通过不限次数的传递灵能操作使得你控制的这一组高阶圣堂武士的不稳定度最小。
题目最终转换为让相邻前缀和之差的最大值最小,即ans=max{ ( |s1-s0|,|s2-s1|…|sn-sn-1| ) },然后根据题目,我们需要知道的是,导致不稳定度大的原因是什么。观察后可以发现,不稳定度大的原因很可能是因为点的分布不均匀导致的。通过观察样例可以得到,点的分布越是稀疏,相邻前缀和间的差值往往越大,于是使用贪心的思想,并且使用某种策略使得点的分布均匀起来这样可以使得相邻前缀和间的差值可以趋于中庸也就是我们所需要的最优解。而这种策略便是视频中所说的排序后记录下s0,s1的位置,分别从s0和s1开始隔一个跳一次的将数值存入res数组中即可得到最优点的分布,最终点的分布一定是尽可能均匀的。
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
typedef long long LL;
LL s[N], res[N];
bool v[N];
int main() {
int t; cin >> t;
while(t --) {
int n; cin >> n;
memset(s, 0, sizeof(s));
memset(res, 0, sizeof(res));
memset(v, 0, sizeof(v));
for(int i = 1; i <= n; i ++) {
cin >> s[i];
s[i] += s[i-1];
}
LL s0 = s[0], s1 = s[n];
if (s0 > s1) swap(s0, s1);
sort(s, s+n+1);
for(int i = 0; i <= n; i ++) {
if (s0 == s[i]) {
s0 = i;
break;
}
}
for(int i = 0; i <= n; i ++) {
if (s1 == s[i]) {
s1 = i;
break;
}
}
int l = 0, r = n;
for(int i = s0; i >= 0; i -= 2) {
res[l ++] = s[i];
v[i] = 1;
}
for(int i = s1; i <= n; i += 2) {
res[r --] = s[i];
v[i] = 1;
}
for(int i = 0; i <= n; i ++) {
if(!v[i]) {
res[l ++] = s[i];
v[i] = 1;
}
}
LL ans = 0;
for (int i = 1; i <= n; i ++) {
ans = max(ans, abs(res[i] - res[i - 1]));
}
cout << ans << '\n';
}
return 0;
}