Codeforces Round 885 (Div. 2)|“朝闻道”知识分享大赛
这是我参加朝闻道知识分享大赛的第27篇文章.
题意:Vika和她的朋友们在有n*m个房间的商场内,每一分钟,Vika移动到相邻房间中,她的朋友们在她之后移动,(每个人都必须移动)如果一分钟后Vika和她朋友在一个房间内,则Vika被捉住。问Vika能不能永远不被朋友捉到。
分析:刚开始没看懂题,以为只有一分钟,,,枚举每一个朋友的坐标,如果距离Vika的曼哈顿距离为奇数就不会被捉到,偶数就会被捉到。(因为所有情况都可以化简为在 2∗2 或 1∗2 的矩阵内,易得结论)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define _cf() int _;cin>>_;while(_--)
const int N = 2e6;
const int inf = 2147483647;
void _()
{
int n,m,k;
cin>>n>>m>>k;
int res=0,a,b,x,y,no=0;
cin>>x>>y;
for(int i=1;i<=k;i++)
{
cin>>a>>b;
res=abs(a-x)+abs(b-y);
if(res%2==0) no=1;
}
if(no) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
int main()
{
int t;cin>>t;
while(t--) _();
return 0;
}
题意:一座木桥由n块不同颜色的木板组成,Vika只能踩颜色相同的木板,且有一次改变木板颜色的机会,问Vika走过这座桥跨越最大木板数的最小值。
分析:开始以为是二分,但是二分不好遍历颜色,所以直接模拟了,记录每个颜色出现的前一次位置,更新每个颜色的最大和次大跨度,处理后遍历所有颜色并更新答案即可。
代码:(寡丑)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define _cf() int _;cin>>_;while(_--)
const int N = 2e5+4;
const int inf = 2147483647;
int a[N],pre[N];
int maxx[N];//最大距离
int maxn[N];//次大距离
int n,k;
void _()
{
cin>>n>>k;
for(int i=1;i<=k;i++) pre[i]=maxx[i]=maxn[i]=0;
int x,gap;
for(int i=1;i<=n;i++)
{
cin>>x,gap=i-pre[x];
if(gap>maxx[x])
{
maxn[x]=maxx[x];
maxx[x]=gap;
}
else if(gap>maxn[x]) maxn[x]=gap;
pre[x]=i;
}
int res=inf;
for(int i=1;i<=k;i++)
{
gap=n+1-pre[i];
if(gap>maxx[i])
{
maxn[i]=maxx[i];
maxx[i]=gap;
}
else if(gap>maxn[i]) maxn[i]=gap;
res=min(res,max((maxx[i]-1)/2,maxn[i]-1));
}
// cout<<"--------- ";
// for(int i=1;i<=k;i++)cout<<maxx[i]<<" ";cout<<endl;
printf("%d\n",res);
}
int main()
{
int t;cin>>t;
while(t--) _();
return 0;
}
题意:给两个数组,每次滚动更新,更新方式为c=abs(a-b),a=b,b=c。问a数组能不能全部更新为0.
分析:很玄学的找规律题,,,,赛时是没写出来,但是发现了三次一循环的规律。
在 的情况,更新三轮:a b a-b a-2b b,可以发现b没变,a变成了a-2b,同理 的情况
先三轮三轮地加速更新,再依次往后挪一个。普遍情况是a,b数组中每对数的更新次数模3必须相等,再处理一下特殊情况。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define _cf() int _;cin>>_;while(_--)
const int N = 2e6;
const int inf = 2147483647;
void _()
{
int n;
cin>>n;
vector<int> a(n+1),b(n+1);
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i];
int x,y,op=-1,no=0;
for(int i=1;i<=n;i++)
{
x=a[i],y=b[i];
if(x==0&&y==0) continue;
int v=0;
while(x!=0)
{
if(x>=2*y&&y) x%=2*y;
else if(y>=2*x&&x) y%=2*x;
tie(x,y)=make_pair(y,abs(x-y));
v=(v+1)%3;
}
if(op!=-1&&op!=v) {
no=1;
break;
}
op=v;
}
cout<<(no?"NO\n":"YES\n");
}
int main()
{
int t;cin>>t;
while(t--) _();
return 0;
}
相关推荐
Educational Codeforces Round 83 (Rated for Div. 2) D
今天CF被D恶心到了,写个题解重新整理下思路,(20开始想,25写完暴力代码,1.30才过,优化后的。。 核心思路就是在暴力的基础上进行组合数等差加速。 C(n-2,i-1)*C(j-1,n-2)*(i-1) __ j: n-1 -> m 我们发现内层循环,每次只是j加一,我们就可以只用一次组合数剩下的用差量表示 对于外层循环同理 只有(i-1) * C(n-2,i-1) i会每次加一。我们也只算一次剩下的用差量表示。 复杂度就降到ON *快速幂log 即可 暴力代码 #include using namespace std; typedef long long ll; #defi
Codeforces Round #628 (Div. 2)【A B C D】
传送门 A. EhAb AnD gCd 直接输出1,n-1即可 #include #include #include #include #include #include #include #include #include #include #define pb push_back #define lb lower_bound #define ub upper_bound #define rep(i,a,b) for(int i=a;i=b;i--) typedef long long ll; using namespace std; const int MAXN=1e5+50; con
Codeforces Round #628 (Div. 2) A~~D
A #include using namespace std; typedef long long ll; int main(){ int t; cin>>t; while(t--){ ll x; cin>>x; cout<<1>>t; while(t--){ st.clear(); ll n; cin >>n; ll re
Educational Codeforces Round 85 (Rated for Div. 2) B. Middle Class(贪心)
目录传送门题意:思路:代码: 传送门 题意: 思路: 排个序,然后从最大的开始判断是否合适即可 代码: #include #include #include #include #include #include #include #include #include #include #define pb push_back #define lb lower_bound #define ub upper_bound #define fi first #define se second #define all(x) (x).begin(),(x).end() #define SZ(x) ((
Codeforces Round #629 (Div. 3) B. K-th Beautiful String
B. K-th Beautiful String 题目链接-B. K-th Beautiful String 题目大意 长度为n的字符串包含n−2n−2n−2个aaa和222个bbb,求按照字典序排列输出第kkk个字符串 解题思路 第一个bbb在倒数第二位有1个字符串,在倒数第三位有2个字符串…在倒数第nnn位时有n−1n-1n−1个字符串 可以根据第一个bbb的位置对字符串进行分组,然后找到第kkk个字符串在第几组里即可,然后再推出第二个bbb的位置即可 记得我们刚开始找的bbb的位置是倒数的,假设bbb在倒数第i位上,那么b就在整数第n+1−in+1-in+1−i位上 具体操作见代码


