标签:数据 预处理 优先 ++ nbsp inf 分享 display query
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<string> 7 #define ll long long 8 #define db double 9 #define N 60000 10 using namespace std; 11 int n; 12 int a[N],q[N*4]; 13 ll c[2][N]; 14 int lowbit(int x){ 15 return x&(-x); 16 } 17 void update(int x){ 18 while (x<=32770){ 19 q[x]++; 20 x+=lowbit(x); 21 } 22 } 23 int query(int x){ 24 int ans=0; 25 while (x>0){ 26 ans+=q[x]; 27 x-=lowbit(x); 28 } 29 return ans; 30 } 31 int main(){ 32 scanf ("%d",&n); 33 int x; 34 ll ans=0; 35 memset(q,0,sizeof(q)); 36 for (int i=1;i<=n;++i) scanf ("%d",&x),a[i]=x+1; 37 for (int i=1;i<=n;++i){ 38 c[0][i]=query(a[i]-1); 39 update(a[i]); 40 } 41 memset(q,0,sizeof(q)); 42 for (int i=n;i>=1;--i){ 43 c[1][i]=query(a[i]-1); 44 update(a[i]); 45 } 46 for (int i=1;i<=n;++i) ans+=c[0][i]*c[1][i]; 47 printf ("%lld\n",ans); 48 return 0; 49 }
2.黄金矿工
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<string> 7 #define ll long long 8 #define db double 9 #define N 400 10 #define inf -9000010 11 using namespace std; 12 int n; 13 ll w[N][N],s[N][N],p[N]; 14 int main(){ 15 scanf ("%d",&n); 16 for (int i=1;i<=n;++i) 17 for (int j=1;j<=n;++j){ 18 scanf ("%lld",&w[i][j]); 19 if (!w[i][j]) w[i][j]=inf; 20 s[i][j]=s[i-1][j]+w[i][j]; 21 } 22 ll mx=inf*1000; 23 for (int i=0;i<n;++i) 24 for (int j=i+1;j<=n;++j){ 25 memset(p,0,sizeof(p)); 26 for (int k=1;k<=n;++k) p[k]=max(s[j][k]-s[i][k],s[j][k]-s[i][k]+p[k-1]); 27 for (int k=1;k<=n;++k) mx=max(mx,p[k]); 28 } 29 if (mx<=10) mx=10; 30 printf ("%lld\n",mx-10); 31 return 0; 32 }
3.旅行
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<string> 7 #define ll long long 8 #define db double 9 #define N 6000 10 #define inf 100000000 11 using namespace std; 12 int n,m,tot=0,s,t,a1,a2; 13 int head[N*4],vis[N*2],f[N]; 14 db ans=inf; 15 struct hh{ 16 int x,y,z; 17 }b[N*4]; 18 int gcd(int x,int y){return y==0?x:gcd(y,x%y);} 19 int find(int x){return x==f[x]?x:f[x]=find(f[x]);} 20 bool cmp(hh a,hh b){return a.z<b.z;} 21 int main(){ 22 scanf ("%d%d",&n,&m); 23 for (int i=1;i<=n;++i) f[i]=i; 24 int x,y,z; 25 for (int i=1;i<=m;++i){ 26 scanf ("%d%d%d",&x,&y,&z); 27 int f1=find(x),f2=find(y); 28 if (f1!=f2) f[f1]=f2; 29 b[++tot].x=x,b[tot].y=y,b[tot].z=z; 30 } 31 scanf ("%d%d",&s,&t); 32 if (find(s)!=find(t)){printf ("IMPOSSIBLE\n") ; return 0; } 33 sort(b+1,b+m+1,cmp); 34 for (int k=1;k<=m;++k){ 35 int l; 36 bool fl=0; 37 for (int i=1;i<=n;++i) f[i]=i; 38 for (int i=k;i<=m;++i){ 39 int f1=find(b[i].x),f2=find(b[i].y); 40 if (f1!=f2) f[f1]=f2; 41 if (find(s)==find(t)){ 42 l=i; 43 fl=1; 44 break; 45 } 46 } 47 if (fl){ 48 db q=(b[l].z+0.0)/(b[k].z+0.0); 49 if (q<ans) ans=q,a1=b[l].z,a2=b[k].z; 50 } 51 } 52 int q=gcd(a1,a2); 53 a1/=q,a2/=q; 54 if (a2==1) printf ("%d\n",a1); 55 else printf("%d/%d\n",a1,a2); 56 return 0; 57 }
标签:数据 预处理 优先 ++ nbsp inf 分享 display query
原文地址:http://www.cnblogs.com/YZSTUDIO/p/6618626.html