1596 搬货物

思路:想了好久都想不到怎么居然是直接模仿二进制进位,最后数下有多少个1即可,真是神奇!

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #define x first
16 #define y second
17 #define pb push_back
18 #define mp make_pair
19 #define lson l,m,rt*2
20 #define rson m+1,r,rt*2+1
21 #define mt(A,B) memset(A,B,sizeof(A))
22 #define mod 1000000007
23 using namespace std;
24 typedef long long LL;
25 const int N=2000000+10;
26 const LL INF=0x3f3f3f3f3f3f3f3fLL;
27 LL val[N],MAX=-INF,p,ans=0;
28 LL Scan()//输入外挂
29 {
30     LL res = 0, ch, flag = 0;
31     if((ch = getchar()) == '-')
32         flag = 1;
33     else if(ch >= '0' && ch <= '9')
34         res = ch - '0';
35     while((ch = getchar()) >= '0' && ch <= '9' )
36         res = res * 10 + ch - '0';
37 
38     return flag ? -res : res;
39 }
40 int main()//直接进位即可
41 {
42 #ifdef Local
43     freopen("data.txt","r",stdin);
44 #endif
45      int n;
46      cin>>n;
47      mt(val,0);
48      for(int i=0;i<n;i++)
49      {
50          p=Scan();
51          MAX=max(MAX,p);
52          val[p]++;
53      }
54      for(int i=0;i<MAX+100;i++)
55      {
56          if(val[i]>1)
57          {
58              val[i+1]+=val[i]/2;
59              val[i]%=2;
60          }
61          ans+=val[i];
62      }
63      cout<<ans<<endl;
64 
65 }
View Code

 

posted @ 2017-01-09 12:50  Kcl886  阅读(228)  评论(0编辑  收藏  举报