Skip to main content
  1. Posts/

codeforces 558c Amr and Chemistry (贪心)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/558/problem/C

题目大意是说,给定N个数,可以对任意数进行任意次两种操作,×2,和/2(整除)

问最少操作多少次,可以让所有数相等。

嘛,前半个小时A掉了前两个提,d E貌似都是线段树。。并不会。。。就一直搞C。。。。

然而并没有做出来。位运算是什么神奇的黑魔法。

转载一份题解:http://blog.csdn.net/qq_24451605/article/details/46906813

思路是声明两个数组,一个数组表示到达i的步数,另一个数组表示n个数中能够达到i的数的个数(包括本身)

 1
 2    /*************************************************************************
 3      > File Name: code/#312C.cpp
 4      > Author: 111qqz
 5      > Email: rkz2013@126.com
 6      > Created Time: Mon 20 Jul 2015 11:36:50 PM CST
 7     ************************************************************************/
 8
 9    #include<iostream>
10    #include<iomanip>
11    #include<cstdio>
12    #include<algorithm>
13    #include<cmath>
14    #include<cstring>
15    #include<string>
16    #include<map>
17    #include<set>
18    #include<queue>
19    #include<vector>
20    #include<stack>
21    using namespace std;
22    #define REP(i, n) for (int i=0;i<int(n);++i)
23    typedef long long LL;
24    typedef unsigned long long ULL;
25    const int N= 1E5+7;
26    const int inf = 0x7fffffff;
27    int a[N];
28    int  vis[N],step[N];
29    bool cmp (int a,int b)
30    {
31        if (a>b) return true;
32        return false;
33    }
34    int main()
35    {
36        int n;
37        cin>>n;
38        int mx = -1;
39        memset(vis,0,sizeof(vis));
40        memset(step,0,sizeof(step));
41
42        for ( int i = 1 ;  i <= n ; i++ )
43        {
44    	cin>>a[i];
45    	if (a[i]>mx)
46    	    mx = a[i];
47        }
48        for ( int i = 1 ; i <= n ; i++ )f
49        {
50    	int tmp = a[i];
51    	int num = 0;
52    	while (tmp<=mx)
53    	{
54    	    step[tmp]+=num;   //到达tmp需要的操作数是之前的数到达tmo的操作数加上当前
55    	    vis[tmp]++;     //能够到达i的数的个数存在vis数组
56    	    num++;
57    	    tmp = tmp << 1;
58    	}
59    	num = 0;
60    	int tmpa = a[i];
61    	while (tmpa)
62    	{
63    	    if (tmpa&1&&tmpa!=1)    //a[i]&1为真当且仅当a[i]的二进制表示的最后一位是1,即a[i]为奇数
64    	    {               //但是如果a[i]为1,/2为0,就无法变大了。
65    		int tmp = (tmpa>>1)<<1;
66    		int sum = num + 2;  //奇数a[i]变为偶数a[i]-1的需要两步。
67    		while (tmp<=mx) //再从a[i]-1扩展下去,看能达到哪些数。
68    		{
69    		    step[tmp]=step[tmp]+sum;
70    		    vis[tmp]++;
71    		    sum++;
72    		    tmp = tmp << 1;
73    		}
74    	    }
75    	    num++;          //往反方向扩展,看能达到哪些数。
76    	    tmpa = tmpa>>1;     //注意a[i]在之前已经达到过了。
77    	    step[tmpa]+=num;
78    	    vis[tmpa]++;
79    	}
80
81        }
82        int ans = inf;
83        for ( int i = 1 ; i <= mx ; i++ )
84        {
85    	//     cout<<"i:"<<i<<" vis[i]:"<<vis[i]<<"  step[i]:"<<step[i]<<endl;
86    	if (vis[i]==n)
87    	{
88    	    ans = min(ans,step[i]);
89    	}
90        }
91        cout<<ans<<endl;
92
93
94
95        return 0;
96    }

Related

codeforces 548B Mike and Fun

·1 min
http://codeforces.com/problemset/problem/548/B 比赛的时候不懂为什么就没做出来…. 其实很容易想到一个o(q*(n+m))的做法… 就是每次更新,要同时更新当前更新行的最大连续和….O(m)可以完成…然后在O(n)扫一遍,找到所有行中的最大值。 然后需要注意的是,在第一次更改之前就要把每个行的最大值处理出来l.. 然后cf机器真是够快,O(nmq)的1.2S过。。。。

codeforces 479D. Long Jumps

·2 mins
http://codeforces.com/problemset/problem/479/D 题意是说有一把尺子,本身有一些刻度,然后需要测量x和y,问最少需要添加多少个刻度,如果需要,这些刻度分别添加在什么位置。

codeforces 525 B. Pasha and String

·1 min
http://codeforces.com/problemset/problem/525/B 1题意是说一个字符串,进行m次颠倒变换(从a[i]位置到a[l-i+1]位置),问得到的字符串。容易发现,对于越在里边(对称,也就是越靠近中间位置)的字符,调换的次数越多。我们可以把a[i]从小到大排序。然后经过分析发现,把两个相邻的a[i]分为一组,做处理,如果m为奇数,最后还剩下a[m]没有被分组,要单独处理a[m]细节上要注意st数组是从st[0]开始的...好吧的确不方便,适牛也说我了。。数组下标以后还是从0开始吧。。。主要是受高中OI用的pascal的影响。。。那个数组下标随便啊。代码: 2 3 4 5 6 /* *********************************************** 7 Author :111qqz 8 Created Time :2016年02月22日 星期一 23时39分51秒 9 File Name :code/cf/problem/525B.cpp 10 ************************************************ */ 11 12 #include <iostream> 13 #include <algorithm> 14 #include <cstring> 15 #include <cmath> 16 #include <cstdio> 17 18 using namespace std; 19 20 int m,k,len; 21 const int N=2E5+7; 22 int a[N]; 23 char st[N]; 24 25 int main() 26 { 27 cin>>st; 28 scanf("%d",&m); 29 for ( int i = 1 ; i <= m ; i++ ) 30 scanf("%d",&a[i]); 31 sort(a+1,a+m+1); 32 k = 1; 33 len = strlen(st); 34 while (k<=m) 35 { 36 for ( int j = a[k] ; j <= a[k+1]-1 ; j++) 37 swap(st[j-1],st[len-j]); 38 k = k + 2; 39 } 40 if ( m %2==1 ) 41 for ( int i = a[m]; i <= len/2 ; i++ ) 42 swap(st[i-1],st[len-i]); 43 cout<<st<<endl; 44 return 0; 45 }

codeforces 482 A. Diverse Permutation(构造)

·1 min
C - C **Time Limit:**1000MS **Memory Limit:**262144KB 64bit IO Format:%I64d & %I64u Submit Status Description Permutation_p_ is an ordered set of integers _p_1, p_2, …, p__n, consisting of n distinct positive integers not larger than n. We’ll denote as_n the length of permutation _p_1, _p_2, …, p__n.

codeforces 47 C. Exams

·1 min
http://codeforces.com/problemset/problem/479/C 1/************************************************ 2Author :111qqz 3Created Time :2016年02月22日 星期一 23时31分10秒 4File Name :code/cf/problem/479C.cpp 5************************************************ */ 6 7#include <iostream> 8#include <algorithm> 9#include <cstring> 10#include <cstdio> 11 12#include <cmath> 13 14using namespace std; 15int n,ans; 16const int N=1E4+5; 17int a[N],b[N]; 18 19struct Q 20{int a,b; 21}q[N]; 22 23bool cmp(Q x, Q y) 24{ 25 if ( x.a<y.a) return true; 26 if ( x.a==y.a &&x.b<y.b ) return true; 27 return false; 28} 29 30int main() 31{ 32 scanf("%d",&n); 33 for ( int i = 1 ; i <= n ; i++ ) 34 scanf("%d %d",&q[i].a,&q[i].b); 35 sort(q+1,q+n+1,cmp); 36 37 ans=q[1].b; 38 for ( int i = 2 ; i <= n; i++ ) 39 { 40 if ( q[i].b>=ans ) 41 ans = q[i].b; 42 else ans = q[i].a; 43 } 44 printf("%d\n",ans); 45 return 0; 46}