牛客练习赛61 题解
A:正义感爆棚去打怪
#include
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int h,a,H,A;
scanf("%d %d %d %d",&h,&a,&H,&A);
int num=(H+a-1)/a;
int now=(num-1)*A;
if(now==0){
puts("-1");
continue;
}
int x=h/now;
int y=h-x*now;
if(y==0){
x--;y+=now;
}
bool f=1;
while(1){
H-=a;if(H<=0)break;
y-=A;if(y<=0){
f=0;break;}
}
x+=f;
printf("%d\n",x);
}
return 0;
}
B:打怪累了吃水果充饥
#include
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d %d",&n,&m);
if(n>m)swap(n,m);
int now=n;
int num=0;
while(now
C:吃完水果去考数学
#include
using namespace std;
typedef pair pii;
int fa[14],num[14],cnt,ans[14];
int _find(int x){
if(fa[x]==x)return x;
else return fa[x]=_find(fa[x]);
}
int dfs(int a,int b,int c,int d,int num){
if(num==0)return 1;
int res=0;
if(a>=ans[num])res+=dfs(a-ans[num],b,c,d,num-1);
if(b>=ans[num])res+=dfs(a,b-ans[num],c,d,num-1);
if(c>=ans[num])res+=dfs(a,b,c-ans[num],d,num-1);
if(d>=ans[num])res+=dfs(a,b,c,d-ans[num],num-1);
return res;
}
int main(){
for(int i=1;i<=12;i++)fa[i]=i,num[i]=1;
int a,b,c,d,m;scanf("%d %d %d %d %d",&a,&b,&c,&d,&m);
for(int i=1;i<=m;i++){
int x,y;scanf("%d %d",&x,&y);
int u=_find(x),v=_find(y);
if(u==v)continue;
fa[u]=v;num[v]+=num[u];
}
for(int i=1;i<=12;i++)
if(_find(i)==i)
ans[++cnt]=num[i];
printf("%d\n",dfs(a,b,c,d,cnt));
return 0;
}
D:考完试解放去旅游
#include
using namespace std;
typedef long long LL;
typedef pair pii;
const int N=1e5+10;
LL n,m,ans;
LL d1[N],d2[N],x[N<<1],y[N<<1],z[N<<1];
LL cnt=1,head[N],to[N<<1],nxt[N<<1],len[N<<1];
void add_edge(LL x,LL y,LL z) {
nxt[++cnt]=head[x]; to[cnt]=y; len[cnt]=z; head[x]=cnt;
}
bool vis[N];
priority_queue q;
void Dijkstra(LL d[],LL s) {
while (!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
d[s]=0; q.push(make_pair(0,s));
while (!q.empty()) {
pii x=q.top(); q.pop();
if (vis[x.second]) continue;
vis[x.second]=1;
for (LL i=head[x.second];i;i=nxt[i]) {
LL y=to[i];
if (d[y]>d[x.second]+len[i]) {
d[y]=d[x.second]+len[i];
q.push(make_pair(-d[y],y));
}
}
}
}
int main()
{
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++) scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
memset(d1,0x3f,sizeof(d1)); memset(d2,0x3f,sizeof(d2));
for (int i=1;i<=m;i++) add_edge(x[i],y[i],z[i]);
Dijkstra(d1,1);
cnt=1; memset(head,0,sizeof(head));
for (int i=1;i<=m;i++) add_edge(y[i],x[i],z[i]);
Dijkstra(d2,n);
ans=d1[n];
int q;scanf("%d",&q);
for (int j=1;j<=q;j++){
int i;scanf("%d",&i);
if (ans>d1[y[i]]+d2[x[i]]+z[i]) puts("YES");
else puts("NO");
}
return 0;
}
E:旅游结束去学英语
#include
using namespace std;
typedef long long ll;
typedef pair pii;
const int N=2e5+10;
unordered_map>mp;
int k,n;
ll hash1[N],pow26[N];
char s[N];
void init(){
pow26[0]=1;
for(int i=1;i<=n;i++)pow26[i]=pow26[i-1]*131;
for(int i=1;i<=n;i++)hash1[i]=hash1[i-1]+(s[i]-'a')*pow26[i];
}
bool check(int mid){
mp.clear();
for(int i=mid;i<=n;i++){
ll has=(hash1[i]-hash1[i-mid])*pow26[n-i];
mp[has].push_back({i-mid+1,i});
}
for(auto it:mp){
int last=0;
int now=0;
for(auto is:it.second){
if(is.first>last){
now++;
last=is.second;
}
}
if(now>=k)return true;
}
return false;
}
int main()
{
scanf("%d %d",&n,&k);scanf(" %s",s+1);
init();
int l=1,r=n,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
l=mid+1;
ans=mid;
}else r=mid-1;
}
printf("%d\n",ans);
return 0;
}
F:英语学累了,吃个苹果压压惊
#include
#include
#include
using namespace std;
const int N=1e5+10;
const int M=2e7;
const int INF=2e9;
int head[N],cnt,cnt1,n,q,fa[N][18],ans,root1[N],root,totsiz,sum[M],son[M][2],vn[N],siz[N],dep[N],mx[N],num1[N],d[N];
bool vis[N];
struct edge{
int next,to,w;
}e[N<<1];
void add(int u,int v,int w){
e[++cnt].next=head[u];
e[cnt].to=v;
e[cnt].w=w;
head[u]=cnt;
}
inline void dfs(int u,int f,int di){
dep[u]=di;d[u]=d[f]+1;fa[u][0]=f;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;if(v==f)continue;
dfs(v,u,di+e[i].w);
}
}
void init1(){
dfs(1,0,0);
for(int j=0;(1<<(j+1))d[v])swap(u,v);int temp=d[v]-d[u];
for(int i=0;(1<=0;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return fa[u][0];
}
int getdis(int u,int v){return dep[u]+dep[v]-2*dep[LCA(u,v)];}
inline void get_root(int u,int f){
mx[u]=0,siz[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;if(v==f||vis[v])continue;
get_root(v,u);siz[u]+=siz[v];mx[u]=max(mx[u],siz[v]);
}
mx[u]=max(mx[u],totsiz-mx[u]);if(mx[u]siz[u]?size-siz[u]:siz[v];root=0;
get_root(v,0),vn[root]=u,dfs2(root,0);
}
}
inline void init(){
root=0,mx[0]=n+1,totsiz=n;get_root(1,0),dfs2(root,0);
}
inline int query(int p,int l,int r,int x,int y){
if(!p)return INF;if(x<=l&&r<=y)return sum[p];
int mid=(l+r)>>1,tmp=INF;
if(x<=mid)tmp=min(tmp,query(son[p][0],l,mid,x,y));
if(y>mid)tmp=min(tmp,query(son[p][1],mid+1,r,x,y));
return tmp;
}
inline void query(int x,int l,int r){
for(int i=x;i;i=vn[i]){
ans=min(ans,getdis(x,i)+query(root1[i],1,10000,l,r));
}
}
inline void change(int &p,int l,int r,int x,int y){
if(!p)p=++cnt1,sum[p]=INF;sum[p]=min(sum[p],y);
if(l==r)return;int mid=(l+r)>>1;
if(x<=mid)change(son[p][0],l,mid,x,y);
else change(son[p][1],mid+1,r,x,y);
}
inline void change(int x,int y){
for(int i=x;i;i=vn[i]){
change(root1[i],1,10000,y,getdis(i,x));
}
}
inline void changes(){for(int i=1;i<=n;i++)change(i,num1[i]);}
int main(){
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++)scanf("%d",&num1[i]);
for(int i=1;i