MXOI题解记录
Yuantianyou · · 个人记录
CF1878A How Much Does Daytona Cost?题解
题目描述
有 t 组数据。每组数据给出一个长度为 n 的序列 a 和一个数字 k。
请你判断 a 中是否存在一个非空子段使得 k 在这个子段中出现的次数严格大于其它数字的出现次数。存在输出 YES,否则输出 NO。
分析&解法
题目中说是否存在一个非空子段,那就说明这个子段长度可以为 1 (就他自己),所以我们只需要遍历数组判断他是否存在就可以了。
Code:
::::info[Code:]
#include<iostream>
using namespace std;
int t,n,k;
int a[150];
int main(){
cin >> t;
while(t--){
cin >> n >> k;
bool f = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
if(a[i] == k)
f = 1;
}
if(f) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
::::
CF1474A Puzzle From the Future
题目描述
意思就是给你一个
分析&解法
根据题目可知,构造的 a 数组中只会包含0,1。
我们如果要让数组 d 尽可能大,就要尽可能让数组 a 数位更多且没有前导0,还要让 d 有尽可能多的2或1且前后不重复。
所以我们可以挨个遍历前一个数和当前的数,如果当前的数加一不等于前面的数。我们就给它赋值当前的数加一,否则就等于零。
这样就可以求出 a 数组了。
Code:
::::info[Code:]
#include<iostream>
#include<string>
using namespace std;
int t,n;
char s1[100005],s2[100005];
int main(){
cin >> t;
while(t--){
cin >> n;
cin >> s1;
s2[0] = '1';
for(int i = 1;i < n;i++){
int a = s1[i] - '0',b = s2[i-1] - '0',c = s1[i-1] - '0';
if(a + 1 != b + c) s2[i] = '1';
else s2[i] = '0';
}
cout << s2 << endl;
}
return 0;
}
::::
CF1932A Thorns and Coins
这道题看上去是DP,实则是假DP,只需要模拟就行了
题目描述
给你一个行走路线,路上有金币和障碍,你一次可以走一步或两步,问最多能获得多少金币。
分析&解法
只需要模拟即可。
因为我们可以走一格,就相当于在没有障碍的情况下每个格子都能走到,所以只要这一格为金币,我们就可以吃到。
如果这一个为障碍,那么我们就可以判断是否能越到这个障碍,如果这一格为障碍且上一格也为障碍,就一定不能跨越(因为无论走一格还是两格都无法穿过这两个障碍)然后就
Code:
::::info[Code:]
#include<iostream>
using namespace std;
int t,n;
char s[55];
int ans;
int main(){
cin >> t;
while(t--){
cin >> n;
cin >> s;
ans = 0;
for(int i = 0;i < n;i++){
if(s[i] == '@') ans++;
if(s[i] == '*' && s[i - 1] == '*') break;
}
cout << ans << endl;
}
return 0;
}
::::
CF1931C Make Equal Again
题目描述
进行至多一次区间赋值操作,使数组中的所有元素都相等的最小代价。
分析&解法
既然题目要求了至多一次,那么最终整个数组的元素要么是
我们进行两次统计,一次统计以
最后取他俩的最小值就可以了。
Code
::::info[Code:]
#include<iostream>
using namespace std;
int t,n,a[200005];
int main(){
cin.tie(0),cout.tie(0);
ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n;
for(int i = 1;i <= n;i++)
cin >> a[i];
int ans1 = 0,ans2 = 0;
int i = 1,j = n;
while(a[i] == a[1]) i++;
while(a[j] == a[1]) j--;
ans1 = max(0,j - i + 1);
i = 1,j = n;
while(a[i] == a[n]) i++;
while(a[j] == a[n]) j--;
ans2 = max(0,j - i + 1);
cout << min(ans1,ans2) << endl;
}
return 0;
}
::::
CF1714C Minimum Varied Number
题目描述
找出数码和为
分析&解法
如果想让一个数尽可能小,那么就要让他数位越少且高位上的数字越小。
那么我们可以让他的个位最大,越往前数越小(
Code:
::::info[Code:]
#include<iostream>
using namespace std;
int t,s;
int ans[50];
int main(){
cin >> t;
while(t--){
cin >> s;
int sy = s,i = 1,j = 9;
while(1){
if(sy > j) ans[i] = j;
else{
ans[i] = sy;
break;
}
sy -= j,i++,j--;
}
for(int k = i;k >= 1;k--) cout << ans[k];
cout << endl;
}
return 0;
}
::::
CF1409B Minimum Product
题目描述
选择
你的任务是,通过不超过
分析&解法
核心:两个数相乘,差越大,他们的积越小;越接近,他们的积越大。
所以我们可以分别让
Code:
::::info[Code:]
#include<bits/stdc++.h>
using namespace std;
int t;
int main() {
cin >> t;
while (t--) {
long long a, b, x, y, n;
cin >> a >> b >> x >> y >> n;
long long ans = 1e18;
int i = 2;
while (i--) {
int da = min(n, a - x);
int db = min(n - da, b - y);
ans = min(ans, (a - da) * 1ll * (b - db));
swap(a, b);
swap(x, y);
}
cout << ans << endl;
}
}
::::
CF1515A Phoenix and Gold
题目描述
有 NO.
分析&解法
(个人见解)
如何构造这个序列呢?我们可以先给他排个序,在输出过程中如果发现有前缀和为
个人证明:
(
Code:
::::info[Code:]
#include <bits/stdc++.h>
using namespace std;
int t,n,x;
int w[105];
int main(){
cin >> t;
while(t--){
cin >> n >> x;
for(int i = 1;i <= n;i++) cin >> w[i];
sort(w + 1,w + n + 1);
int sum = 0;
bool f = 0;
for(int i = 1;i <= n;i++)
sum += w[i];
if(sum == x){
cout << "NO\n";
continue;
}
sum = 0;
cout << "YES\n";
for(int i = 1;i <= n;i++){
if(sum + w[i] == x){
printf("%d %d ",w[i+1],w[i]);
sum += w[i+1]+w[i],i++;
}else{
printf("%d ",w[i]);
sum += w[i];
}
}
cout << endl;
}
return 0;
}
::::
CF1501B Napoleon Cake
题目描述
对一个堆进行
分析&解法
模拟 + 差分 即可
维护一个差分数组,每次将
最后输出这个差分数组,如果
Code:
::::info[Code:]
#include <bits/stdc++.h>
using namespace std;
int t, n;
int a[200005];
int main()
{
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
int b[200005] = {0};
for (int i = 1; i <= n; i++)
{
if (a[i] == 0)
continue;
b[i + 1] -= 1;
b[i - a[i] + 1 <= 0 ? 1 : i - a[i] + 1] += 1;
}
for (int i = 1; i <= n; i++)
b[i] = b[i - 1] + b[i];
for (int i = 1; i <= n; i++)
{
if (b[i] >= 1)
cout << 1 << ' ';
else
cout << 0 << ' ';
}
cout << endl;
}
return 0;
}
::::
CF1554A Cherry
题目描述
给定
分析&解法
仔细分析就会发现,
假设我们有两个相邻的数
- 第一种情况,当
b\lt c 时,解为a\cdot c 。 - 第二种情况,当
a\gt c 时,解为b\cdot c 。
对于第一种情况,显然
综上,我们只需要考虑序列长度为
Code:
::::info[Code:]
#include <iostream>
using namespace std;
using ll = long long;
int t;
int n, a[1000006];
int main()
{
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
ll ans = -1e18;
for (int i = 1; i < n; ++i)
ans = max(ans, 1ll * a[i] * a[i + 1]);
cout << ans << '\n';
}
}
::::
P3029 [USACO11NOV] Cow Lineup S
难度: 普及/提高−
分析&解法
先按照坐标从小到大排序,接着维护一个包含所有品种奶牛的单调队列,每次求一下成本取最小值就行了。 具体维护方法如下:
- 开始前先求出来一个包含所有品种奶牛的队列
- 每次把队尾多余的奶牛删掉
- 求成本
- 加入新奶牛
Code: ::::info[
Code: ]#include <algorithm> #include <iostream> #include <map> using namespace std; int n; struct node{ int x, id; } a[500005]; bool cmp(node x1, node x2){ return x1.x < x2.x; } map<int, int> mp; int cnt = 0, ans = 2e9 + 10; int main(){ cin >> n; for (int i = 1; i <= n; i++){ cin >> a[i].x >> a[i].id; mp[a[i].id]++; if (mp[a[i].id] == 1) cnt++; } sort(a + 1, a + n + 1, cmp); mp.clear(); int dft = 0, l = 1, r; for (r = 1; r <= n; ++r){ mp[a[r].id]++; if (mp[a[r].id] == 1) dft++; if (dft == cnt) break; } ans = min(ans, a[r].x - a[l].x); while (r <= n){ while (mp[a[l].id] - 1 >= 1) mp[a[l].id]--, l++; ans = min(ans, a[r].x - a[l].x); mp[a[++r].id]++; } cout << ans; return 0; }::::
AT_abc141_d [ABC141D] Powerful Discount Tickets
难度: 普及−(中位)
分析&解法
首先我们要知道,对一个价格为
X 的商品使用一张优惠券后的价格是\left\lfloor \frac{X}{2} \right\rfloor 元。
所以我们的贪心策略就是每次对最大的数使用一张优惠卷,使其价格折半。最后把剩下的价格加起来就行了。
因为每次要取最大的数,所以用一个堆(优先队列)来维护。
Code:
::::info[
#include<iostream>
#include<queue>
using namespace std;
int m;
int n;
int s[200005];
long long ans;
int main(){
cin >> n;
cin >> m;
priority_queue<int> q;
for(int i = 1;i <= n;i++){
cin >> s[i];
q.push(s[i]);
}
for(int i = 1;i <= m;i++){
int now = q.top();
q.pop();
q.push(now / 2);
}
for(int i = 1;i <= n;i++){
ans += q.top();
q.pop();
}
cout << ans;
return 0;
}
::::
CF1800C2 Powering the Hero (hard version)
难度: 普及−(中位)
分析&解法
可以发现,每个英雄牌所有的力量值都为其前未被使用的最大奖励牌的力量值。
所以我们可以维护一个大根堆(优先队列),存放奖励牌,这样每次就可以取到奖励牌的最大值了。
Code:
::::info[
#include<bits/stdc++.h>
using namespace std;
long long T;
void solve() {
long long ans=0;
priority_queue<long long> s;
long long n;
cin >> n ;
for(long long i = 1; i <= n; i++) {
long long x;
cin>>x;
if(x == 0 && s.size() > 0) {
ans += s.top();
s.pop();
} else {
s.push(x);
}
}
cout<<ans<<'\n';
}
int main() {
cin>>T;
while(T--)
solve();
return 0;
}
::::
CF1353D Constructing the Array
难度: 普及−(上位)
分析&解法
因为每次都会取最长的区间,所以我们可以用一个大根堆(优先队列)维护一个最大的区间。
其中大根堆里要存储区间
重点知识
结构体的优先队列:
实现按
- 结构体内部重构,例如
struct node { int l, r; bool operator < (const node& x) const{ if(l == x.l) return r > x.r; return l < x.l; }//这里的‘>’‘<’是优先级 }; priority_queue<node> q; - 结构体外比较函数,例如
struct cmp{ bool operator () (const node& x,const node& y){ if(x.l == y.l) return x.r > y.r; return x.l < y.l; }//这里的‘>’‘<’是优先级 }; priority_queue<node,vector<node>,cmp> q;注意两者的使用方式!!!
Code: ::::info[
Code: ]#include <queue> #include <iostream> using namespace std; int t, n; struct node { int l, r; }; struct cmp{ bool operator () (const node& x,const node& y){ if(x.r - x.l + 1 == y.r - y.l + 1) return x.l > y.l; return x.r - x.l + 1 < y.r - y.l + 1; } }; int main() { cin >> t; while (t--) { cin >> n; priority_queue<node,vector<node>,cmp> q; q.push({1, n}); int a[200005] = {}, i = 1; while (!q.empty()) { node u = q.top(); q.pop(); int mid; if ((u.r - u.l + 1) % 2 == 0) mid = (u.l + u.r - 1) / 2; else mid = (u.l + u.r) / 2; a[mid] = i++; if (u.l <= mid - 1) q.push({u.l, mid - 1}); if (mid + 1 <= u.r) q.push({mid + 1, u.r}); } for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << '\n'; } }::::
CF1579D Productive Meeting
难度: 普及/提高-(下位)
分析&解析
贪心策略是:每次社交次数最大的人与第二大的人社交。 所以需要一个大根堆(优先队列)来维护社交次数最大的人。
Code: ::::info[
Code: ]#include <iostream> #include <queue> #include <map> #include <algorithm> using namespace std; int t; int n; struct node { int sj, id; }; struct cmp{ bool operator () (const node& x,const node& y){ return x.sj < y.sj; } }; int main() { cin >> t; while (t--) { cin >> n; priority_queue<node,vector<node>,cmp> q; for (int i = 1; i <= n; i++) { int sj; cin >> sj; q.push({sj,i}); } int ans = 0; node b[200010] = {}; while(1) { node u = q.top(); q.pop(); node v = q.top(); q.pop(); if(u.sj == 0 || v.sj == 0) break; b[++ans] = {u.id,v.id}; u.sj--,v.sj--; q.push(u),q.push(v); } cout << ans << '\n'; for (int i = 1; i <= ans; i++) cout << b[i].sj << ' ' << b[i].id << '\n'; } return 0; }::::
P14971 『GTOI - 2B』DDoS
难度: 普及/提高-(中位)
分析&解析
题解做法: 首先,可以使用全部时间,进行 1 次操作。但可用代价可能不够,所以需要去除一些间隔,使得代价不大于 m;所以,去除长度更长的区间更优。
Code: ::::info[
Code: ]#include <bits/stdc++.h> using namespace std; int n, m, mn = 1e9, mx; int jian[1000005]; struct node { int l, r; } a[1000005]; bool cmp1(node x, node y) { return x.l < y.l; } bool cmp2(int x, int y) { return x > y; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i].l >> a[i].r; mn = min(mn, a[i].l); mx = max(mx, a[i].r); } sort(a + 1, a + n + 1, cmp1); int sz = 0; int nowr = a[1].r; // 处理间隔 for (int i = 2; i <= n; ++i) { if (a[i].l > nowr + 1) jian[++sz] = a[i].l - nowr - 1, nowr = a[i].r; else nowr = max(nowr, a[i].r); } if (mx - mn + 1 <= m) { cout << 1; return 0; } int sy = mx - mn + 1 - m; int ans = 1; sort(jian + 1, jian + sz + 1, cmp2); for (int i = 1; i <= sz; ++i) { sy -= jian[i]; ++ans; if (sy <= 0) { cout << ans; return 0; } } cout << -1; return 0; }::::
CF1777C Quiz Master
难度: 普及/提高-(上位)
分析&解析
观察可以发现,我们选取的人一定是一个连续的区间。
所以我们可以用双指针(Two-pointer)算法来解决这个问题。
本问题需要求出每个数的因数,所以我们可以预处理(
双指针过程中,用数组
Code:
::::info[
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int M = 1e5, N = 1e5 + 10;
int t, n, m, cnt, ans, a[N], vis[N];
vector<int> v[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
for (int i = 1; i <= M; i++)
for (int j = i; j <= M; j += i)
v[j].push_back(i);
while (t--)
{
cin >> n >> m;
ans = M;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for (int l = 1, r = 0; l <= n; l++)
{
while (r < n && cnt < m)
{
r++;
for (int x : v[a[r]])
{
if (x <= m)
{
if (!vis[x])
cnt++;
vis[x]++;
}
}
}
if (cnt == m)
ans = min(ans, a[r] - a[l]);
for (int x : v[a[l]])
{
if (x <= m)
{
vis[x]--;
if (!vis[x])
cnt--;
}
}
}
cout << (ans < M ? ans : -1) << endl;
}
return 0;
}
::::