ZOJ Problem Set - 1788 Quad Trees
根据题意模拟即可
// // main.cpp // zoj1788 // // Created by cfhaiteeh on 13/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstring> #include <cmath> #include <cstdio> #include <queue> using namespace std; const int maxn=1234567; struct he{ int x1,y1,x2,y2; }; int ai[maxn]; int mat[513][513]; int T,n; void init(){ scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&mat[i][j]); } int st; bool getCmp(int x1,int y1,int x2,int y2){ int cmp=mat[x1][y1]; for(int i=x1;i<=x2;i++){ for(int j=y1;j<=y2;j++){ if(mat[i][j]!=cmp)return 0; } } return 1; } queue<he> Q; void pushQ(int x1,int x2,int y1,int y2){ he c; c.x1=x1; c.x2=x2; c.y1=y1; c.y2=y2; Q.push(c); } void BFS(){ he fir; fir.x1=1; fir.y1=1; fir.x2=n; fir.y2=n; st=0; Q.push(fir); while(!Q.empty()){ he c=Q.front(); Q.pop(); bool flag=getCmp(c.x1, c.y1, c.x2, c.y2); if(flag){ ai[++st]=0; ai[++st]=mat[c.x1][c.y1]; } else{ ai[++st]=1; int mid1=(c.x1+c.x2)/2; int mid2=(c.y1+c.y2)/2; pushQ(c.x1, mid1, c.y1, mid2); pushQ(c.x1,mid1,mid2+1,c.y2); pushQ(mid1+1, c.x2, c.y1, mid2); pushQ(mid1+1,c.x2,mid2+1,c.y2); } } } char ans[maxn]; int ci[]={1,2,4,8}; char getChar(int t){ int s=0; for(int i=0;i<4;i++){ s=s+ai[t]*ci[i]; t--; if(t==0)break; } if(s>10){ s-=10; return s+'A'; } return s+'0'; } int now; void play(){ if(!Q.empty()){ Q.pop(); } BFS(); now=0; for(int i=st;i>=1;i-=4){ ans[++now]=getChar(i); } } void pri(){ for(int i=now;i>=1;i--)printf("%c",ans[i]); printf("\n"); } int main() { // insert code here... scanf("%d",&T); while(T--){ init(); play(); pri(); } return 0; }
ZOJ Problem Set - 2418 Matrix
// // main.cpp // zoj2418 // // Created by cfhaiteeh on 13/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cmath> #include <queue> #include <cstdio> #include <cstring> #include <algorithm> #define LL long long #define INF 1e9 using namespace std; int n; int MIN; int mat[8][8]; int ai[8][8]; void init(){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&mat[i][j]); memset(ai,0,sizeof(ai)); } void DFS(int depth){ if(depth==n+1){ int MAX=0; for(int i=1;i<=n;i++) MAX=max(ai[n][i],MAX); MIN=min(MIN,MAX); return; } for(int i=1;i<=n;i++){ int st=1; int t=i; while(st<=n){ ai[depth][st]=mat[depth][t]; t++; st++; if(t==n+1)t=1; } for(int j=1;j<=n;j++){ ai[depth][j]+=ai[depth-1][j]; } DFS(depth+1); } } int ans; void play(){ MIN=1e9; DFS(1); ans=MIN; } void pri(){ printf("%d\n",ans); } int main() { while(scanf("%d",&n)!=EOF){ if(n==-1)break; init(); play(); pri(); } return 0; }
ZOJ Problem Set - 2974 Just Pour the Water
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <stack> #include <queue> #include <vector> #include <set> using namespace std; const int maxn=23; double ai[maxn]; struct matrix { double e[maxn][maxn]; }; matrix Cal(matrix a,matrix b,int len) { matrix c; for(int i=1; i<=len; i++) { for(int j=1; j<=len; j++) { c.e[i][j]=0; for(int k=1; k<=len; k++) { c.e[i][j]+=a.e[i][k]*b.e[k][j]; } } } return c; } matrix a,s; int n; void exp_pow(int b) { while(b) { if(b&1)s=Cal(s,a,n); a=Cal(a,a,n); b>>=1; } } void init() { scanf("%d",&n); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { s.e[i][j]=0; } } for(int i=1; i<=n; i++) { scanf("%lf",&s.e[1][i]); } int k,t; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { a.e[i][j]=0; } } for(int i=1; i<=n; i++) { scanf("%d",&k); if(k==0) { a.e[i][i]=1; } else { double fm=1.0/k; for(int j=1; j<=k; j++) { scanf("%d",&t); a.e[i][t]=fm; } } } } void play() { int k; scanf("%d",&k); exp_pow(k); bool first=1; for(int i=1; i<=n; i++) { if(!first)printf(" "); first=0; printf("%.2f",s.e[1][i]); } printf("\n"); } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3829 Known Notation
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #include <stack> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=1234; char ch[maxn]; int len; int ai[maxn]; int f; void init() { scanf("%s",ch+1); len=strlen(ch+1); f=0; for(int i=1; i<=len; i++) { if(ch[i]=='*')ai[i]=1; else ai[i]=0; f+=ai[i]; } } void play() { int f2=len-f; f2=f-f2; int ans=0; if(f2>=0) { ans=f2+1; } int before=ans; int st=len; for(int i=1; i<=len; i++) { if(ai[i]==1) { if(before<2) { while(ai[st]!=0) { st--; } ai[st]=1; before++; ans++; } else { before--; } } else { before++; } } if(ai[len]!=1&&f!=0)ans++; printf("%d\n",ans); } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3913 Bob wants to pour water
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const double PI=3.1415926535898; double CalSphere(double r,double h) { return PI*(r*h*h-1.0*h*h*h/3); } const int maxn=123456; struct he { double ch,w,l,h,r; } Sky[maxn*2]; double w,l,v; int n,m; double maxH; void init() { maxH=0; scanf("%lf%lf%lf%d%d",&w,&l,&v,&n,&m); for(int i=1; i<=n; i++) { scanf("%lf%lf%lf%lf",&Sky[i].ch,&Sky[i].w,&Sky[i].l,&Sky[i].h); Sky[i].ch=Sky[i].ch-Sky[i].h/2; maxH=max(maxH,Sky[i].ch+Sky[i].h); } for(int i=1; i<=m; i++) { scanf("%lf%lf",&Sky[n+i].ch,&Sky[n+i].r); Sky[n+i].ch=Sky[n+i].ch-Sky[n+i].r; maxH=max(maxH,Sky[n+i].ch+Sky[n+i].r*2); } } bool check(double h) { double nv=0; for(int i=1; i<=n; i++) { double mh=Sky[i].ch+Sky[i].h; if(mh<=h) { nv+=Sky[i].h*Sky[i].l*Sky[i].w; } else if(Sky[i].ch>=h) { } else { double hh=h-Sky[i].ch; nv+=hh*Sky[i].l*Sky[i].w; } } for(int i=1; i<=m; i++) { double mh=Sky[n+i].ch+Sky[n+i].r*2; if(mh<=h) { nv+=CalSphere(Sky[n+i].r,Sky[n+i].r*2); } else if(Sky[n+i].ch>=h) { } else { double hh=h-Sky[n+i].ch; nv+=CalSphere(Sky[n+i].r,hh); } } double cm=w*l*h; if(cm<=nv+v) { return 1; } else { return 0; } } double BRSearch() { double l=0; double r=maxH+1000; double ans=0; while(l<=r) { double mid=(l+r)/2; bool ck=check(mid); if(ck) { ans=mid; l=mid+EPS; } else { r=mid-EPS; } } return ans; } void play() { double ans=BRSearch(); if(ans>maxH) { for(int i=1; i<=n; i++) { v+=Sky[i].w*Sky[i].l*Sky[i].h; } for(int i=1; i<=m; i++) { v+=CalSphere(Sky[n+i].r,Sky[n+i].r*2); } ans=v/(w*l); printf("%.8f\n",ans); } else { printf("%.8f\n",ans); } } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } return 0; }
ZOJ Problem Set - 3908 Number Game
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=123456; int ai[maxn]; int Link[12345]; int bi[12345]; int T; int n,m,k; bool cmp(int a,int b) { return a>b; } void init() { scanf("%d%d%d",&n,&m,&k); memset(bi,0,sizeof(bi)); for(int i=1; i<=n; i++) { scanf("%d",&ai[i]); if(ai[i]==0)continue; bi[ai[i]]++; } Link[0]=0; bi[0]=100000000; for(int i=1; i<=10000; i++) { if(bi[i]==0)Link[i]=Link[i-1]; else Link[i]=i; } sort(ai+1,ai+1+n,cmp); } vector<int> V; int Find(int x) { if(Link[x]==x)return x; Link[x]=Find(Link[x]); return Link[x]; } void play() { V.clear(); for(int i=1; i<=n; i++) { int cz=k-ai[i]; if(ai[i]==0)continue; if(bi[ai[i]]==0)continue; bi[ai[i]]--; if(bi[ai[i]]==0)Link[ai[i]]=Link[ai[i]-1]; if(cz<=0)continue; if(cz>10000)cz=10000; int f=Find(cz); bi[f]--; if(bi[f]==0)Link[f]=Link[f-1]; V.push_back(f*ai[i]); } int p=V.size(); LL ans=0; sort(V.begin(),V.end(),cmp); for(int i=0; i<min(p,m); i++) { ans+=V[i]; } printf("%lld\n",ans); } int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3903 Ant
import java.math.BigInteger; import java.util.*; public class zoj3903 { public static BigInteger pow2(long p) { BigInteger big = BigInteger.ZERO; BigInteger nn = new BigInteger(p + ""); BigInteger nn1 = nn.add(BigInteger.ONE); BigInteger tw = new BigInteger(2 + ""); BigInteger si = new BigInteger(6 + ""); big = nn.multiply(nn1).multiply(nn.multiply(tw).add(BigInteger.ONE)); return big.divide(si); } public static BigInteger pow3(long p) { BigInteger big = BigInteger.ZERO; BigInteger nn = new BigInteger(p + ""); BigInteger nn1 = nn.add(BigInteger.ONE); BigInteger tw = new BigInteger(2 + ""); big = nn.multiply(nn1).divide(tw); return big.multiply(big); } public static void main(String[] args) { // TODO Auto-generated method stub int T; Scanner sca = new Scanner(System.in); T = sca.nextInt(); long p; BigInteger MOD = new BigInteger("1000000007"); long o = 1; BigInteger tw = new BigInteger(2 + ""); for (int i = 1; i <= T; i++) { p = Long.parseLong(sca.next()); p++; BigInteger q = new BigInteger((p - 1) + ""); BigInteger b1 = pow3(p).subtract(pow3(o)); BigInteger b2 = pow2(p).subtract(pow2(o)); BigInteger ans = b1.subtract(b2); BigInteger b3 = pow2((p - 1) * 2).subtract(pow2(p)); b3 = b3.multiply(q); BigInteger b4 = pow3((p - 1) * 2).subtract(pow3(p)); BigInteger b5 = pow2((p - 1) * 2).subtract(pow2(p)); b5=b5.multiply(new BigInteger(p+"")); BigInteger ans1 = b4.subtract(b5); ans1 = b3.subtract(ans1); ans = ans.add(ans1); ans =ans.add(pow2(p - 1).multiply(tw).multiply(tw)); ans = ans.divide(tw); BigInteger st = q.multiply(new BigInteger(p + "")).divide(tw) .multiply(q).multiply(q); ans = ans.add(st); ans = ans.mod(MOD); System.out.println(ans); } } }
ZOJ Problem Set - 3905 Cake
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=8*123; int dp[maxn][maxn]; int n; struct he { int id, x,y; } Sky[maxn]; bool cmp(he a,he b) { return a.y>b.y; } void init() { scanf("%d",&n); memset(dp,0,sizeof(dp)); for(int i=1; i<=n; i++) { scanf("%d%d",&Sky[i].x,&Sky[i].y); Sky[i].id=i; } sort(Sky+1,Sky+1+n,cmp); } void play() { dp[1][0]=0; for(int i=2; i<=n; i++) { int x=Sky[i].x; int y=Sky[i].y; for(int j=1; j<i; j++) { int le=j; int ri=i-j-1; if(le<ri)continue; dp[le+1][ri]=dp[le][ri]; } for(int j=1; j<i; j++) { int le=j; int ri=i-j-1; if(le<=ri)continue; dp[le][ri+1]=max(dp[le][ri]+x,dp[le][ri+1]); } } printf("%d\n",dp[n/2][n/2]); } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3911 Prime Query
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=12345678; bool notPrime[maxn]; int ai[123456]; struct he { int l,r,lazy,ans; } Sky[123456*4]; void BuildTree(int l,int r,int now) { Sky[now].l=l; Sky[now].r=r; Sky[now].lazy=0; Sky[now].ans=0; if(l==r) { Sky[now].lazy=ai[l]; if(!notPrime[Sky[now].lazy])Sky[now].ans=1; else Sky[now].ans=0; return; } int mid=(l+r)>>1; BuildTree(l,mid,now<<1); BuildTree(mid+1,r,now<<1|1); Sky[now].ans=Sky[now<<1].ans+Sky[now<<1|1].ans; } void PushDown(int x) { if(Sky[x].lazy==0)return; int ls=x<<1; int rs=x<<1|1; if(!notPrime[Sky[x].lazy]) { Sky[ls].ans=Sky[ls].r-Sky[ls].l+1; Sky[rs].ans=Sky[rs].r-Sky[rs].l+1; } else { Sky[ls].ans=0; Sky[rs].ans=0; } Sky[ls].lazy=Sky[x].lazy; Sky[rs].lazy=Sky[x].lazy; Sky[x].lazy=0; }//单点 void Update1(int id,int v,int now) { if(Sky[now].l==id&&Sky[now].r==id) { Sky[now].lazy+=v; if(!notPrime[Sky[now].lazy])Sky[now].ans=1; else Sky[now].ans=0; return; } PushDown(now); int mid=(Sky[now].l+Sky[now].r)>>1; if(id<=mid) { Update1(id,v,now<<1); } else { Update1(id,v,now<<1|1); } Sky[now].ans=Sky[now<<1].ans+Sky[now<<1|1].ans; } //区间 void Update2(int l,int r,int now,int v) { if(Sky[now].l==l&&Sky[now].r==r) { Sky[now].lazy=v; if(!notPrime[v]) { Sky[now].ans=r-l+1; } else { Sky[now].ans=0; } return; } PushDown(now); int mid=(Sky[now].l+Sky[now].r)>>1; if(r<=mid) { Update2(l,r,now<<1,v); } else if(l>mid) { Update2(l,r,now<<1|1,v); } else { Update2(l,mid,now<<1,v); Update2(mid+1,r,now<<1|1,v); } Sky[now].ans=Sky[now<<1].ans+Sky[now<<1|1].ans; } int Query(int l,int r,int now) { if(Sky[now].l==l&&Sky[now].r==r) { return Sky[now].ans; } PushDown(now); int mid=(Sky[now].l+Sky[now].r)>>1; int o1=0,o2=0; if(r<=mid) { o1=Query(l,r,now<<1); } else if(l>mid) { o2=Query(l,r,now<<1|1); } else { o1=Query(l,mid,now<<1); o2=Query(mid+1,r,now<<1|1); } return o1+o2; } int T,N,Q; char OP[10]; void init() { scanf("%d%d",&N,&Q); for(int i=1; i<=N; i++) { scanf("%d",&ai[i]); } BuildTree(1,N,1); } void play() { int l,r,v; while(Q--) { scanf("%s",OP+1); scanf("%d%d",&v,&l); if(OP[1]=='A') { Update1(l,v,1); } else if(OP[1]=='R') { scanf("%d",&r); Update2(l,r,1,v); } else { int ans=Query(v,l,1); printf("%d\n",ans); } } } int main() { memset(notPrime,0,sizeof(notPrime)); notPrime[1]=1; for(int i = 2; i*i <= maxn; i++) { if(!notPrime[i]) { for(int j = i*i; j <= maxn; j += i) { notPrime[j] = true; } } } scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3497 Mistwald
/* Author:Chieh Because Of Coding */ #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <vector> #include <stack> #include <map> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; struct matrix { int e[26][26]; }; matrix Cal(matrix p,matrix q,int len) { matrix c; for(int i=1; i<=len; i++) { for(int j=1; j<=len; j++) { c.e[i][j]=0; for(int k=1; k<=len; k++) { c.e[i][j]=c.e[i][j]+p.e[i][k]*q.e[k][j]; } c.e[i][j]=min(1,c.e[i][j]); } } return c; } int can[6][6]; int T; char ch[123]; int n,m; bool dist[30][30]; matrix s,a; void init() { memset(dist,0,sizeof(dist)); scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { scanf("%s",ch+1); int u=can[i][j]; int len=strlen(ch+1); int o1=-1,o2=-1; for(int k=1; k<=len; k++) { if(ch[k]>='1'&&ch[k]<='5') { if(o1==-1)o1=ch[k]-'0'; else { o2=ch[k]-'0'; int v=can[o1][o2]; dist[u][v]=1; o1=-1; o2=-1; } } } } } } void exp_pow(int b) { while(b) { if(b&1)s=Cal(s,a,25); a=Cal(a,a,25); b>>=1; } } int ai[30]; int bi[30]; void play() { int q,t; scanf("%d",&q); while(q--) { scanf("%d",&t); for(int i=1; i<=25; i++) { for(int j=1; j<=25; j++) { a.e[i][j]=0; if(i==j)s.e[i][j]=1; else s.e[i][j]=0; if(i==can[n][m])continue; if(dist[i][j]==1) { a.e[i][j]=1; } } } exp_pow(t); int sum=0; bool flag=0; for(int j=1; j<=25; j++) { int tm=0; for(int k=1; k<=25; k++) { tm=tm+ai[k]*s.e[k][j]; } tm=min(tm,1); if(tm)sum++; if(j==can[n][m]&&tm)flag=1; } if(flag&&sum>1)printf("Maybe\n"); else if(flag&&sum==1)printf("True\n"); else printf("False\n"); } } int main() { memset(ai,0,sizeof(ai)); ai[1]=1; int st=0; for(int i=1; i<=5; i++) { for(int j=1; j<=5; j++) { can[i][j]=++st; } } scanf("%d",&T); while(T--) { init(); play(); printf("\n"); } // cout << "Hello world!" << endl; return 0; }