当前位置:网站首页>AtCoder Beginner Contest 255
AtCoder Beginner Contest 255
2022-06-30 22:19:00 【Chels.】
C - ±1 Operation 1
Title Description :
I'll give you an arithmetic sequence , The first item is
A, The tolerance isD, The number of items isN, askXAnd hereNWhat is the minimum absolute value of the number difference in the item
Ideas :
Two minutes , Find the two nearest to him , Just find a minimum
Note that the tolerance may be negative , So we can discuss it according to the situation , Or you can put this
NIf you look backward at the number, it becomes an equal difference sequence with a positive toleranceBe careful , If you find greater than or equal to after two points
xThe position of is the first item or n+1 term , The answer is determined by only one number , Otherwise, take the minimum value of the left and right , We need a special verdict
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define mod7 1000000007
#define mod9 998244353
#define m_p(a,b) make_pair(a, b)
#define mem(a,b) memset((a),(b),sizeof(a))
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(a) cout << "Debuging...|" << #a << ": " << a << "\n";
typedef long long ll;
typedef pair <int,int> pii;
#define MAX 300000 + 50
ll n, a, d, x;
ll f(ll n){
return a + (n - 1) * d;
}
void work(){
cin >> x >> a >> d >> n;
ll l = 1, r = n;
while(l <= r){
ll mid = (l + r) / 2;
if(d >= 0){
if(f(mid) > x)r = mid - 1;
else if(f(mid) < x)l = mid + 1;
else {
cout << 0 << endl;
return;
}
}
else{
if(f(mid) < x)r = mid - 1;
else if(f(mid) > x)l = mid + 1;
else {
cout << 0 << endl;
return;
}
}
}
if(d >= 0){
ll id = l - 1;
if(!id)cout << a - x << endl;
else if(id == n)cout << x - f(n) << endl;
else{
cout << min(x - f(id), f(id + 1) - x) << endl;
}
}
else{
ll id = r + 1;
if(id == 1)cout << x - a << endl;
else if(id == n + 1)cout << f(n) - x << endl;
else{
cout << min(f(id - 1) - x, x - f(id)) << endl;
}
}
}
int main(){
io;
work();
return 0;
}
D - ±1 Operation 2
Title Description :
Here's your number A, Several inquiries , Every time you ask for a number
X, You can add or subtract one several times to any number in the sequence , Ask will arrayAAll the figures of becomeXHow many operations are needed
Ideas :
Maintain a prefix and after sorting , For each
X, We divide the array into smaller than X The sum of numbers is greater than or equal to X Number of numbers , Answer these two parts separately , You only need to know the number of numbers and the sum of numbers , So maintain a prefix and
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define mod7 1000000007
#define mod9 998244353
#define m_p(a,b) make_pair(a, b)
#define mem(a,b) memset((a),(b),sizeof(a))
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(a) cout << "Debuging...|" << #a << ": " << a << "\n";
typedef long long ll;
typedef pair <int,int> pii;
#define MAX 300000 + 50
ll n, m, k, x;
ll tr[MAX];
ll sum[MAX];
ll getsum(ll l, ll r){
return sum[r] - sum[l - 1];
}
void work(){
cin >> n >> m;
for(int i = 1; i <= n; ++i)cin >> tr[i];
sort(tr + 1, tr + 1 + n);
for(int i = 1; i <= n; ++i)sum[i] = sum[i - 1] + tr[i];
tr[n + 1] = 1e18;
for(int i = 1; i <= m; ++i){
cin >> x;
ll id = upper_bound(tr + 1, tr + 2 + n, x) - tr;
if(id == 1 || id == n + 1){
cout << abs(n * x - getsum(1, n)) << endl;
}
else{
cout << (id - 1) * x - getsum(1, id - 1) + getsum(id, n) - (n - id + 1) * x << endl;
}
}
}
int main(){
io;
work();
return 0;
}
E - Lucky Numbers
Title Description :
Here you are.
N-1A digitalS[i], as well asMA digitalX[i], Now define the arrayASatisfyA[i] + A[i + 1] = S[i],i=1,2,...,N-1, Asking when M Numbers in the array A How many times does it appear in
Ideas :
It's not hard to find out , When
A[1]After fixing , ArrayAIt's fixed , So we have to find a way toA[i]useA[1]Express it
A[2] = S[1] - A[1]
A[3] = S[2] - A[2] = S[2] - S[1] + A[1]
A[4] = S[3] - A[3] = S[3] - S[2] + S[1] - A[1]We set up
B[i] = S[i] - B[i], i >= 2, B[1] = 0therefore A [ i ] = B [ i ] + ( − 1 ) i + 1 ∗ A [ 1 ] A[i] = B[i] + (-1)^{i+1}*A[1] A[i]=B[i]+(−1)i+1∗A[1]
We put
A[1]separate from , You can get A [ 1 ] = ( − 1 ) i + 1 ∗ ( A [ i ] − B [ i ] ) A[1] = (-1)^{i+1}* (A[i] - B[i]) A[1]=(−1)i+1∗(A[i]−B[i])Because what we want is
A[i] = X[j], So for each of usi, Enumerate eachX[j], Calculate what you wantA[i] = X[j]whenA[1]Value , We use itmapSave the quantity , Last run map, Find the output with the largest second key value , Remember to drivelonglong
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 0x3f3f3f3f
#define mod7 1000000007
#define mod9 998244353
#define m_p(a,b) make_pair(a, b)
#define mem(a,b) memset((a),(b),sizeof(a))
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(a) cout << "Debuging...|" << #a << ": " << a << "\n";
typedef long long ll;
typedef pair <int,int> pii;
#define MAX 300000 + 50
ll n, m, k, x;
ll s[MAX];
ll br[MAX];
void work(){
cin >> n >> m;
for(int i = 1; i < n; ++i){
cin >> s[i];
}
for(int i = 2; i <= n; ++i){
br[i] = s[i - 1] - br[i - 1];
}
map<ll, ll>mp;
for(int j = 1; j <= m; ++j){
cin >> x;
for(int i = 1; i <= n; ++i){
++mp[(x - br[i]) * (i % 2 == 0 ? -1 : 1)];
}
}
ll ans = 0;
for(auto [x, y] : mp)ans = max(ans, y);
cout << ans << endl;
}
int main(){
io;
work();
return 0;
}
边栏推荐
- Is there a shortage? No need to download the free online resources! 2022 favorites must have it!
- HDFS集中式缓存管理(Centralized Cache Management)
- Analysis of doctor Aifen's incident
- Technical principle of decentralized exchange system development - digital currency decentralized exchange system development (illustrative case)
- Do machine learning jobs require graduate students?
- Interesting plug-ins summary
- Develop technology - get time 10 minutes ago
- Go Web 编程入门: 一探优秀测试库 GoConvey
- What if the taskbar is blank after win11 update? Solution to blank and stuck taskbar after win11 update
- A new one from Ali 25K came to the Department, which showed me what the ceiling is
猜你喜欢

基于kubernetes平台微服务的部署

Zhoushaojian, rare

B_ QuRT_ User_ Guide(33)

部门新来了个阿里25K出来的,让我见识到了什么是天花板

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

WinDbg debugging tool introduction

Uniapp rich text editor

Vite2 is compatible with lower versions of chrome (such as Sogou 80). Some grammars requiring higher versions are processed through polyfills

Modify the name of the launched applet

深入解析 Apache BookKeeper 系列:第四篇—背压
随机推荐
dba
The Three Musketeers: One for All!
Where can I find the computer device manager
Using Obsidian with Hugo, markdown's local editing software is seamlessly connected with online
Docker installing MySQL
JVM Part 21 of interview with big companies Q & A
Technical principle of decentralized exchange system development - digital currency decentralized exchange system development (illustrative case)
RP prototype resource sharing - shopping app
Spark - understand partitioner in one article
Pytorch quantitative perception training (qat) steps
Installing jupyter notebook under Anaconda
阿婆做的臭豆腐
Some memory problems summarized
Discuz forum speed up to delete XXX under data/log PHP file
Uniapp life cycle / route jump
[introduction to MySQL] the first conversation · first time in the "database" Mainland
请指教同花顺软件究竟是什么?另外想问,现在在线开户安全么?
[micro service ~nacos] configuration center of Nacos
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
Pytorch quantitative practice (1)