BestCoder Round #92

  现场赛就出了一题= =。

  A题,水题。但是几天没写代码有点手生,调试了一会才A= =。

  B题,考虑到只要连续的四个即可,那么枚举中间的两个即可。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <vector>
 5 using namespace std;
 6 typedef long long ll;
 7 const int N = 100000 + 50;
 8
 9 vector<int> B[N];
10 int C[N];
11 int n,m,k;
12 void init()
13 {
14     memset(C,0,sizeof(C));
15     for(int i=1;i<=m;i++) B[i].clear();
16 }
17
18 int main()
19 {
20     int T;
21     scanf("%d",&T);
22     while(T--)
23     {
24         scanf("%d%d%d",&n,&m,&k);
25         init();
26         for(int i=1;i<=k;i++)
27         {
28             int u,v;
29             scanf("%d%d",&u,&v);
30             B[v].push_back(u);
31             C[u]++;
32         }
33         ll ans = 0;
34         for(int b=1;b<=m;b++)
35         {
36             if(B[b].size() < 2) continue;
37             for(int i=0;i<B[b].size();i++)
38             {
39                 int c = B[b][i];
40                 if(C[c] < 2) continue;
41                 int a_can = B[b].size() - 1;
42                 int d_can = C[c] - 1;
43                 ans += 1LL * a_can * d_can;
44             }
45         }
46         ans *= 2;
47         printf("%I64d\n",ans);
48     }
49     return 0;
50 }

B

  C题,看数据挺小的,但是想不出什么好方法- -,题解是记忆化搜索。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 const int N = 100 + 5;
 6
 7 int n,m,cnt,kase;
 8 int dp[N][N][N];
 9 int vis[N][N][N];
10 char s[N];
11 int pos[N];
12 void update(int &a,int b) {if(a < b) a = b;}
13 int dfs(int k,int last_pos,int iq)
14 {
15     if(k > cnt) return n - last_pos >= 2;
16     if(vis[k][last_pos][iq] == kase) return dp[k][last_pos][iq];
17     vis[k][last_pos][iq] = kase;
18     dp[k][last_pos][iq] = -2e9;
19     int L = max(last_pos + 1, pos[k] - iq);
20     int R = min(n, pos[k] + iq);
21     for(int i=L;i<=R;i++)
22     {
23         int cost = std::abs(i - pos[k]);
24         update(dp[k][last_pos][iq], dfs(k+1,i,iq-cost) + (i-last_pos>=3) * (k > 1));
25     }
26     return dp[k][last_pos][iq];
27 }
28
29 int main()
30 {
31     int T;
32     scanf("%d",&T);
33     kase = 0;
34     while(T--)
35     {
36         kase++;
37         scanf("%d%d",&n,&m);
38         scanf("%s",s+1);
39         m >>= 1;
40         cnt = 0;
41         for(int i=1;i<=n;i++) if(s[i] == ‘2‘) pos[++cnt] = i;
42         if(cnt == 0) {puts("0"); continue;}
43         printf("%d\n",dfs(1,0,m));
44     }
45     return 0;
46 }

C

时间: 2024-10-30 04:14:05

BestCoder Round #92的相关文章

从lca到树链剖分 bestcoder round#45 1003

bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或),高手就不这么做了,直接树链剖分.为什么不能用lca,因为如果有树退化成链,那么每次询问的复杂度是O(n), 那么q次询问的时间复杂度是O(qn) 什么是树链剖分呢? 就是把树的边分成轻链和重链 http://blogsina.com.cn/s/blog_6974c8b20100zc61.htmlh

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

BestCoder Round #1 1001 &amp;&amp; 1002 hdu 4857 4858

hdu 4857 逃生 第一题是拓扑排序,不是按照字典序最小输出,而是要使较小的数排在最前面..赛后弄了好久,才比较明白,我一直以为 反向建图,i从1到n,开始深搜dfs( i ),对i点的边,由小到大继续搜一下,同时标记搜过的数,搜过之后就不再搜,搜到底之后ans[cnt++] = u;这样顺序输出就是答案,后来经过超哥指点,才明白深搜贪心是错的.只有 反向建图,用优先队列把较大的数尽量排在前面,然后反序输出才是正解.. 1 #include<iostream> 2 #include<

BestCoder Round #91

传送门:http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+Round+%2391&source=1&searchmode=source A题:给你n种字母,每种字母有个权值vali,共cnti个,现在让你在里面挑出任意数量的字符,组合成一个字符串,该字符串的权值的计算方式为val1*1+val2*2+--+valn*n,让你输出字符串最大的权值是多少.这题很容易会有一个错误的贪心,就是把val为负的舍去,然后v

BestCoder Round #65 (ZYB&#39;s Game)

ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description ZYBZYBZYB played a game named NumberBombNumber BombNumberBomb with his classmates in hiking:a host keeps a

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

Problem Description After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to

hdu 5163 Taking Bus (BestCoder Round #27)

Taking Bus                                                               Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 501    Accepted Submission(s): 203 Problem Description Bestland has a v