A
Codeforces Beta Round #84 (Div. 2 Only), problem: (A) Nearly Lucky Number
题目大意
规定只有4和7的数字为幸运数,求含有4和7个数为4或7的数字的个数
题解
水题,用字符串做处理,统计4和7出现次数 如果出现次数也是等于4或者7的话,就输出YES 否则输出NO
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin>>s;
int cnt=0;
for(int i=0;i<s.length();i++){
if(s[i]=='4'||s[i]=='7')
cnt++;
}
if(cnt==4||cnt==7)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
B
Codeforces Round #267 (Div. 2), problem: (A) George and Accommodation
题目大意
两个人想在一个地方 就看有没有多的剩余空间
题解
水题,统计当前地方空间是否还大于等于2 如果大于等于2 那么代表可以 加一次
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
int a,b,cnt=0;
for(int i=0;i<n;i++){
cin>>a>>b;
if(b-a>=2)
++cnt;
}
cout<<cnt<<endl;
return 0;
}
C
Codeforces Round #290 (Div. 2), problem: (B) Fox And Two Dots
题目大意
其实就是一个判断成环的dfs
题解
由于dfs搜索只能上下左右,所以只要我们步数走回了自己就是一个环一个矩形 不过得排除一个 或者两个情况 所以最小的环都必须由四个相同的大写字母组成
第一种
#include<bits/stdc++.h>
using namespace std;
int dir_x[]={1,0,-1,0};
int dir_y[]={0,-1,0,1};
int n,m;
const int maxn=100+5;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int ff;
int ex,ey;
bool check(int x,int y,int xx,int yy){
if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&mp[x][y]==mp[xx][yy])
return true;
return false;
}
void dfs(int x,int y,int tmp){
vis[x][y]=true;
for(int i=0;i<4;i++){
int xx=x+dir_x[i];
int yy=y+dir_y[i];
if(check(x,y,xx,yy)){
dfs(xx,yy,tmp+1);
}
if(xx==ex&&yy==ey&&tmp>2){
ff=1;
return;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>m){
memset(vis,false,sizeof(vis));
for(int i=0;i<n;i++)
cin>>mp[i];
ff=0;
for(int i=0;!ff&&i<n;i++){
for(int j=0;!ff&&j<m;j++){
memset(vis,false,sizeof(vis));
ex=i,ey=j;
dfs(i,j,0);
}
}
if(ff)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
第二种
#include<bits/stdc++.h>
using namespace std;
int dir_x[]={1,0,-1,0};
int dir_y[]={0,-1,0,1};
int n,m;
const int maxn=100+5;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int ff;
int ex,ey;
bool check(int x,int y,int xx,int yy){
if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&mp[x][y]==mp[xx][yy])
return true;
return false;
}
void dfs(int x,int y,int tmp){
vis[x][y]=true;
for(int i=0;i<4;i++){
int xx=x+dir_x[i];
int yy=y+dir_y[i];
if(xx==ex&&yy==ey&&tmp>=3){ //区别在这里
ff=1;
return;
}
if(check(x,y,xx,yy)){
dfs(xx,yy,tmp+1);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>m){
memset(vis,false,sizeof(vis));
for(int i=0;i<n;i++)
cin>>mp[i];
ff=0;
for(int i=0;!ff&&i<n;i++){
for(int j=0;!ff&&j<m;j++){
memset(vis,false,sizeof(vis));
ex=i,ey=j;
dfs(i,j,0);
}
}
if(ff)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
D
Codeforces Round #175 (Div. 2), problem: © Building Permutation
题目大意
题意是每次对数组中一个元素进行+1或-1操作 使得所有数组元素都在1-n中
题解
从小到大进行排序,用当前下标值减去当前值就OK 注意用long long 用int会被卡
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
int a[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
long long ans=0;
for(int i=1;i<=n;i++){
ans+=abs(i-a[i]);
}
cout<<ans<<endl;
return 0;
}
E
Codeforces Round #163 (Div. 2), problem: (B) Queue at the School
题目大意
一群男女排队,每一个回合如果一个男生后面站着一个女生,那么女士优先,先让女生站前面
题解
水题,每一轮进行判断,按题意要求即可
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,t;
vector<char> vt;
cin>>n>>t;
string s;
cin>>s;
for(int i=0;i<t;i++){
for(int j=0;j<n-1;j++){
if(s[j]=='B'&&s[j+1]=='G'){
s[j]='G';
s[j+1]='B';
j++;
}
}
}
cout<<s<<endl;
return 0;
}
学如逆水行舟,不进则退