cf312div2题解

http://codeforces.com/contests/558

我真的是一个动不动就会用map的蒟蒻,这次才做了三题,第三题pretest还挂了,改好的没提交上去,(这是我第二次参加cf的比赛吧……)

 

A:Lala Land and Apple Trees

大意:初始位置x=0,一开始可以选择向左或向右走,走到一个没来过的树摘苹果,然后反向走,直到方向上没有苹果树。

看起来还比较简单,就是个模拟…… 4*O(n)

真不明白为什么我O(n)的算法还要分类讨论[而且n<=100],其实没什么优化的必要,sort+lower_bound是刚需,其他的其实都差不多……

 1 /* 
 2 * @Author: robertking
 3 * @Date:   2015-07-14 21:20:15
 4 * @Last Modified by:   robertking
 5 * @Last Modified time: 2015-07-14 23:35:26
 6 */
 7 
 8 #include <iostream>
 9 #include <cstdio>
10 #include <algorithm>
11 #include <cmath>
12 
13 using namespace std;
14 
15 struct tree_t
16 {
17     int x, a;
18     bool operator< (const tree_t & a) const
19     {
20         return x < a.x;
21     }
22 } tree[108];
23 
24 inline int count(int begin, int end)
25 {
26     int ret=0;
27     for(int i=begin; i<end; ++i) {
28         ret+=tree[i].a;
29     }
30     return ret;
31 }
32 
33 int main(int argc, char const *argv[])
34 {
35     // freopen("in", "r", stdin);
36     int n;
37     cin >> n;
38     for(int i=0; i<n; ++i) {
39         cin >> tree[i].x >> tree[i].a;
40     }
41     sort(tree, tree+n);
42     //    calc pos of x=0
43     int pos;
44     {
45         tree_t x;
46         x.x = x.a = 0;
47         pos=lower_bound(tree, tree+n, x) - tree;
48     }
49     int ans=0;
50     if(abs(n-2*pos) <= 1)
51         ans=count(0, n);
52     else if(2*pos < n)
53         ans=count(0, 2*pos+1);
54     else if(2*pos > n)
55         ans=count(2*pos-n-1, n);
56     else
57         { ; }
58 
59     cout << ans << endl;
60 
61     return 0;
62 }

 

B:Amr and The Large Array

大意就是找到一组数中出现最多的数,跨度有最小,输出开始结束为止,输出一组即可。

n<=10^6, ai<=10^6

就是喜欢用map,就是不想写hash……

其实就是懒……

不过值得一提的是……终于搞明白了map::iterator原来是个pair<*, *>,[LLVM-Clang的错误提示太给力了好吗!超g++ INT_MAX倍!],所以正常用法是it->second.abc

 1 /* 
 2 * @Author: robertking
 3 * @Date:   2015-07-14 21:20:15
 4 * @Last Modified by:   robertking
 5 * @Last Modified time: 2015-07-14 23:58:45
 6 */
 7 
 8 #include <iostream>
 9 #include <cstdio>
10 #include <map>
11 
12 using namespace std;
13 
14 struct number
15 {
16     int l, r, t;
17     number()
18     {
19         l=99999;
20         r=0;
21         t=0;
22     }
23     void update(int pos)
24     {
25         t++;
26         l=min(l, pos);
27         r=max(r, pos);
28     }
29 };
30 
31 map<int, number> a;
32 
33 int main(int argc, char const *argv[])
34 {
35     // freopen("in", "r", stdin);
36     int n;
37     cin >> n;
38     int x;
39     for(int i=0; i<n; ++i) {
40         cin >> x;
41         a[x].update(i);
42     }
43     int mini=99999;
44     int l, r;
45     int maxi=0;
46     for(map<int, number>::iterator i = a.begin(); i != a.end(); ++i) {
47         number *it = &(i->second);
48         // cout << it->t << ' ' << it->l << ' ' << it->r << endl;
49         if( (it->t > maxi) || ( (it->t == maxi) && ( (it->r - it->l) < mini) ) ) {
50             maxi = it->t;
51             mini = it->r - it->l;
52             l = it->l;
53             r = it->r;
54         }
55     }
56 
57     cout << l+1 << ' ' << r+1 << endl;
58 
59     return 0;
60 }

 

C:Amr and Chemistry

大意:有n个试管里,每个里面有ai升试剂,要求把所有体积都变成一样的,每一步可以有2种做法:a*=2或a/=2。

n<=10^5, a<=10^5

暴力咯,map咯,[[我是个]蒟蒻咯]……

然后就坑爹了,之前我没有用step_i,结果样例答案出来最小68……,找了半天…………

更kd的是……之前我把每个map<int, possible_l>里的existed都写成map<i, bool>,真是沙掉了,更沙的是我把exist拿到外面来写成map然后每次都clear()……,最后比赛结束,我才慢慢改完程序,已经来不及提交了

……现在这个还是TLE的……,内存倒是从73296K降到1200K了,有空想想算法

 1 /* 
 2 * @Author: robertking
 3 * @Date:   2015-07-14 21:20:15
 4 * @Last Modified by:   robertking
 5 * @Last Modified time: 2015-07-15 01:16:58
 6 */
 7 
 8 #include <iostream>
 9 #include <cstdio>
10 #include <map>
11 #include <climits>
12 
13 using namespace std;
14 
15 struct possible_l
16 {
17     bool existed;
18     int step_count;
19     int chem_count;
20 };
21 
22 //    possible_l -> possible_l_t
23 map<int, possible_l> a;
24 
25 int main(int argc, char const *argv[])
26 {
27     // freopen("in", "r", stdin);
28     int n;
29     cin >> n;
30     int x;
31     for(int i=0; i<n; ++i) {
32         cin >> x;
33         for(map<int, possible_l>::iterator i = a.begin(); i != a.end(); ++i) {
34             i->second.existed=false;
35         }
36         for(int step_i=0; x>0; x/=2, ++step_i) {
37             for(int p=x, steps=step_i; p<=100000; p*=2, ++steps) {
38                 // cout << "p "<< p << " step "<<steps <<endl;
39                 if(a[p].existed==false) {
40                     a[p].existed=true;
41                     a[p].chem_count++;
42                     a[p].step_count+=steps;
43                 }
44             }
45         }
46     }
47     int mini=INT_MAX;
48     for(map<int, possible_l>::iterator i = a.begin(); i != a.end(); ++i) {
49         // cout << "ord " << i->first << " count "     << i->second.chem_count << " step " << i->second.step_count << endl;
50         if(i->second.chem_count == n) {
51             mini=min(mini, i->second.step_count);
52         }
53     }
54     cout << mini << endl;
55     return 0;
56 }

 

排名1007/3668,rating还没算

第三题应该dp,第四题是树,第五题字符串

2015.7.15 凌晨2:01----另外两道题还没看,太晚了,有空再写。

 

转载于:https://www.cnblogs.com/robertking/p/4647056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值