Codeforces Round 919 (Div. 2)(A~D) 题解
Codeforces Round 919 (Div. 2) (A~D)题解
A. Satisfying Constraints
题意:给你一些条件让你求出满足条件的整数有多少个。
模拟即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5+5;
ll Tex = 1, n;
void AC(){
cin >> n;
ll l = 1, r = 1e9;
vector<ll> p;
unordered_map<ll, ll> mp;
for(int i = 1; i <= n; i ++){
ll a, x;
cin >> a >> x;
if(a == 1) l = max(l, x);
else if(a == 2) r = min(r, x);
else p.push_back(x);
}
if(l > r) cout << 0 << endl;
else{
ll ans = r - l + 1;
for(int i = 0; i < p.size(); i ++)
if(p[i] >= l && p[i] <= r && !mp[p[i]]) ans --, mp[p[i]] = 1;
cout << ans << endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin >> Tex;
while(Tex --) AC();
return 0;
}
B. Summation Game
题意:给你一个序列,
前缀和一下然后每个位置讨论即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 4e5+5;
ll Tex = 1, n, x, y, a[MAXN], s[MAXN];
void AC(){
cin >> n >> x >> y;
memset(s, 0, sizeof s);
ll ans = - 1e14;
if(x >= n) ans = 0;
for(int i = 1; i <= n; i ++)
cin >> a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i ++)
s[i] = s[i - 1] + a[i];
for(int i = max(1ll, n - x); i <= n; i ++)
ans = max(ans, s[i] - 2 * (s[i] - s[max(i - y, 0ll)]));
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin >> Tex;
while(Tex --) AC();
return 0;
}
C. Partitioning the Array
赛时写了粗心特判
对于每一组
其实就是找所有对应相邻位置上的数之差的绝对值最大公因数是否为
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5+5;
ll Tex = 1, n, a[MAXN], ans;
void check(ll x){
ll GGcd = 0;
for(int i = 1; i <= x; i ++){
ll Gcd = 0;
for(int k = x + i; k <= n; k += x)
Gcd = __gcd(Gcd, abs(a[k] - a[k - x]));
GGcd = __gcd(GGcd, Gcd);
if(GGcd == 1) return;
}
ans ++;
}
void AC(){
cin >> n;
ans = 0;
for(int i = 1; i <= n; i ++)
cin >> a[i];
if(n == 1){
cout << 1 << endl;
return;
}
for(int i = 1; i <= sqrt(n); i ++){
if(n % i == 0){
check(i);
if(n / i != i) check(n / i);
}
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin >> Tex;
while(Tex --) AC();
return 0;
}
D. Array Repetition
发现最终数组不能超过
开一个结构体数组记录每次新加的数,上一次的数组长度 和 当前数组的长度,每次进行操作
对于每次查询操作,二分查找属于哪一次操作里面,然后
#include<bits/stdc++.h>
using namespace std;
typedef __int128 ll;
const ll MAXN = 105;
ll Tex, n, q, opt, x;
vector<ll> Len;
struct node{
vector<ll> in;
ll len, tmp;
node(){
in.clear();
len = 0, tmp = 0;
}
bool operator < (const node & p) const {return len < p.len;}
}a[MAXN];
inline ll read(){
ll x = 0, f = 1;
char ch = getchar();
while(!isdigit(ch) && ch != '-') ch = getchar();
if(ch == '-') f = -1, ch = getchar();
while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f * x;
}
inline void print(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) print(x / 10);
putchar(x % 10 + '0');
}
void dfs(ll x){
ll idx = lower_bound(Len.begin(), Len.end(), x) - Len.begin();
ll mod = a[idx].tmp + a[idx].in.size();
ll real = x % mod == 0 ? mod : x % mod;
if(real <= a[idx].tmp) dfs(real);
else print(a[idx].in[real - a[idx].tmp - 1]), putchar(' ');
}
void AC(){
n = read(); q = read();
Len.clear();
for(int i = 0; i < 80; i ++){
a[i].in.clear();
a[i].len = 0;
a[i].tmp = 0;
}
Len.push_back(0);
ll cnt = 1, p = 0;
for(int i = 1; i <= n; i ++){
opt = read(); x = read();
if(p) continue;
if(opt == 1) a[cnt].in.push_back(x);
else{
a[cnt].len = (a[cnt].tmp + a[cnt].in.size()) * (x + 1);
Len.push_back(a[cnt].len);
if(a[cnt].len > 1e18) p = 1;
cnt ++;
a[cnt].tmp = a[cnt - 1].len;
}
}
a[cnt].len = a[cnt].tmp + a[cnt].in.size();
Len.push_back(a[cnt].len);
while(q --){
x = read();
dfs(x);
}
putchar('\n');
}
int main(){
Tex = read();
while(Tex --) AC();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App