难忘的瞬间-1
序言
在每个选手的生涯里,都有很多难忘的瞬间。它或许是你AK了比赛时兴奋的尖叫,或你爆零是的两行泪水;或许是你克服了许多困难写对一道难题的欢喜,或是你半个月的时间调不对一道题的悲伤……从现在开始,我将用手中的笔,把这些交织在我记忆中的一个个画面记录下来吧!在不仅仅会给你经验,让你总结和提高,更或许成为你初学时期美好的记忆呢!从本章开始,我将讲述我做题、打比赛时的心路历程(和题解),让这些瞬间不被磨灭!
(作者:Timmylyx)
正题
本章是一场比赛的记录,比赛链接:CF1883。
这是我第一次AK 本蒟蒻 的题解,谢谢支持!(不懂的概念查百度 ^_^)
第 A 题,Morning
非常水的一道题!
可以发现,打出这些数字是有序的,于是我们就只需考虑没两位之间的切换光标次数(包括开始的数字1)。很容易得出计算两个数字切换次数的公式:
剩下的就很容易了,代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
int t; string s;
cin>>t;
while (t--){
cin>>s;s='1'+s;
int ans=s.size()-1;
for (int i=1; i<5; i++){
int now=s[i]-'0';
now=(now+9)%10+1;
int nox=s[i-1]-'0';
nox=(nox+9)%10+1;
ans+=abs(now-nox);
}
cout<<ans<<endl;
}
}
第 B 题,Chemistry
稍微有些思维,但很简单。
这道题有个结论:先设有
证明的话,就分类讨论吧:
-
-
-
- $x-k$ 是偶数,那么我们可以先把所有删成偶数,在把剩余的两个两个删,可以。 - $x-k$ 是奇数,我们可以用最后一次把一个偶数变为奇数,可以。
代码:
#include <bits/stdc++.h>
using namespace std;
int t,k; string s;
int cnt[30];
int main(){
cin>>t;
while (t--){
memset(cnt,0,sizeof(cnt));
cin>>k>>k>>s;int cntx=0;
for (int i=0; i<s.size(); i++) cnt[s[i]-'a']++;
for (int i=0; i<26; i++) if (cnt[i]%2==1) cntx++;
cout<<(k>=cntx-1?"YES\n":"NO\n");
}
}
第 C 题,Raspberries
看上去挺难的,但不要崩溃,先看一下
可以 惊奇 地发现,竟然
那么我们就可以分类讨论了。
-
- $a_{i} ~mod~ 2==0$,$ans$ 减少(可以少变一次数)。 - $a_{i} ~mod~ 4==0$,$ans$ 再减少(同理)。 - $a_{i} ~mod~ 4==3$,$tmp$ 设为 $1$(可以变一次它)。 答案为 $min(max(ans,0),tmp)$(小心负数)。 -
否则
k 为质数,不能东拼西凑,于是我们只要输出最少的单个数字改变次数即可,公式:solve(x)=k-(x-1) ~mod~ k-1
代码:
#include <bits/stdc++.h>
using namespace std;
int t,n,k,x;
int main(){
cin>>t;
while (t--){
cin>>n>>k;
if (k==4){
int ans=2,tt=2;
while (n--){
cin>>x;
if (x%2==0) ans--;
if (x%4==0) ans--;
if (x%4==3) tt=1;
}
cout<<min(max(ans,0),tt)<<endl;
}
else{
int ans=100;
while (n--){
cin>>x; ans=min(ans,k-(x-1)%k-1);
}
cout<<ans<<endl;
}
}
}
第 D 题,In~Love
关于这道题目,我们可以先从两个点入手。
1.如何统计答案
题目问你,是否可在当前线段群里挑选
现在,我们可以想一想,我们挑选哪几个线段呢?
答案很简单,我们要保证任意线段重叠,那么我们考虑最坏情况:
2.如何处理数据
现在,我们要寻找一种数据结构,可以维护最大、最小,也可以加入、删除呢?
显然,
代码:
#include <bits/stdc++.h>
using namespace std;
struct node1{
int x,y;
bool operator < (const node1&a) const{
if (y!=a.y) return y<a.y;
return x<a.x;
}
};multiset <node1> s1;
struct node2{
int x,y;
bool operator < (const node2&a) const{
if (x!=a.x) return x>a.x;
return y>a.y;
}
};multiset <node2> s2;
int main(){
int n,x,y; char op;
cin>>n;
while (n--){
cin>>op>>x>>y;
if (op=='+') {
s1.insert(node1{x,y});
s2.insert(node2{x,y});
}
else{
s1.erase(s1.find(node1{x,y}));
s2.erase(s2.find(node2{x,y}));
}
cout<<((*s1.begin()).y<(*s2.begin()).x?"YES\n":"NO\n");
}
}
第 E 题,Look~Back
这道题目有坑!(难怪 评分
首先,贪心思路就很简单了,第一个不动,其他的尽可能少动(满足为止)。
接下来是重点 数据大小 !
因为本题是
可以发现,
那么
-
1\le p\le10^9 因为
p 为乘2 之前的数,所以较小,不会超过初始的a_{i} 的大小。 -
1\le q\le2\times10^6 因为
q 为指数,每次顶多增加log_{2}~n 次,而且有时还会减少,所以综合下来得出2\times10^6 。
最后,我们根据前面的
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,t,k;
int x,p,ans;
signed main(){
cin>>t;
while (t--){
cin>>n>>k; n--;
x=k; p=0; ans=0;
while (n--){
cin>>k;int tmp=k;
if (k>x){
while (x*2<=k&&p){
x*=2; p--;
}
}
else{
while (k<x){
k*=2; p++;
}
}
ans+=p;x=tmp;
}
cout<<ans<<endl;
}
}
第 F 题,You~Are~So~Beautiful
也是一道思维结论题(哪道题不是?!)。
题目要求我们选出一段练续的子序列,让它在序列中只有一种取法。
其实答案之和开头、结尾有关。
-
如果开头在之前没有出现,那么它会卡住接下来的数字,不会出现另一种取法。
-
如果开头在之前出现过,那么它可以替换成前面那个数字。
我们可以先预处理一个前缀和,如果这一个位置前面没有和它一样的数,就
然后再 倒序 枚举一遍,如果这一个位置后面没有和它一样的数,就把答案加上
对于判重的话,可以使用
代码:
#include <bits/stdc++.h>
using namespace std;
int n,t,a[100010],sum[100010];
int main(){
cin>>t;
while (t--){
cin>>n; set <int> st;
for (int i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=n; i++){
if (!st.count(a[i])){
st.insert(a[i]); sum[i]=sum[i-1]+1;
}
else sum[i]=sum[i-1];
}
st.clear(); long long ans=0;
for (int i=n; i>=1; i--){
if (!st.count(a[i])){st.insert(a[i]);ans+=sum[i];}
}
cout<<ans<<"\n";
}
}
第 G1,G2 题,Dances
两道题是一样的(用
小规律
首先,我们可以建表:
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
可以发现,只有
我们可以先处理出来
我们怎么处理答案呢?
可以使用贪心+
我们先把所有点(见上)加入
接下来的处理
我们可以把没有删除成功的点加入一个
当然,答案要比
代码:
#include <bits/stdc++.h>
using namespace std;
multiset <int> st1,st2;
int t,n,m,x,a[100010];
int main(){
cin>>t;
while (t--){
cin>>n>>m;
for (int i=1; i<n; i++) {
cin>>x; st1.insert(x);
}int 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++){
if (st1.size()&&a[i]>(*st1.begin())){
auto it=st1.lower_bound(a[i]);
it--; st1.erase(it);
}
else {
st2.insert(a[i]); ans++;
}
}
auto it=st2.end(); it--;
cout<<1ll*ans*m-min(m,*it-1)<<'\n';
st1.clear(); st2.clear();
}
return 0;
}
总结
通过我的题解,想必大家已经和我一样 如愿
不瞒大家,本蒟蒻是在是太菜了!我看到 魂飞魄散,叫苦不迭,后来,当我再次平静下来时,就
本期结束了,谢谢大家!