当前位置:网站首页>CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
2022-04-22 06:16:00 【INGg__】
A - Good Pairs
题意
找一个数对满足题目所给的公式
输出下标
题解
输出最大值和最小值的下标就行了,因为本质上就是这个点到两端的距离之和
Code
int T;
int n;
PII a[N];
void solve(){
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i].x;
a[i].y = i;
}
sort(a + 1, a + 1 + n);
cout << a[1].y << ' ' << a[n].y << endl;
}
B - Subtract Operation
题意
每次选择一个数对全体数组的数来相减,问最后剩一个数能不能时k
题解
本质上,每次操作过后两个数的相对大小关系是不会改变的。
所以说,最后剩的两个数是多少,与他们最初的差是一样的
所以对于每一个数检查一下有没有与之对应的数使得两者相加为k
Code
int T;
ll n, k;
int a[N];
void solve(){
cin >> n >> k;
// ll summ = 0;
map<ll, int> m;
int maxx = 0;
for (int i = 1; i <= n; i++){
cin >> a[i];
maxx = max(a[i], maxx);
m[a[i]]++;
}
for (ll i = 1; i <= n; i++){
if(m[a[i] + k]){
cout << "YES" << endl;
re;
}
}
cout << "NO" << endl;
}
C - Make Equal With Mod
题意
给定数组,可以选取一个数 2 ≤ k 2\leq k 2≤k使得,将数组中的所有元素变为 a i % k a_i \%k ai%k
若干操作后,使得数组各元素相等
题解
因为操作可以无限次,那么我们最开始的想法不妨设最后都变成0
那不就都除以这个数本身就完了吗
但是题目要求 2 ≤ k 2\leq k 2≤k,如果数组中出现了1的话,那么肯定是不能除以自己的,而要满足题意,那必须得是让所有的数变成1,也就是 mod 上他自己减一,就可以让他们全部都变成1了
但是如果数组中出现了两个数相差为1的情况,这时候按照上面的方法就会出现0了,之后就不会再相等了
而当数组出现了0,那么还是遵循上面第一条的情况就可以了
如果数组中出现了0和1,就符合第二种说的情况
Code
int T;
int n;
int a[N];
void solve(){
int one = 0;
int ok = 1;
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
if(a[i] == 1)
one = 1;
if(i > 1 && a[i - 1] != a[i])
ok = 0;
}
if(ok){
cout << "YES" << endl;
re;
}
if(one){
sort(a + 1, a + 1 + n);
for (int i = 2; i <= n; i++){
if(a[i] - a[i - 1] == 1){
cout << "NO" << endl;
re;
}
}
cout << "YES" << endl;
re;
}
else cout << "YES" << Endl;
}
D - K-good
题意
给你一个数n,问能不能拆成k个数的和使这k个数模k的结果都相同
输出这个k
题解
假设我们现在n已经被拆成了k个数,即
a 1 + a 2 + a 3 + ⋯ + a k = n a_1+a_2+a_3+\cdots+a_k=n a1+a2+a3+⋯+ak=n
而这些数%k都不相同,而且有k个,那么显然这些数一定都是 0 ∼ k − 1 0 \sim k-1 0∼k−1之间的,那么
n = k ∗ ( k − 1 ) 2 + t k n = k 2 − k + 2 t k 2 n = k 2 ∗ ( k − 1 + 2 t ) n = k ∗ ( k − 1 + 2 t ) 2 n=\frac{k*(k-1)}{2}+tk \\ n=\frac{k^2-k+2tk}{2} \\ n=\frac{k}{2}*(k-1+2t) \\ n=k*\frac{(k-1+2t)}{2} n=2k∗(k−1)+tkn=2k2−k+2tkn=2k∗(k−1+2t)n=k∗2(k−1+2t)
若k为奇数,则 k − 1 + 2 t k-1+2t k−1+2t为偶数;若k为偶数,则 k − 1 + 2 t k-1+2t k−1+2t为奇数
即无论怎样,2n必定为一寄一偶相乘得到的
那么我们可以把这个偶数求出来,当2n不能再被2整除的时候,奇数也就出来了
需要注意的是n构造的式子中,已经减了一个2的质因子
最后输出的最小值是因为等式成立的话,k+2t-1一定是大于k的,所以输出最小
Code
ll n;
void solve(){
cin >> n;
ll tmp = n;
int cnt = 1;
while(tmp % 2 == 0)
tmp /= 2, cnt++;
if(tmp == 1) // 不满足一寄一偶
cout << -1 << endl;
else{
cout << min(tmp, 1ll << (cnt)) << endl;
}
cout << endl;
}
E - Equal Tree Sums
参考https://www.zhihu.com/people/pzr-84
void dfs(int u, int fa){
if(fa != -1)
col[u] = !col[fa];
else
col[u] = 1;
for(auto v: g[u]){
if(v != fa && !col[v]){
dfs(v, u);
}
}
}
void solve(){
cin >> n;
for (int i = 1; i <= n; i++){
d[i] = col[i] = 0;
g[i].clear();
}
for (int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
d[a]++;
d[b]++;
g[a].pb(b);
g[b].pb(a);
}
dfs(1, -1);
for (int i = 1; i <= n; i++){
if(col[i])
cout << -d[i] << ' ';
else
cout << d[i] << ' ';
}
cout << endl;
}
版权声明
本文为[INGg__]所创,转载请带上原文链接,感谢
https://blog.csdn.net/INGg__/article/details/123774328
边栏推荐
- 八大排序的思想及其代码
- synchronized锁优化的一些机制(锁升级)
- In the process of class loading, the allocation area of class variables is different from that of instance variables
- If an error is reported, process the ES6 filter to filter the array
- 【数论】同余(一):同余的基本概念与性质
- instanceof的使用说明及实例讲解
- 2021学习计划
- Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
- (5) Use Navicat to create database data table and set ID auto increment
- Vscode, this is enough
猜你喜欢

LeetCode - 7 - (二叉树的最近公共祖先、轮转数组、二叉树的直接、下一个排列、组合总和)

L2-005 集合相似度(set判重)

快排与归并排序

Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)

LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)

最强操作符学习之路(C语言详解)

最强数组学习之路

LaTex中插入图片报错Unknown graphics extension: .1.jpg. }

C language | quick sorting

L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)
随机推荐
[number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
Add author photos and author profiles to latex
JVM中唯一一个不会发生GC和OOM的存储区域
Codeforces Round #774 (Div. 2)
Idea does not display the run dashboard view window
快排与归并排序
Vscode, this is enough
C language | quick sorting
1. Compile the following three modules of student information management system: and detect the implementation. 1. Add student information 4. Query student information 5. Query all student information
Prompt the user to enter his name, and the user will write his name to the file guest Txt program determines that when it is not equal to N, it executes to create the file data Txt, a total of 100000
区间求和的问题——差分
字节暑期实习一面——20220304
Redis Advanced
LaTex中插入图片报错Unknown graphics extension: .1.jpg. }
The only storage area in the JVM where GC and oom will not occur
SQL复习语法笔记整理,新鲜出炉
Byte Summer Internship - 20220304
P1095 [NOIP2007 普及组] 守望者的逃离
[number theory] congruence (2): inverse element
2021学习计划