Codeforces Round #287 (Div. 2) ABCDE

A题。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 #define LL long long
11 #define eps 1e-8
12 #define inf 0x3f3f3f3f
13 #define lson l, m, rt << 1
14 #define rson m+1, r, rt << 1 | 1
15 #define mnx 500
16 
17 struct node{
18     int v, id;
19     bool operator < ( const node &b ) const {
20         return v < b.v;
21     }
22 }a[mnx];
23 int b[mnx];
24 int main(){
25     int n, k;
26     scanf( "%d %d", &n, &k );
27     for( int i = 1; i <= n; ++i ){
28         scanf( "%d", &a[i].v );
29         a[i].id = i;
30     }
31     sort( a + 1, a + 1 + n );
32     int i = 1, ans = 0, sum = 0;
33     while( sum + a[i].v <= k && i <= n ){
34         b[ans++] = a[i].id, sum += a[i++].v;
35     }
36     printf( "%d\n", ans );
37     for( i = 0; i < ans; ++i )
38         printf( "%d ", b[i] );
39     return 0;
40 }
View Code

B题。。注意有可能 爆int

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 #define LL long long
11 #define eps 1e-8
12 #define inf 0x3f3f3f3f
13 #define lson l, m, rt << 1
14 #define rson m+1, r, rt << 1 | 1
15 #define mnx 500
16 
17 int main(){
18     double r, x, y, xx, yy;
19     scanf( "%lf%lf%lf%lf%lf", &r, &x, &y, &xx, &yy );
20     double dis = sqrt( ( xx - x ) * ( xx - x ) + ( yy - y ) * ( yy - y ) );
21     int ans = ceil( dis / 2 / r );
22     cout << ans << endl;
23 }
View Code

C题。。把它当做一棵满二叉树来看,根节点是1,然后第二层是2,3, 第三层是4,5,6,7。。先计算出第h层第n个节点在整个满二叉树是第cur个,然后一直向上得到父亲节点,一直到根节点。。计算的话,因为是LRLRLR这样走的,如果a[i+1] %2 == a[i]%2的话,就要加上某一边的子树的所有节点b[h+1-i],不同的话就直接ans++就好。。具体自己画图加结合代码理解一下

 1 include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 #define LL long long
10 #define eps 1e-8
11 #define inf 0x3f3f3f3f
12 #define lson l, m, rt << 1
13 #define rson m+1, r, rt << 1 | 1
14 #define mnx 500
15 
16 LL a[mnx], b[mnx];
17 int main(){
18     LL h, n;
19     cin >> h >> n;
20     LL L = 1;
21     for( int i = 0; i < h; ++i )
22         L <<= 1;
23     b[0] = 1;
24     for( int i = 1; i <= h; ++i )
25         b[i] = b[i-1] * 2;
26     LL cur = L + n - 1;
27     int m = h + 1;
28     while( cur ){
29         a[m--] = cur;
30         cur >>= 1;
31     }
32     LL ans = 0;
33     for( int i = 1; i < h+1; ++i ){
34         if( a[i+1] % 2 == a[i] % 2 )
35             ans += b[h+1-i];
36         else ans++;
37     }
38     cout << ans << endl;
39     
View Code

D题。。不会,数位dp,复制别人的题解

给定n,k,m

求有多少个恰好为n位的整数,且这个整数的某个后缀能整除k,答案模m

思路:

因为是求后缀,所以从低位往高位构造。

dp[i][j][0] 表示长度为i位的整数,模k结果为j,且不存在某个后缀能整除k的个数

dp[i][j][1] 表示长度为i位的整数,模k结果为j,且存在某个后缀能整除k的个数

然后转移就在某个状态前加一个数字,判断一下加上后能否被k整除来决定加上数字后到达的状态

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 #include <map>
 8 #include <queue>
 9 
10 using namespace std;
11 
12 #define LL long long
13 #define eps 1e-8
14 #define inf 0x3f3f3f3f
15 #define lson l, m, rt << 1
16 #define rson m+1, r, rt << 1 | 1
17 #define mnx 1050
18 
19 LL dp[mnx][120][2], len[mnx], mod;
20 int n, k;
21 void add( LL &x, LL y ){
22     x = ( x + y ) % mod;
23 }
24 int main(){
25     cin >> n >> k >> mod;
26     dp[0][0][0] = 1, len[0] = 1;
27     for( int i = 1; i < mnx; ++i )
28         len[i] = len[i-1] * 10 % k;
29     for( int i = 0; i < n; ++i ){
30         for( int j = 0; j < k; ++j ){
31             for( int v = 0; v < 10; ++v ){
32                 if( i == n-1 && !v ) continue;
33                 LL val = ( v * len[i] + j ) % k;
34                 if( !val && v )
35                     add( dp[i+1][0][1], dp[i][j][0] );
36                 else
37                     add( dp[i+1][val][0], dp[i][j][0] );
38                 add( dp[i+1][val][1], dp[i][j][1] );
39             }
40         }
41     }
42     LL ans = 0;
43     for( int i = 0; i < k; ++i )
44         ans = ( ans + dp[n][i][1] ) % mod;
45     cout << ans << endl;
46     return 0;
47 }
View Code

E题。。最短路,感觉挺水的,把最短路上所有为0的边改为1,把不是最短路的边所有为1的变为0。。

表示自己的代码写的好挫。。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <cmath>
  6 #include <vector>
  7 #include <map>
  8 #include <queue>
  9 
 10 using namespace std;
 11 
 12 #define LL long long
 13 #define eps 1e-8
 14 #define inf 0x3f3f3f3f
 15 #define lson l, m, rt << 1
 16 #define rson m+1, r, rt << 1 | 1
 17 #define mnx 200500
 18 
 19 int fst[mnx], nxt[mnx], vv[mnx], work[mnx], e, dis[mnx], sum[mnx], n, fa[mnx];
 20 bool vis[mnx];
 21 map< pair<int,int>, int > mp;
 22 void add( int u, int v, int c ){
 23     vv[e] = v, nxt[e] = fst[u], work[e] = c, fst[u] = e++;
 24 }
 25 struct edge{
 26     int v, u, d;
 27     edge(){}
 28     edge( int u, int v, int d ) : u(u), v(v), d(d) {}
 29     bool operator < ( const edge &b ) const {
 30         return d > b.d;
 31     }
 32 }ee[mnx];
 33 void dij(){
 34     memset( fa, -1, sizeof fa );
 35     fa[1] = 1;
 36     memset( dis, 0x3f, sizeof dis );
 37     priority_queue<edge> q;
 38     dis[1] = 0;
 39     edge p;
 40     p.u = 1, p.d = 0;
 41     q.push( p );
 42     while( !q.empty() ){
 43         p = q.top(); q.pop();
 44         int u = p.u;
 45         if( vis[u] ) continue;
 46         vis[u] = 1;
 47         for( int i = fst[u]; i != -1; i = nxt[i] ){
 48             int v = vv[i], w = work[i];
 49             if( dis[v] > dis[u] + 1 || ( dis[v] == dis[u] + 1 && sum[v] < sum[u] + w ) ){
 50                 dis[v] = dis[u] + 1;
 51                 sum[v] = sum[u] + w;
 52                 edge pp;
 53                 pp.u = v, pp.d = dis[v];
 54                 q.push( pp );
 55                 fa[v] = u;
 56             }
 57         }
 58     }
 59     //cout << dis[n] << endl;
 60 }
 61 void dfs( int u ){
 62     vis[u] = 1;
 63     if( fa[u] != u ){
 64         dfs( fa[u] );
 65     }
 66 }
 67 int main(){
 68     int m, all = 0;
 69     scanf( "%d%d",&n, &m );
 70     memset( fst, -1, sizeof fst );
 71     for( int i = 0; i < m; ++i ){
 72         int u, v, c;
 73         scanf( "%d%d%d", &u, &v, &c );
 74         add( u, v, c );
 75         add( v, u, c );
 76         ee[all++] = edge( u, v, c );
 77         ee[all++] = edge( v, u, c );
 78     }
 79     dij();
 80     memset( vis, 0, sizeof vis );
 81     dfs( n );
 82     int ans = 0;
 83     for( int i = 1; i <= n; ++i ){
 84         int u = i;
 85         if( !vis[i] ){
 86             for( int j = fst[u]; j != -1; j = nxt[j] ){
 87                 int v = vv[j];
 88                 if( mp.find( make_pair( u, v ) ) == mp.end() && mp.find( make_pair( v, u ) ) == mp.end() ){
 89                     if( work[j] ) ans++;
 90                     mp[make_pair(u,v)]++;
 91                 }
 92             }
 93         }
 94         else{
 95             for( int j = fst[u]; j != -1; j = nxt[j] ){
 96                 int v = vv[j];
 97                 if( v == fa[u] ){
 98                     if( !work[j] ) ans++;
 99                     mp[make_pair(u,v)]++;
100                 }
101             }
102         }
103     }
104     for( int i = 1; i <= n; ++i ){
105         int u = i;
106         if( vis[i] ){
107             for( int j = fst[u]; j != -1; j = nxt[j] ){
108                 int v = vv[j];
109                 if( mp.find( make_pair(u,v) ) == mp.end() && mp.find( make_pair(v,u) ) == mp.end() ){
110                     if( work[j] ) ans++;
111                     mp[make_pair(u,v)]++;
112                 }
113             }
114         }
115     }
116     cout << ans << endl;
117     mp.clear();
118     for( int i = 1; i <= n; ++i ){
119         int u = i;
120         if( !vis[i] ){
121             for( int j = fst[u]; j != -1; j = nxt[j] ){
122                 int v = vv[j];
123                 if( mp.find( make_pair( u, v ) ) == mp.end() && mp.find( make_pair( v, u ) ) == mp.end() ){
124                     if( work[j] )
125                         printf( "%d %d %d\n", min(u,v),max(u,v), 0 );
126                     mp[make_pair(u,v)]++;
127                 }
128             }
129         }
130         else{
131             for( int j = fst[u]; j != -1; j = nxt[j] ){
132                 int v = vv[j];
133                 if( v == fa[u] ){
134                     if( !work[j] )
135                         printf( "%d %d %d\n", min(u,v),max(u,v), 1 );
136                     mp[make_pair(u,v)]++;
137                 }
138             }
139         }
140     }
141     for( int i = 1; i <= n; ++i ){
142         int u = i;
143         if( vis[i] ){
144             for( int j = fst[u]; j != -1; j = nxt[j] ){
145                 int v = vv[j];
146                 if( mp.find( make_pair(u,v) ) == mp.end() && mp.find( make_pair(v,u) ) == mp.end() ){
147                     if( work[j] )
148                         printf( "%d %d %d\n", min(u,v),max(u,v), 0 );
149                     mp[make_pair(u,v)]++;
150                 }
151             }
152         }
153     }
154     return 0;
155 }
View Code

 

posted @ 2015-01-28 00:33  L__J  阅读(127)  评论(0编辑  收藏  举报