Codeforces Beta Round 77 (Div. 2 Only)
阿新 • • 發佈:2019-03-16
mit sizeof .com 輸出 c++ 主串 test post 上一個
layout: post
title: Codeforces Beta Round 77 (Div. 2 Only)
author: "luowentaoaa"
catalog: true
tags:
mathjax: true
- codeforces
傳送門
A - Football (簽到)
題意
問有沒有連續的七個1或者七個0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll mod=1e9+7; const int maxn=5e5+50; const int inf=1e8; char s[150]; int zero[150],one[150]; int main() { cin>>s+1; int len=strlen(s+1); for(int i=1;i<=len;i++){ if(s[i]=='1'){ one[i]=one[i-1]+1; zero[i]=0; } else zero[i]=zero[i-1]+1,one[i]=0; if(zero[i]>=7||one[i]>=7)puts("YES"),exit(0); } puts("NO"); return 0; }
B - Lucky Numbers (easy) (dfs)
題意
問你大於等於N的最小的只有4和7組成的最小的相同4/7個數的數是多少
思路
直接爆搜
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll mod=1e9+7; const int maxn=5e5+50; const ll inf=1e17; ll ans=inf; ll n; void dfs(ll num,ll a,ll b){ if(a==b&&num>=n)ans=min(ans,num); if(num>n*100)return; dfs(num*10+4,a+1,b); dfs(num*10+7,a,b+1); } int main() { cin>>n; dfs(0,0,0); cout<<ans<<endl; return 0; }
C - Hockey (模擬 ,題意殺)
題意
給你n個禁用子串w1,w2……wn,在主串w中找出與禁用相同的的子串用字母letter替換。若要替換的子串中有字母與letter相同,
那麽就按字典序(PS:如果letter是a,那是b啦,不然就用a好了)去替換。輸出時大小寫保持不變。
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll mod=1e9+7; const int maxn=1e3+50; const ll inf=1e17; string s[maxn],sa[maxn],w,wa; bool m[maxn]; char ned; string to(string s){ for(int i=0;s[i];i++) if(s[i]>='A'&&s[i]<='Z')s[i]=tolower(s[i]); return s; } int main() { int n; cin>>n; for(int i=0;i<n;i++)cin>>s[i],sa[i]=to(s[i]); cin>>w; wa=to(w); cin>>ned;ned=tolower(ned); for(int i=0;i<wa.size();i++){ for(int j=0;j<n;j++){ if(wa.substr(i,sa[j].size())==sa[j]){ for(int k=i;k<i+sa[j].size();k++)m[k]=1; } } } for(int i=0;i<wa.size();i++){ if(m[i]){ if(wa[i]==ned){ char tm; if(wa[i]=='a')tm='b'; else tm='a'; if(w[i]>='A'&&w[i]<='Z')w[i]=tm-32; else w[i]=tm; } else if(w[i]>='A'&&w[i]<='Z')w[i]=ned-32; else w[i]=ned; } } cout<<w<<endl; return 0; }
D - Volleyball (最短路)
題意
有n個地方,m條路,一個人要從x到y,他要打車走,每條路上都有出租車,出租車走的距離為t,收c元,求從x到y最小花費
思路
重建圖算出每個點能去哪些點 然後跑bfs或者dij
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int maxn=1e3+50;
const ll inf=1e17;
int n,m;
struct node{
int to;
ll w;
bool operator <(const node &a)const{
return w>a.w;
}
};
vector<node>G[maxn],G2[maxn];
int x,y;
ll t[maxn],c[maxn];
ll val[maxn];
priority_queue<node>Q;
void dij(int st){
for(int i=1;i<=n;i++)val[i]=inf;
val[st]=0;
while(!Q.empty())Q.pop();
Q.push(node{st,0});
while(!Q.empty()){
node now=Q.top();
Q.pop();
int u=now.to;
ll w=now.w;
if(val[u]>t[st])break;
if(u!=st&&val[u]<=t[st])G2[st].push_back({u,0});
for(int i=0;i<G[u].size();i++){
int to=G[u][i].to;
if(val[to]>val[u]+G[u][i].w){
val[to]=val[u]+G[u][i].w;
Q.push(node{to,val[to]});
}
}
}
}
void dij2(){
for(int i=1;i<=n;i++)val[i]=inf;
val[x]=c[x];
while(!Q.empty())Q.pop();
Q.push(node{x,val[x]});
while(!Q.empty()){
node now=Q.top();Q.pop();
int u=now.to;
for(int i=0;i<G2[u].size();i++){
int v=G2[u][i].to;
ll tmp=0;
if(v==y)tmp=c[v];
if(val[v]>val[u]+c[v]-tmp){
val[v]=val[u]+c[v]-tmp;
Q.push(node{v,val[v]});
}
}
}
}
int main()
{
cin>>n>>m;
cin>>x>>y;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
G[u].push_back({v,w});
G[v].push_back({u,w});
}
for(int i=1;i<=n;i++){
cin>>t[i]>>c[i];
dij(i);
}
if(x==y)puts("0"),exit(0);
dij2();
if(val[y]==inf)val[y]=-1;
cout<<val[y]<<endl;
return 0;
}
E - Horse Races (數位DP)
題意
求區間內幸運數字(4/7)間隔不超過K的數字的個數 區間大小為\(10^{1000}\)
思路
一眼數位DP
\(dp[pos][dis][yes]\) 表示位置在pos +上一個幸運數字和現在隔多少位置+是否已經有間隔不超過K的幸運數字
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int maxn=5e5+50;
const ll inf=1e16;
int k;
char st[2000],ed[2000];
ll dp[2000][3000][2];
int num[2000];
ll dfs(int pos,int ok,bool limit,int yes){
if(pos==-1)return yes;
if(!limit&&dp[pos][ok][yes]!=-1)return dp[pos][ok][yes];
int up=limit?num[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++){
int dis=ok+1;
if(i==4||i==7){
if(ok<=k)dis=0;
else dis=1;
}
ans+=dfs(pos-1,dis,limit&&i==up,dis==0||yes);
ans%=mod;
}
if(!limit)dp[pos][ok][yes]=ans;
return ans;
}
ll acc(char x[]){
int len=strlen(x);
for(int i=0;i<len;i++)num[i]=x[len-1-i]-'0';
return dfs(len-1,k+5,true,false);
}
int main()
{
int t;
memset(dp,-1,sizeof(dp));
ios::sync_with_stdio(false);
cin>>t>>k;
while(t--){
cin>>st>>ed;
int lena=strlen(st),p=lena-1;
while(st[p]=='0')st[p--]='9';
st[p]=st[p]-1;
cout<<(acc(ed)-acc(st)+mod)%mod<<endl;
}
return 0;
}
Codeforces Beta Round 77 (Div. 2 Only)