hdu 6103 Kirinriki
#include<bits/stdc++.h> using namespace std; int n,m,ans; char s[5005]; void doit(int l,int r) { int d=0,nw=0; for(;l<r;l++,r--) { while(l+d<r-d&&nw+abs(s[l+d]-s[r-d])<=m)nw+=abs(s[l+d]-s[r-d]),d++; ans=max(ans,d); nw-=abs(s[l]-s[r]); d--; } } void sol() { scanf("%d%s",&m,s); n=strlen(s); ans=0; for(int i=n-1;i>=1;i--)doit(0,i); for(int i=1;i<n-1;i++)doit(i,n-1); printf("%d\n",ans); } int main() { int tt;scanf("%d",&tt); while(tt--)sol(); return 0; }
hdu 6035 Colorful Tree
#include<bits/stdc++.h> using namespace std; int n,cc,cs,cnt[200005],vis[200005],c[200005],sz[200005]; long long ans,res; vector<int>g[200005]; void dfs(int x,int p) { sz[x]=1; int ss=0; for(int i=0;i<g[x].size();i++)if(g[x][i]!=p) { int lst=cnt[c[x]]; dfs(g[x][i],x); sz[x]+=sz[g[x][i]]; int nw=cnt[c[x]]-lst; ss+=nw; nw=sz[g[x][i]]-nw; res+=1ll*nw*(nw-1)/2; } cnt[c[x]]+=sz[x]-ss; } void sol() { cc=0; memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++)scanf("%d",&c[i]),vis[c[i]]=1; for(int i=1;i<=n;i++)if(vis[i])cc++; for(int i=1;i<=n;i++)g[i].clear(); for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);} res=0; dfs(1,0); ans=1ll*n*(n-1)/2*cc; for(int i=1;i<=n;i++)if(vis[i]) { int sz=n-cnt[i]; res+=1ll*sz*(sz-1)/2; } ans-=res; printf("Case #%d: %lld\n",++cs,ans); } int main() { while(scanf("%d",&n)==1)sol(); return 0; }
hdu 6071 Lazy Running
#include<bits/stdc++.h> using namespace std; long long k,a,b,c,dd,d[5][600005]; vector<pair<long long,int> >g[5]; void sol() { scanf("%lld%lld%lld%lld%lld",&k,&a,&b,&c,&dd); long long w=2*min(a,b); for(int i=1;i<=4;i++)g[i].clear(); g[1].push_back(make_pair(2,a)); g[2].push_back(make_pair(1,a)); g[2].push_back(make_pair(3,b)); g[3].push_back(make_pair(2,b)); g[3].push_back(make_pair(4,c)); g[4].push_back(make_pair(3,c)); g[4].push_back(make_pair(1,dd)); g[1].push_back(make_pair(4,dd)); memset(d,0x3f,sizeof(d)); d[2][0]=0; queue<pair<int,int> >q; q.push(make_pair(0,2)); while(!q.empty()) { long long l=q.front().first,x=q.front().second; q.pop(); for(int i=0;i<2;i++)if(d[g[x][i].first][(l+g[x][i].second)%w]>l+g[x][i].second) { d[g[x][i].first][(l+g[x][i].second)%w]=l+g[x][i].second; q.push(make_pair(l+g[x][i].second,g[x][i].first)); } } long long ans=2e18; for(int i=0;i<w;i++)if(d[2][i]>k)ans=min(ans,d[2][i]);else ans=min(ans,d[2][i]+(k-d[2][i]+w-1)/w*w); printf("%lld\n",ans); } int main() { int tt;cin>>tt; while(tt--)sol(); return 0; }
hdu 6073 Matching in Multiplication
#include<bits/stdc++.h> using namespace std; const int md=998244353; int n,vis[600005]; vector<pair<int,int> >g[600005]; vector<int>e,t; void sol() { scanf("%d",&n); for(int i=1;i<=n*2;i++)vis[i]=0,g[i].clear(); for(int i=1;i<=n;i++) { int u,wu,v,wv; scanf("%d%d%d%d",&u,&wu,&v,&wv); g[i].push_back(make_pair(n+u,wu)); g[n+u].push_back(make_pair(i,wu)); g[i].push_back(make_pair(n+v,wv)); g[n+v].push_back(make_pair(i,wv)); } bool f=false; for(int i=1;i<=n;i++)if(!g[n+i].size())f=true; if(f){printf("0\n");return;} int ans=1; for(int i=n+1;i<=2*n;i++)if(g[i].size()==1&&!vis[i]) { int res=g[i][0].second,lst=i,nw=g[i][0].first; while(true) { vis[lst]=1;vis[nw]=1; int nxt=g[nw][0].first+g[nw][1].first-lst; if(g[nxt].size()==1)res=0; if(g[nxt].size()!=2) { int j; for(j=0;j<g[nxt].size();j++)if(g[nxt][j].first==nw)break; g[nxt].erase(g[nxt].begin()+j); break; } lst=nxt; nw=g[nxt][0].first+g[nxt][1].first-nw; int cc=(g[lst][0].first==nw)?g[lst][0].second:g[lst][1].second; res=1ll*res*cc%md; } ans=1ll*ans*res%md; } for(int i=1;i<=2*n;i++)if(g[i].size()==2&&!vis[i]) { e.clear();t.clear(); e.push_back(i);e.push_back(g[i][0].first); t.push_back(g[i][0].second); int lst=i,nw=g[i][0].first; vis[lst]=1; while(true) { vis[nw]=1; int nxt=g[nw][0].first+g[nw][1].first-lst; int cc=(g[nw][0].first==nxt)?g[nw][0].second:g[nw][1].second; t.push_back(cc); if(vis[nxt])break; lst=nw;nw=nxt;e.push_back(nw); } int res1=1,res2=1; for(int i=0;i<t.size();i+=2)res1=1ll*res1*t[i]%md; for(int i=1;i<t.size();i+=2)res2=1ll*res2*t[i]%md; res1=(res1+res2)%md; ans=1ll*ans*res1%md; } printf("%d\n",ans); } int main() { int tt;scanf("%d",&tt); while(tt--)sol(); return 0; }
hdu 6070 Dirt Ratio
#include<bits/stdc++.h> using namespace std; const double eps=1e-6; struct st{double mn,tg;}seg[240005]; int n,a[60005],ps[60005]; void up(int x){seg[x].mn=min(seg[x<<1].mn,seg[x<<1|1].mn);} void down(int x) { seg[x<<1].mn+=seg[x].tg; seg[x<<1].tg+=seg[x].tg; seg[x<<1|1].mn+=seg[x].tg; seg[x<<1|1].tg+=seg[x].tg; seg[x].tg=0; } void build(int x,int l,int r) { seg[x]=(st){0,0}; if(l==r)return; int md=l+r>>1;build(x<<1,l,md);build(x<<1|1,md+1,r); } void modify(int x,int l,int r,int tl,int tr,double v) { if(tl<=l&&r<=tr){seg[x].mn+=v,seg[x].tg+=v;return;} if(seg[x].tg)down(x); int md=l+r>>1; if(tl<=md)modify(x<<1,l,md,tl,tr,v); if(tr>md)modify(x<<1|1,md+1,r,tl,tr,v); up(x); } double query(int x,int l,int r,int tl,int tr) { if(tl<=l&&r<=tr)return seg[x].mn; if(seg[x].tg)down(x); int md=l+r>>1;double ans=1e9; if(tl<=md)ans=min(ans,query(x<<1,l,md,tl,tr)); if(tr>md)ans=min(ans,query(x<<1|1,md+1,r,tl,tr)); return ans; } bool chk(double x) { build(1,1,n); memset(ps,0,sizeof(ps)); for(int i=1;i<=n;i++) { modify(1,1,n,ps[a[i]]+1,i,1); modify(1,1,n,1,i,-x); if(query(1,1,n,1,i)<=0)return true; ps[a[i]]=i; } return false; } int main() { int tt;scanf("%d",&tt); while(tt--) { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); double l=0,r=1,ans=1; while(r-l>=eps) { double md=(l+r)/2.0; if(chk(md))ans=md,r=md;else l=md; } printf("%.6f\n",ans); } return 0; }
hdu 6078 Wavel Sequence
#include<bits/stdc++.h> using namespace std; const long long md=998244353ll; int n,m,a[2005],b[2005]; long long dp[2][2005]; void sol() { scanf("%d%d",&n,&m); for(int i=0;i<n;i++)scanf("%d",&a[i]); for(int i=0;i<m;i++)scanf("%d",&b[i]); long long ans=0; memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { long long s1=0,s2=0;//·?£?1è for(int j=0;j<m;j++) { if(a[i]>b[j])dp[0][j]=(dp[0][j]+s1)%md; else if(a[i]<b[j])dp[1][j]=(dp[1][j]+s2)%md; else { s1=(s1+dp[1][j])%md; s2=(s2+dp[0][j]+1)%md; } } ans=(ans+s1+s2)%md; } printf("%lld\n",ans); } int main() { int tt;scanf("%d",&tt); while(tt--)sol(); return 0; }