AtCoder Beginner Contest 305 A-G

比赛链接

A.Water Station

找最近的%5=0的数

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
int n;
void solve(){
scanf("%lld",&n);
if(n%5<=2)printf("%lld",n-(n%5));
else printf("%lld",n+5-(n%5));
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();
}
return 0;
}

B.ABCDEFG

模拟

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
int f[300];
void solve(){
f['A']=0;
f['B']=3;
f['C']=4;
f['D']=8;
f['E']=9;
f['F']=14;
f['G']=23;
char x,y;
cin>>x>>y;
if(x>y)swap(x,y);
printf("%lld",f[y]-f[x]);
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();
}
return 0;
}

C.Snuke the Cookie Picker

找到一个上下左右有大于等于2个‘#’的空白格

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
char a[505][505];
int c[505][505];
int n,m;
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
	for(int j=1;j<=m;j++){
		cin>>a[i][j];
		if(a[i][j]=='#')c[i][j]=1;
	}
}
for(int i=1;i<=n;i++){
	for(int j=1;j<=m;j++){
		int sum=0;
		if(c[i][j])continue;
		sum+=c[i-1][j]+c[i+1][j]+c[i][j-1]+c[i][j+1];
		if(sum>=2){
			printf("%lld %lld",i,j);
			return;
		}
	}
}
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();

}
return 0;
}

D. Sleep Log

前缀和+二分

预处理睡觉时间的前缀和,二分找到大于等于这个时间的第一个,然后判断这个时间是否是睡眠时间如果是还要减去这个时间-二分找到的时间,得到在k分钟内的睡觉时间.用r分钟内的睡觉时间-l分钟内的睡觉时间即可

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
int n;
int a[500005];
int q[500005];
int erfen(int k){
	int l=1,r=n;
	while(l<r){
		int mid=(l+r)/2;
		if(a[mid]>=k)r=mid;
		else l=mid+1;
	}
	if(l%2)return q[l]-(a[l]-k);
	return q[l];
}
void solve(){
scanf("%lld",&n);
for(int i=1;i<=n;i++){
	scanf("%lld",&a[i]);
	q[i]+=q[i-1];
	if(i%2)q[i]+=a[i]-a[i-1];
}
int q;
scanf("%lld",&q);
for(int i=1;i<=q;i++){
	int t1,t2;
	scanf("%lld %lld",&t1,&t2);
	printf("%lld\n",erfen(t2)-erfen(t1));
}
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();
}
return 0;
}

E.Art Gallery on Graph

变相最短路

只是把找距离最短的改成找剩余距离最大的,其他同djistla

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
struct node{
	int x,y;
	bool operator < (const node & that)const{
		return y>that.y;
	}
	node(int xx,int yy){
		x=xx;
		y=yy;
	}
};
multiset<node>s;
bool vis[500005];
vector<int>p[200005];
bool us[500005];
int dis[200005];
int n,m,q;
void solve(){
scanf("%lld %lld %lld",&n,&m,&q);
for(int i=1;i<=m;i++){
	int t1,t2;
	scanf("%lld %lld",&t1,&t2);
	p[t1].push_back(t2);
	p[t2].push_back(t1);
}
vector<int>v;
for(int i=1;i<=q;i++){
	int t1,t2;
	scanf("%lld %lld",&t1,&t2);
	v.push_back(t1);
	vis[t1]=1;
	s.insert(node(t1,t2));
	dis[t1]=t2;
}
while(!s.empty()){
	int t1=(*s.begin()).x,t2=(*s.begin()).y;
	//cout<<t1<<" "<<t2<<endl;
	s.erase(s.begin());
	if(!t2)continue;
	if(us[t1])continue;
	t2--;
	us[t1]=1;
	for(int i=0;i<p[t1].size();i++){
		
		if(vis[p[t1][i]]&&dis[p[t1][i]]>t2)continue;
		
		if(!vis[p[t1][i]])v.push_back(p[t1][i]);
		vis[p[t1][i]]=1;
		s.insert(node(p[t1][i],t2));
		dis[p[t1][i]]=t2;
	}
}
sort(v.begin(),v.end());
int op=v.size();
printf("%lld\n",op);
for(int i=0;i<v.size();i++){
	printf("%lld ",v[i]);
}
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();
}
return 0;
}

F.Dungeon Explore

atcoder在交互题使用了有返回值的函数没写返回值会返回TLE

每次找到一个没走过的走,如果都走过了就往回走

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
int now=1;
int n,m;
int t;
int a[100005];
bool v[100005];
int la[100005];
void r(){
	scanf("%lld",&t);
	for(int i=1;i<=t;i++){
		scanf("%lld",&a[i]);
	}
}
int work(){
	for(int i=t;i>0;i--){
		if(!v[a[i]])return a[i];
	}
	return la[now];
}
void solve(){
scanf("%lld %lld",&n,&m);
v[1]=1;
while(now!=n){
	r();
	int temp=now;
	now=work();
	if(!v[now])la[now]=temp;
	v[now]=1;
	printf("%lld\n",now);
	fflush(stdout);
	if(now==n){
	string s;
	cin>>s;
	exit(0);
	}
}
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();
}
return 0;
}

G.Banned Substrings

矩阵快速幂

需要先写一个字符串匹配判断合法

可以写出简单状压dp[i][j] j, 前i个字符,最近6位状态为j的可能数,一共64种状态,利用矩阵快速幂加速转移

暴力处理前6位
1 / 0 ( 合法 / 不合法 ) 1 / 0 ( 合法 / 不合法 ) 1 / 0 ( 合法 / 不合法 ) … … (1) \begin{matrix} 1/0(合法 /不合法) &1/0(合法 /不合法) &1/0(合法 /不合法)&…… \end{matrix}\tag{1} 1/0(合法/不合法)1/0(合法/不合法)1/0(合法/不合法)……(1)

到状态 1 的转移(一列 64 位) 到状态 2 的转移 到状态 3 的转移 … … (2) \begin{matrix} 到状态1的转移(一列64位)& 到状态2的转移 & 到状态3的转移&…… \end{matrix}\tag{2} 到状态1的转移(一列64位)到状态2的转移到状态3的转移……(2)

f = ( 1 ) ∗ ( 2 ) [ n − 6 ] f=(1)* (2)^{[n-6]} f=(1)(2)[n6]

a n s = ∑ i = 1 64 f [ 1 ] [ i ] % m o d ans=\sum_{i=1}^{64} f[1][i] \% mod ans=i=164f[1][i]%mod

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T;
int n,m;
int mod=998244353;
struct node{
	int a[70][70];
	node(){
		memset(a,0,sizeof(a));
	}
};
int c[10];
string s[130];
bool ban[150];
int ans=0;
node fir,sec;
node cheng(node x,node y){
	node cnt;
	for(int i=1;i<=64;i++){
		for(int j=1;j<=64;j++){
			for(int k=1;k<=64;k++){
			
			cnt.a[i][j]+=x.a[i][k]*y.a[k][j]%mod;
			if(cnt.a[i][j]>=mod)cnt.a[i][j]-=mod;
			}
		}
	}
	return cnt;
}
node ksm(node k,int w){
	node cnt;
	for(int i=1;i<=64;i++){
		cnt.a[i][i]=1;
	}
	while(w){
		if(w%2){
			cnt=cheng(cnt,k);
		}
		w/=2;
		k=cheng(k,k);
	}
	return cnt;
}
bool check(int k){
	for(int i=1;i<=m;i++){
		if(s[i].size()>k)continue;
		for(int j=1;j+s[i].size()-1<=k;j++){
			bool ok=1;
			for(int ep=0;ep<s[i].size();ep++){
				if(s[i][ep]!=c[j+ep]){
					ok=0;
					break;
				}
			}
			if(ok)return 0;
		}
	}
	return 1;
}
int get_c(){
	int cnt=0;
	for(int i=1;i<=6;i++){
		cnt+=c[i]*(1<<(i-1));
	}
	return cnt;
}
void dfs1(int k){
	if(k==n+1){
		if(check(n)){
			ans++;
		}
		
		return;
	}
	c[k]=0;
	dfs1(k+1);
	c[k]=1;
	dfs1(k+1);
}
void dfs2(int k){
	if(k==7){
		if(check(6)){
			fir.a[1][get_c()+1]=1;
		}
		else ban[get_c()]=1;
		return;
	}
	c[k]=0;
	dfs2(k+1);
	c[k]=1;
	dfs2(k+1);
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
	cin>>s[i];
	for(int j=0;j<s[i].size();j++){
		if(s[i][j]=='a')s[i][j]=0;
		else s[i][j]=1;
	}
}
if(n<=6){
	dfs1(1);
	cout<<ans;
	return;
}
dfs2(1);
int op=(1<<5);
for(int i=0;i<64;i++){
	int temp=i/2;
	int t1=temp,t2=temp+op;
	if(!ban[t1])sec.a[i+1][t1+1]=1;
	if(!ban[t2])sec.a[i+1][t2+1]=1;
}
sec=ksm(sec,n-6);
fir=cheng(fir,sec);
for(int i=1;i<=64;i++){
	ans+=fir.a[1][i];
	if(ans>=mod)ans-=mod;
}
cout<<ans;
}
signed main(){
T=1;
//ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
//cin>>T;
while(T--){
solve();

}
return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值