Codeforces Round #725 (Div. 3) A-D,F题解
A. Stone Game
思路:总共3种情况,都从最左端被拿走,都从最右端被拿走,左侧的从最左端被拿走且右侧的从最右端被拿走,取最小值即可
代码:
//CF-725.A
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair<int, int>P;
typedef vector<int>vec;
#define INF 0x3f3f3f3f
const double EPS = 1e-18;
const int MOD = 1e9 + 7;
const int maxn = 8e4 + 1;
int T;
int N, A[maxn];
void solve()
{
int maxi = -1, mini = -1, maxa = -1, mina = INF;
for (int i = 0; i < N; i++)
{
if (A[i] < mina)
{
mina = A[i];
mini = i;
}
if (A[i] > maxa)
{
maxa = A[i];
maxi = i;
}
}
if (mini < maxi)
{
int ans[3];
ans[0] = mini + 1 + N - maxi;
ans[1] = max(mini, maxi) + 1;
ans[2] = N - min(mini, maxi);
int res = INF;
for (int i = 0; i < 3; i++)
res = min(res, ans[i]);
cout << res << endl;
return;
}
else
{
int ans[3];
ans[0] = maxi + 1 + N - mini;
ans[1] = max(mini, maxi) + 1;
ans[2] = N - min(mini, maxi);
int res = INF;
for (int i = 0; i < 3; i++)
res = min(res, ans[i]);
cout << res << endl;
return;
}
}
int main()
{
IOS;
cin >> T;
while (T--)
{
cin >> N;
for (int i = 0; i < N; i++)
cin >> A[i];
solve();
}
return 0;
}
B. Friends and Candies
思路:只能是从糖多的人那里把糖分给糖少的,所以只要求出糖数大于平均的人数即可,当糖数不能被人数整除时输出-1。
代码:
//CF-725.B
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair<int, int>P;
typedef vector<int>vec;
#define INF 0x3f3f3f3f
const double EPS = 1e-18;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 1;
int T;
ll N, A[maxn];
void solve()
{
int sum = 0, ans = 0;
int avg;
for (int i = 0; i < N; i++)
sum += A[i];
if (sum % N)
{
cout << -1 << endl;
return;
}
avg = sum / N;
for (int i = 0; i < N; i++)
{
if (A[i] > avg)
ans++;
}
cout << ans << endl;
}
int main()
{
IOS;
cin >> T;
while (T--)
{
cin >> N;
for (int i = 0; i < N; i++)
cin >> A[i];
solve();
}
return 0;
}
C. Number of Pairs
又是因为没开longlongWA了半天。。。
思路:分治法,不断将数列分为左右两个数列,完全在其中一个数列中的数对数量可以递归求得。合并的同时将数列排序,对于跨两个数列的数对,只需枚举左边数列中的数,在右边找加上它之后处于区间内的数的个数即可。
代码:
//CF-725.C
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair<int, int>P;
typedef vector<int>vec;
#define INF 0x3f3f3f3f
const double EPS = 1e-18;
const int MOD = 1e9 + 7;
const int maxn = 1e9 + 1;
int T;
ll n, l, r;
vec A;
ll merge_solve(vec& a)
{
ll ans = 0;
ll d = a.size();
if (d <= 1)
return 0;
vec b(a.begin(), a.begin() + d / 2);
vec c(a.begin() + d / 2, a.end());
ans += merge_solve(b);
ans += merge_solve(c);
ll ai = 0, bi = 0, ci = 0;
while (ai < d)
{
if (bi < b.size() && (ci == c.size() || b[bi] < c[ci]))
{
if (b[bi] <= r)//只有小于右边界才可能可以
{
int hi = r - b[bi], lo = l - b[bi] - 1;
ans += upper_bound(c.begin(), c.end(), hi) - upper_bound(c.begin(), c.end(), lo);
}
a[ai++] = b[bi++];
}
else
a[ai++] = c[ci++];
}
return ans;
}
void solve()
{
cout << merge_solve(A) << endl;
}
int main()
{
IOS;
cin >> T;
while (T--)
{
A.clear();
cin >> n >> l >> r;
for (int i = 0; i < n; i++)
{
int num;
cin >> num;
A.push_back(num);
}
solve();
}
return 0;
}
D. Another Problem About Dividing Numbers
思路:当K==1时,如果A和B不相等并且其中一个能整除另外一个时才YES,否则NO。当K!=1时,计算操作次数的上限,当把每个数都除到1的时候操作次数最多,所以直接统计每个数的质因数个数即可,两个数质因数的和M就是允许操作的最大次数。小于这个次数的操作显然都可以完成。如果K<=M,就YES,否则NO。
代码:
//CF-725.D
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair<int, int>P;
typedef vector<int>vec;
#define Y "YES"
#define N "NO"
#define INF 0x3f3f3f3f
const double EPS = 1e-18;
const int MOD = 1e9 + 7;
const int maxn = 1e9 + 1;
int T, A, B, K;
vec prime;
bool isprime(int n)
{
if (n == 0 || n == 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
{
if (!(n % i))
return false;
}
return true;
}
void solve()
{
int numa = 0, numb = 0, index = 0;
if (K == 1)
{
if ((A != B) && (A % B == 0 || B % A == 0))
{
cout << Y << endl;
return;
}
else
cout << N << endl;
}
else
{
while (A != 1 && index < prime.size())
{
if (!(A % prime[index]))
{
A /= prime[index];
numa++;
}
else
index++;
}
if (A != 1)
numa++;
index = 0;
while (B != 1 && index < prime.size())
{
if (!(B % prime[index]))
{
B /= prime[index];
numb++;
}
else
index++;
}
if (B != 1)
numb++;
int times = numa + numb;
cout << (times >= K ? Y : N) << endl;
}
}
int main()
{
IOS;
for (int i = 0; i <= 1e5 + 7; i++)
{
if (isprime(i))
prime.push_back(i);
}
cin >> T;
while (T--)
{
cin >> A >> B >> K;
solve();
}
return 0;
}
F. Interesting Function
没想到F题居然这么简单,快结束的时候看见一堆人过了才赶紧去做的。。。
思路:每加1就会有1个数字改变,如果进位了,每进1位就额外有1个数字改变,对于从a加到b,可以看作是从1加到b再减去从1加到a,进位的次数直接分别除以10,100……即可。
代码:
//CF-725.F
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair<int, int>P;
typedef vector<int>vec;
#define Y "YES"
#define N "NO"
#define INF 0x3f3f3f3f
const double EPS = 1e-18;
const int MOD = 1e9 + 7;
const int maxn = 1e9 + 1;
ll T, L, R;
ll A[10] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
void solve()
{
ll ansr = 0, ansl = 0;
for (int i = 0; i < 10; i++)
{
ansr += R / A[i];
ansl += L / A[i];
}
cout << ansr - ansl << endl;
}
int main()
{
IOS;
cin >> T;
while (T--)
{
cin >> L >> R;
solve();
}
return 0;
}
Codeforces Round #725 (Div. 3) A-D,F题解的更多相关文章
- 刷题记录:Codeforces Round #725 (Div. 3)
Codeforces Round #725 (Div. 3) 20210704.网址:https://codeforces.com/contest/1538. 感觉这个比上一个要难. A 有一个n个数 ...
- Codeforces Round #609 (Div. 2)前五题题解
Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...
- Codeforces Round #376 (Div. 2) C D F
在十五楼做的cf..一会一断...比赛的时候做出了ABCF 就没有时间了 之后没看题解写出了D..E是个神奇的博弈(递推或者dp?)看了题解也没有理解..先写了CDF.. C 有n个袜子 每个袜子都有 ...
- Codeforces Round#402(Div.1)掉分记+题解
哎,今天第一次打div1 感觉头脑很不清醒... 看到第一题就蒙了,想了好久,怎么乱dp,倒过来插之类的...突然发现不就是一道sb二分吗.....sb二分看了二十分钟........ 然后第二题看了 ...
- Codeforces Round #556 (Div. 2) D. Three Religions 题解 动态规划
题目链接:http://codeforces.com/contest/1150/problem/D 题目大意: 你有一个参考串 s 和三个装载字符串的容器 vec[0..2] ,然后还有 q 次操作, ...
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...
- Codeforces Round #635 (Div. 2)部分(A~E)题解
虽然打的是div1,但最后半小时完全处于挂机状态,不会做1C,只有个 \(O(n^3)\) 的想法,水了水论坛,甚至看了一下div2的AB,所以干脆顺便写个div2的题解吧,内容看上去还丰富一些(X) ...
- Codeforces Round #676 (Div. 2) A - D个人题解(E题待补)
1421A. XORwice 题目链接:Click Here // Author : RioTian // Time : 20/10/18 #include <bits/stdc++.h> ...
- Codeforces Round #677 (Div. 3) E、G题解
E. Two Round Dances #圆排列 题目链接 题意 \(n\)(保证偶数)个人,要表演一个节目,这个节目包含两种圆形舞蹈,而每种圆形舞蹈恰好需要\(n/2\)个人,每个人只能跳一种圆形舞 ...
随机推荐
- Git安装详解
官网地址: https://git-scm.com/ 查看 GNU 协议,可以直接点击下一步. 选择 Git 安装位置,要求是非中文并且没有空格的目录,然后下一步. Git 选项配置,推荐默认设置,然 ...
- 新年好 takoyaki,期待再次与你相见
一.序 今天是中国农历一年的最后一天,往年都叫年三十,今年没有三十,最后一天是二十九.厨房的柴火味.窗外的鞭炮声还有不远处传来的说笑声,一切都是熟悉味道,新年到了,家乡热闹起来了.平常左邻右舍都是看不 ...
- django入门 01 创建项目
安装django库 pip install django 创建--by 终端 django-admin startproject myproject 通过命令创建的django项目,默认不含templ ...
- 如果遇到继承控件,添加到新项目里在工具栏找不到的情况下,F5启动一下,重新生成是不会有的,要运行成功才有
继承控件只的是cs结尾的那种,类直接继承对应控件,不是usercontrol类型的
- attachEvent
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- axios ajax fetch 区别以及优缺点
将jQuery的ajax.axios和fetch做个简单的比较,所谓仁者见仁智者见智,最终使用哪个还是自行斟酌 1.jQuery ajax $.ajax({ type: 'POST', url: ur ...
- Linux防火墙(iptables/firewalld)
Linux防火墙(iptables/firewalld) 目录 Linux防火墙(iptables/firewalld) 一.iptables 1. iptables概述 2. netfilter和i ...
- gpg 使用入门
1. 生成秘钥 gpg --full-generate-key 等价于 gpg --full-gen-key Real name: yellowconvict为该秘钥的名字,也称为 USER-ID 在 ...
- F WebDriver and 环境配置
https://seleniumhq.github.io/docs/wd.html WEBDRIVER The biggest change in Selenium recently has been ...
- mock测试出现Circular view path [trade_records]: would dispatch back to the current handler URL
这是因为你的Controller中返回的视图名称与你当前的requestMapping名称一样,这并没有很好的解决方案,除非你改掉其中一个名字. 因为springframework test时你并没有 ...