Codeforces Round #636 (Div. 3) A-D

news/2025/3/25 13:27:46/

A. Candies

题意

给定一个整数,判断是否存在在这里插入图片描述

思路

先对公式进行预处理 明显是一个等比数列
化简后得到
在这里插入图片描述
因为答案一定存在 所以用快速幂从小到大枚举即可

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define eps 1e-6
#define mem(n,a) memset(n,a,sizeof(n))
#define rep(i,be,en) for(int i=be;i<=en;++i)
#define pre(i,be,en) for(int i=en;i>=be;--i)
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) { return x & -x; }using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100010;LL quick_pow(int a, int b) {int ans = 1;while (b) {if (b & 1)ans *= a;a *= a;b >>= 1;}return ans;
}
int main() {int t;cin >> t;while (t--) {int n;cin >> n;for (int i = 2;i <= 32;++i) {int t = quick_pow(2, i) - 1;if (n % t == 0) {printf("%d\n", n / t);break;}}}return 0;
}

B. Balanced Array

题意

构造一个长度为n的数列,n为偶数,使得前[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S4DY4w2x-1594654027477)(https://www.nowcoder.com/equation?tex=%5Cdfrac%20%7Bn%7D%7B2%7D "图片标题")] 个数全为偶数
在这里插入图片描述个数全为奇数
并且使前后区间和相等

思路

观察样例可知 n/2为奇数时不能构造出来
n/2为偶数时 对前面的偶数按顺序构造,后面的n/2-1个奇数也按顺序构造,用最后一个奇数补足与偶数和的差值使前后区间和相等
容易知道最后一个奇数应该为在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define eps 1e-6
#define mem(n,a) memset(n,a,sizeof(n))
#define rep(i,be,en) for(int i=be;i<=en;++i)
#define pre(i,be,en) for(int i=en;i>=be;--i)
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) { return x & -x; }using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100010;int main() {int t;cin >> t;while (t--) {int n;cin >> n;if ((n / 2) & 1)puts("NO");else {puts("YES");int t = 2;for (int i = 1;i <= n / 2; ++i)printf("%d ", t), t += 2;t = 1;for (int i = 1;i <= n / 2 - 1; ++i)printf("%d ", t), t += 2;printf("%d\n", n / 2 - 1 + n);}}return 0;
}

C. Alternating Subsequence

题意

从给定序列中选择一个正负交替的子序列使得子序列的和最大

思路

双指针,每次扫过一个正负号相同的区间,找到区间的最大值作为子序列的元素

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define eps 1e-6
#define mem(n,a) memset(n,a,sizeof(n))
#define rep(i,be,en) for(int i=be;i<=en;++i)
#define pre(i,be,en) for(int i=en;i>=be;--i)
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) { return x & -x; }using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 200010;int a[N];int np(int a) {if (a < 0)return 1;else return 2;
}int main() {int t;cin >> t;while (t--) {int n;cin >> n;for (int i = 1;i <= n;++i)cin >> a[i];LL ans = 0;int s, p, maxn;p = 1;while (p <= n) {s = p;maxn = a[p];while (np(a[s]) == np(a[p]) && p <= n) {maxn = max(maxn, a[p]);++p;}ans += maxn;}cout << ans << endl;}return 0;
}

D. Constant Palindrome Sum

题意

给一个长度为n的数列和一个k 要求用不大于k的数替换数组中的任意一个数 使 所有关于数组对称的两个位置的元素和相等 即在这里插入图片描述      问最少替换多少次

思路

刚开始我想先算出每两个数对的和出现次数最多的当作x 再根据x与其他组数对的大小计算答案 后来发现这种方法是不可行的 因为每个数对和都不一样的情况是可能出现的 这时候无法判断到底选哪个作为x更合适
后来 看到别人的博客 才想起来下面这种方法
即 算出每个数对的和 并且记录该数对每一个x的贡献 再选择一个最小的当作答案
很容易分情况讨论
minn = min(a[i], a[n - i + 1])
maxn = max(a[i], a[n - i + 1])
t = a[i] + a[n - i + 1]
delta代表差分数组
因为数组元素的范围是 [1,k]
所以数对的取值范围为 [2,2*k]
①x取 [2,minn] 时 即使较大的那个数改成了1 还是不符合这个范围 所以需要两个都改变
②x取 [minn + 1,maxn + k] 时,只需要改变其中一个数字即可
③x取 [maxn + k + 1, 2 * k] 时,两个都需要改变
④因为当数对和为x时 不需要改变任何一个数字 而第二部已经把这种情况包括了 所以需要减去 即 delta[x]–

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define eps 1e-6
#define mem(n,a) memset(n,a,sizeof(n))
#define rep(i,be,en) for(int i=be;i<=en;++i)
#define pre(i,be,en) for(int i=en;i>=be;--i)
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) { return x & -x; }using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 200010;int a[N], delta[N << 1];int main() {int t;cin >> t;while (t--) {memset(delta, 0, sizeof delta);int n, k;cin >> n >> k;for (int i = 1;i <= n;++i)cin >> a[i];for (int i = 1;i <= n / 2;++i) {int t = a[i] + a[n - i + 1];int minn = min(a[i], a[n - i + 1]);int maxn = max(a[i], a[n - i + 1]);delta[2] += 2;delta[minn + 1] -= 2;delta[maxn + k + 1] += 2;delta[2 * k + 1] -= 2;delta[minn + 1] ++;delta[maxn + k + 1]--;delta[x]--;delta[x + 1]++;}LL ans = INF;for (int i = 2;i <= 2 * k;++i) {delta[i] += delta[i - 1];ans = min(ans, (LL)delta[i]);}cout << ans << endl;}return 0;
}

http://www.ppmy.cn/news/712793.html

相关文章

Codeforces Round #636 (Div. 3) D.Constant Palindrome Sum

Codeforces Round #636 (Div. 3) D.Constant Palindrome Sum 题目链接 You are given an array a consisting of n integers (it is guaranteed that n is even, i.e. divisible by 2). All ai does not exceed some integer k. Your task is to replace the minimum number …

Codeforces Round #633 (Div. 2) B.Sorted Adjacent Differences

Codeforces Round #633 (Div. 2) B.Sorted Adjacent Differences 题目链接 You have array of n numbers a1,a2,…,an. Rearrange these numbers to satisfy |a1−a2|≤|a2−a3|≤…≤|an−1−an|, where |x| denotes absolute value of x. It’s always possible to find su…

Codeforces Round #633 (Div. 2) C.Powered Addition

Codeforces Round #633 (Div. 2) C.Powered Addition 题目链接 You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: Select some distinct indices i1,i2,…,ik which are between 1…

ubuntu 20.04挂载ntfs磁盘

ubuntu 20.04挂载ntfs磁盘步骤如下&#xff1a; 查看当前用户id和组id&#xff0c;记下uid和gid id显示结果如下&#xff1a; uid1000(scue) gid1000(scue) 组1000(scue),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),124(sambashare),125(vboxusers),1001(u…

d3和弦图

效果图&#xff1a; 高亮效果&#xff1a; //chord.js,使用时直接引入&#xff0c;调用 //引入依赖d3,color.js是我自己的配色方案,可以直接使用d3的配色方案&#xff0c;例&#xff1a;d3.schemeSet3 define([d3.v5.min.js,../color.js,],function(d3,color){var relation…

Codeforces Round #833 (Div. 2)E. Yet Another Array Counting Problem(笛卡尔树+树形DP)

题目链接&#xff1a;Problem - E - Codeforces 样例输入&#xff1a; 4 3 3 1 3 2 4 2 2 2 2 2 6 9 6 9 6 9 6 9 9 100 10 40 20 20 100 60 80 60 60样例输出&#xff1a; 8 5 11880 351025663题意&#xff1a;给定一个长度为n的数组a[],对于每一个区间[l,r]&#xff0c;这个…

d313(d3131)

动车D313从苏州哪里&#xff1f;动车D313从苏州哪里坐 苏州火车南站 就是苏州新建的动车站 D313无锡到上海虹桥转乘D377上海虹桥到雁荡山&#xff01;1、问&#xff1a;D3? 换乘是要出站再进站的~ 北京至苏州D313次火车是在北京南站上车吗 北京——苏州&#xff1a;D313次动车…

MOOG伺服阀D633-7205

MOOG伺服阀D633-7205是一种节流控制阀&#xff0c;可应用于三通、四通和2*2通。具有快速性好、单位重量输出功率大、传动稳定、抗干扰能力强等特点。另一方面&#xff0c;在伺服系统中&#xff0c;传递信号和补偿特性时多使用电气部件。因此&#xff0c;现代高性能伺服系统都采…