bzoj 1800 & 洛谷 P2165 [AHOI2009]飞行棋 —— 模拟
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1800
https://www.luogu.org/problemnew/show/P2165
n 才20,所以模拟;
一开始是这样写的,惨WA2个点:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int n; ll a[25],s[25],ans,sum; int main() { scanf("%d",&n); // for(int i=1;i<=2*n;i++) // { // if(i<=n)scanf("%lld",&a[i]); else a[i]=a[i-n]; // s[i]=s[i-1]+a[i]; // } // sum=s[n]; for(int i=1;i<=n;i++)scanf("%d",&a[i]),s[i]=s[i-1]+a[i],sum+=a[i]; for(int i=n+1;i<=n+n;i++)a[i]=a[i-n],s[i]=s[i-1]+a[i]; for(int i=1;i<=n;i++) for(int j=i;s[j]-s[i-1]<sum/2;j++) { int t=s[j]-s[i-1],k=j,p; while(s[k+1]-s[i-1]<=sum/2)k++; if(s[k]-s[i-1]!=sum/2||k==j)continue;//k==j p=k+1; while(s[p+1]-s[k]<=t)p++; if(s[p]-s[k]!=t||p==k)continue;//p==k ans++; } printf("%lld\n",ans/4); return 0; }
然后又改改,惨WA6个点:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int a[25],n; ll s[25],ans,sum; int main() { scanf("%d",&n); for(int i=1;i<=2*n;i++) { if(i<=n)scanf("%d",&a[i]); else a[i]=a[i-n]; s[i]=s[i-1]+a[i]; } sum=s[n]; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) for(int k=j+2;k<=n;k++) for(int l=k+1;l<=n;l++) if(s[j]-s[i-1]==s[l]-s[k-1]&&s[k-1]-s[j]==sum-s[l]+s[i-1])ans++; printf("%lld\n",ans); return 0; }
完全改成TJ样子就A了...
不会写模拟了...
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int n; ll a[25],s[25],ans,sum; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++) { s[i+1]=s[i]+a[i]; sum+=a[i]; } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) for(int k=j+1;k<=n;k++) for(int l=k+1;l<=n;l++) if(s[j]-s[i]==s[l]-s[k]&&s[k]-s[j]==sum-s[l]+s[i])ans++; printf("%lld\n",ans); return 0; }
也可以求出有多少条直径,然后从中任选2条就可以组成一个矩形。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int n,a[25],s[25],ans,sum; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]),s[i]=s[i-1]+a[i],sum+=a[i]; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(s[j]-s[i]==sum/2)ans++;//不是-s[i-1] printf("%d\n",ans*(ans-1)/2); return 0; }