Codeforces Round #277.5 (Div. 2) ABCDF

http://codeforces.com/contest/489

 

Problems
 
 
#Name  
A
standard input/output
1 s, 256 MB
Submit Add to favourites  x2668
B
standard input/output
1 s, 256 MB
Submit Add to favourites  x2659
C
standard input/output
1 s, 256 MB
Submit Add to favourites  x2261
D
standard input/output
1 s, 256 MB
Submit Add to favourites  x1151
E
standard input/output
1 s, 256 MB
Submit Add to favourites  x165
F
standard input/output
1 s, 256 MB
Submit Add to favourites  x427

 

ABCD都是水题,F题有点碉,是个记忆化搜索/DP。

代码:

A:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13 #define mz(array) memset(array, 0, sizeof(array))
14 #define mf1(array) memset(array, -1, sizeof(array))
15 #define minf(array) memset(array, 0x3f, sizeof(array))
16 #define REP(i,n) for(i=0;i<(n);i++)
17 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
18 #define RD(x) scanf("%d",&x)
19 #define RD2(x,y) scanf("%d%d",&x,&y)
20 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
21 #define WN(x) printf("%d\n",x);
22 #define RE  freopen("D.in","r",stdin)
23 #define WE  freopen("huzhi.txt","w",stdout)
24 #define mp make_pair
25 #define pb push_back
26 #define pf push_front
27 #define ppf pop_front
28 #define ppb pop_back
29 typedef long long ll;
30 typedef unsigned long long ull;
31 
32 const double pi=acos(-1.0);
33 const double eps=1e-10;
34 
35 const int maxn=3333;
36 
37 int n;
38 
39 int ans;
40 int a[maxn];
41 int ax[maxn],ay[maxn];
42 
43 ll farm(){
44     int i,j,k;
45     ll re=0;
46     ans=0;
47     FOR(i,0,n-2){
48         k=i;
49         FOR(j,i+1,n-1){
50             if(a[j]<a[k]){
51                 k=j;
52             }
53         }
54         if(k!=i){
55             ax[ans]=i;
56             ay[ans]=k;
57             ans++;
58             swap(a[k],a[i]);
59         }
60     }
61     return re;
62 }
63 
64 int main(){
65     int i;
66     scanf("%d",&n);
67     REP(i,n)RD(a[i]);
68     farm();
69     WN(ans);
70     REP(i,ans){
71         printf("%d %d\n",ax[i],ay[i]);
72     }
73     return 0;
74 }
View Code

 

B:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13 #define mz(array) memset(array, 0, sizeof(array))
14 #define mf1(array) memset(array, -1, sizeof(array))
15 #define minf(array) memset(array, 0x3f, sizeof(array))
16 #define REP(i,n) for(i=0;i<(n);i++)
17 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
18 #define RD(x) scanf("%d",&x)
19 #define RD2(x,y) scanf("%d%d",&x,&y)
20 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
21 #define WN(x) printf("%d\n",x);
22 #define RE  freopen("D.in","r",stdin)
23 #define WE  freopen("huzhi.txt","w",stdout)
24 #define mp make_pair
25 #define pb push_back
26 #define pf push_front
27 #define ppf pop_front
28 #define ppb pop_back
29 typedef long long ll;
30 typedef unsigned long long ull;
31 
32 const double pi=acos(-1.0);
33 const double eps=1e-10;
34 
35 const int maxn=111;
36 const int maxm=33333;
37 
38 int n,m;
39 
40 //struct Edge{
41 //    int v,next;
42 //}e[maxm];
43 //int head[maxn],en;
44 //
45 //inline void add(const int &x,const int &y){
46 //    e[en].v=y;
47 //    e[en].next=head[x];
48 //    head[x]=en++;
49 //}
50 
51 int a[maxn],b[maxn];
52 
53 int farm(){
54     sort(a,a+n);
55     sort(b,b+m);
56     int i,j;
57     j=0;
58     int ans=0;
59     REP(i,n){
60         while(j<m && b[j]<a[i]-1)j++;
61         if(j==m)break;
62         if(abs(a[i]-b[j])<=1){
63             j++;
64             ans++;
65         }
66     }
67     return ans;
68 }
69 
70 int main(){
71     int i;
72     RD(n);
73     REP(i,n)RD(a[i]);
74     RD(m);
75     REP(i,m)RD(b[i]);
76     WN(farm());
77     return 0;
78 }
View Code

 

C:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13 #define mz(array) memset(array, 0, sizeof(array))
14 #define mf1(array) memset(array, -1, sizeof(array))
15 #define minf(array) memset(array, 0x3f, sizeof(array))
16 #define REP(i,n) for(i=0;i<(n);i++)
17 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
18 #define FORD(i,x,y) for(i=(x);i>=(y);i--)
19 #define RD(x) scanf("%d",&x)
20 #define RD2(x,y) scanf("%d%d",&x,&y)
21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
22 #define WN(x) printf("%d\n",x);
23 #define RE  freopen("D.in","r",stdin)
24 #define WE  freopen("huzhi.txt","w",stdout)
25 #define mp make_pair
26 #define pb push_back
27 #define pf push_front
28 #define ppf pop_front
29 #define ppb pop_back
30 typedef long long ll;
31 typedef unsigned long long ull;
32 
33 const double pi=acos(-1.0);
34 const double eps=1e-10;
35 
36 const int maxn=3333;
37 const int maxm=33333;
38 
39 int m,s;
40 
41 //struct Edge{
42 //    int v,next;
43 //}e[maxm];
44 //int head[maxn],en;
45 //
46 //inline void add(const int &x,const int &y){
47 //    e[en].v=y;
48 //    e[en].next=head[x];
49 //    head[x]=en++;
50 //}
51 
52 char s1[111],s2[111];
53 
54 bool farm(){
55     if(m>1 && s==0)return 0;
56     if(s > m*9)return 0;
57     if(m==1 && s==0){
58         strcpy(s1,"0");
59         strcpy(s2,"0");
60         return 1;
61     }
62     int t=s;
63     s1[0]='1';
64     t--;
65     int i;
66     FORD(i,m-1,1){
67         if(t){
68             int q=min(9,t);
69             s1[i]='0'+q;
70             t-=q;
71         }else s1[i]='0';
72     }
73     if(t)s1[0]+=t;
74 
75     t=s;
76     FOR(i,0,m-1){
77         if(t){
78             int q=min(9,t);
79             s2[i]='0'+q;
80             t-=q;
81         }else s2[i]='0';
82     }
83     return 1;
84 }
85 
86 int main(){
87     RD2(m,s);
88     if(farm())
89         printf("%s %s\n",s1,s2);
90     else puts("-1 -1");
91     return 0;
92 }
View Code

 

D:

  1 //#pragma comment(linker, "/STACK:102400000,102400000")
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<iostream>
  5 #include<cstring>
  6 #include<algorithm>
  7 #include<cmath>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<queue>
 12 using namespace std;
 13 #define mz(array) memset(array, 0, sizeof(array))
 14 #define mf1(array) memset(array, -1, sizeof(array))
 15 #define minf(array) memset(array, 0x3f, sizeof(array))
 16 #define REP(i,n) for(i=0;i<(n);i++)
 17 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
 18 #define RD(x) scanf("%d",&x)
 19 #define RD2(x,y) scanf("%d%d",&x,&y)
 20 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
 21 #define WN(x) printf("%d\n",x);
 22 #define RE  freopen("D.in","r",stdin)
 23 #define WE  freopen("huzhi.txt","w",stdout)
 24 #define mp make_pair
 25 #define pb push_back
 26 #define pf push_front
 27 #define ppf pop_front
 28 #define ppb pop_back
 29 typedef long long ll;
 30 typedef unsigned long long ull;
 31 
 32 const double pi=acos(-1.0);
 33 const double eps=1e-10;
 34 
 35 const int maxn=3333;
 36 const int maxm=33333;
 37 
 38 int n,m;
 39 
 40 struct Edge{
 41     int v,next;
 42 }e[maxm];
 43 int head[maxn],en;
 44 
 45 inline void add(const int &x,const int &y){
 46     e[en].v=y;
 47     e[en].next=head[x];
 48     head[x]=en++;
 49 }
 50 
 51 ll ans;
 52 
 53 int a[maxn][maxn];
 54 int gf;
 55 
 56 
 57 void dfs(int x,int y){
 58     if(y==0){
 59         a[gf][x]++;
 60         return;
 61     }
 62     int i;
 63     for(i=head[x]; i!=-1; i=e[i].next){
 64         if(e[i].v!=gf)dfs(e[i].v, y-1);
 65     }
 66 }
 67 
 68 void farm(){
 69     int i,j;
 70     ans=0;
 71     mz(a);
 72     FOR(i,1,n){
 73         gf=i;
 74         dfs(i,2);
 75     }
 76     FOR(i,1,n){
 77         FOR(j,1,n){
 78             if(i!=j){
 79                 if(a[i][j]>=2){
 80                     ans+= a[i][j]*(a[i][j]-1)/2 ;
 81                 }
 82             }
 83         }
 84     }
 85 }
 86 
 87 int main(){
 88     int i,j;
 89     int x,y;
 90     RD2(n,m);
 91     en=0;
 92     mf1(head);
 93     REP(i,m){
 94         RD2(x,y);
 95         add(x,y);
 96     }
 97     farm();
 98     printf("%I64d",ans);
 99     return 0;
100 }
View Code

 

F:

  1 //#pragma comment(linker, "/STACK:102400000,102400000")
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<iostream>
  5 #include<cstring>
  6 #include<algorithm>
  7 #include<cmath>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<queue>
 12 using namespace std;
 13 #define mz(array) memset(array, 0, sizeof(array))
 14 #define mf1(array) memset(array, -1, sizeof(array))
 15 #define minf(array) memset(array, 0x3f, sizeof(array))
 16 #define REP(i,n) for(i=0;i<(n);i++)
 17 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
 18 #define RD(x) scanf("%d",&x)
 19 #define RD2(x,y) scanf("%d%d",&x,&y)
 20 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
 21 #define WN(x) printf("%d\n",x);
 22 #define RE  freopen("D.in","r",stdin)
 23 #define WE  freopen("huzhi.txt","w",stdout)
 24 #define mp make_pair
 25 #define pb push_back
 26 #define pf push_front
 27 #define ppf pop_front
 28 #define ppb pop_back
 29 typedef long long ll;
 30 typedef unsigned long long ull;
 31 
 32 const double pi=acos(-1.0);
 33 const double eps=1e-10;
 34 
 35 const int maxn=555;
 36 
 37 int n,m,mod;
 38 
 39 int a[maxn][maxn];
 40 
 41 int r[maxn],c[maxn];
 42 
 43 ll f[maxn][maxn];
 44 
 45 inline ll gank(const int &w1, const int &w0){
 46     //printf("%d,%d\n",w1,w0);
 47     if(f[w1][w0] != -1)return f[w1][w0];
 48     ll re=0;
 49     if(w0>1){
 50         re+=w0*(w0-1)/2 * gank(w1+2,w0-2);
 51         re%=mod;
 52     }
 53     if(w0>0 && w1>0){
 54         re+=w0*w1 * gank(w1,w0-1);
 55         re%=mod;
 56     }
 57     if(w1>1){
 58         re+=w1*(w1-1)/2 * gank(w1-2,w0);
 59         re%=mod;
 60     }
 61     f[w1][w0]=re;
 62     return re;
 63 }
 64 
 65 ll farm(){
 66     int i,j,k;
 67 //    REP(i,n){
 68 //        REP(j,n)printf("%d ",a[i][j]);
 69 //        puts("");
 70 //    }
 71     mz(r);
 72     mz(c);
 73     REP(i,m)REP(j,n){
 74         if(a[i][j]){
 75             r[i]++;
 76             c[j]++;
 77         }
 78     }
 79     REP(i,n){
 80         if(r[i]>2 || c[i]>2)return 0;
 81     }
 82     REP(i,m) if(r[i]!=2)return 0;
 83     int ww[3]={0};
 84     REP(i,n){
 85         ww[c[i]]++;
 86     }
 87     ll ans=0;
 88     mf1(f);
 89     f[0][0]=1;
 90     ans = gank(ww[1], ww[0]);
 91     return ans;
 92 }
 93 
 94 int main(){
 95     char c;
 96     int i,j;
 97     RD3(n,m,mod);
 98     mf1(a);
 99     REP(i,m){
100         REP(j,n){
101             do{c=getchar();}while(c!='0' && c!='1');
102             a[i][j] = c-'0';
103         }
104     }
105     printf("%I64d\n",farm());
106     return 0;
107 }
View Code

 

posted @ 2014-11-18 15:49  带鱼Yuiffy  阅读(230)  评论(0编辑  收藏  举报