2014 Multi-University Training Contest 5

官方解题报告http://blog.sina.com.cn/s/blog_a19ad7a10102uyxr.html

Matrix multiplication http://acm.hdu.edu.cn/showproblem.php?pid=4920

 1 #include<cstdio>
 2 #include<cctype>
 3 const int M=810;
 4 int a[M][M],b[M][M],c[M][M];
 5 inline int getint(){
 6     int res=0;
 7     char tmp;
 8     while(!isdigit(tmp=getchar()));
 9     do{
10         res=(res<<3)+(res<<1)+tmp-'0';
11     }while(isdigit(tmp=getchar()));
12     return res;
13 }
14 int main(){
15     int n,x,i,j,k;
16     while(~scanf("%d",&n)){
17         for(i=0;i<n;i++){
18             for(j=0;j<n;j++){
19                 a[i][j]=getint();
20                 a[i][j]%=3;
21             }
22         }
23         for(i=0;i<n;i++){
24             for(j=0;j<n;j++){
25                 b[i][j]=getint();
26                 b[i][j]%=3;
27                 c[i][j]=0;
28             }
29         }
30         for(k=0;k<n;k++){
31             for(i=0;i<n;i++){
32                 if(a[i][k]){
33                     for(j=0;j<n;j++){
34                         c[i][j]+=a[i][k]*b[k][j];
35                     }
36                 }
37             }
38         }
39         for(i=0;i<n;i++){
40             for(j=0;j<n;j++){
41                 if(j) putchar(' ');
42                 putchar(c[i][j]%3+'0');
43             }
44             putchar('\n');
45         }
46     }
47     return 0;
48 }
View Code

 

Inversion http://acm.hdu.edu.cn/showproblem.php?pid=4911

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cctype>
 4 typedef __int64 LL;
 5 const int M=100010;
 6 int a[M];
 7 class InverseNum {              //逆序对数O(nlogn)
 8 #define _cp(a,b) ((a)<=(b))     //可更改元素类型和比较函数
 9     typedef int typev;
10     typev b[M];
11 public:
12     LL inv(int n,typev a[]) {  //传入序列长度和内容,返回逆序对数
13         int l=n>>1,r=n-l,i,j;
14         LL ret=(r>1?(inv(l,a)+inv(r,a+l)):0);
15         for (i=j=0; i<=l; b[i+j]=a[i],i++)
16             for (ret+=j; j<r&&(i==l||!_cp(a[i],a[l+j])); b[i+j]=a[l+j],j++);
17         memcpy(a,b,sizeof(typev)*n);
18         return ret;
19     }
20 } gx;
21 int getint(){
22     int res=0;
23     char tmp;
24     while(!isdigit(tmp=getchar()));
25     do{
26         res=(res<<3)+(res<<1)+tmp-'0';
27     }while(isdigit(tmp=getchar()));
28     return res;
29 }
30 int main(){
31     int n,k;
32     while(~scanf("%d%d",&n,&k)){
33         for(int i=0;i<n;i++){
34 //            scanf("%d",&a[i]);
35             a[i]=getint();
36         }
37         LL ans=gx.inv(n,a);
38         if(ans<k) ans=0;
39         else ans-=k;
40         printf("%I64d\n",ans);
41     }
42     return 0;
43 }
View Code

 

 

 

end

posted on 2014-08-05 21:17  gaolzzxin  阅读(337)  评论(0编辑  收藏  举报