zoukankan      html  css  js  c++  java
  • [AHOI2005]矿藏编码

    嘟嘟嘟

    这道题题面我是看了小半天才懂(太菜了),然后就发现好水啊。

    只要维护一个栈,存的是t,代表当前的正方形是2t * 2t的,然后从头开始扫序列,如果遇到2,就把栈顶元素取出来,然后放进去四个t - 1;如果遇到0,就往结果中加入当前栈顶元素t的2t * 2t.

    此题最大范围是250 * 250,long long也不够,得开double(long long 和 double虽然都只有64位,但因为储存凡是不同,double范围却比long long 大)。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<cctype>
     8 #include<vector>
     9 #include<stack>
    10 #include<queue>
    11 using namespace std;
    12 #define enter puts("")
    13 #define space putchar(' ')
    14 #define Mem(a) memset(a, 0, sizeof(a))
    15 typedef long long ll;
    16 typedef unsigned long long ull;
    17 typedef double db;
    18 const int INF = 0x3f3f3f3f;
    19 const int eps = 1e-8;
    20 const int maxn = 205;
    21 inline ll read()
    22 {
    23     ll ans = 0;
    24     char ch = getchar(), last = ' ';
    25     while(!isdigit(ch)) {last = ch; ch = getchar();}
    26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
    27     if(last == '-') ans = -ans;
    28     return ans;
    29 }
    30 inline void write(ll x)
    31 {
    32     if(x < 0) x = -x, putchar('-');
    33     if(x >= 10) write(x / 10);
    34     putchar(x % 10 + '0');
    35 }
    36 
    37 int k;
    38 char s[maxn];
    39 db ans = 0;
    40 stack<int> st;
    41 db quickpow(db a, int b)
    42 {
    43     db ret = 1;
    44     while(b)
    45     {
    46         if(b & 1) ret *= a;
    47         a *= a; b >>= 1;
    48     }
    49     return ret;
    50 }
    51 
    52 int main()
    53 {
    54     k = read();
    55     scanf("%s", s);
    56     int n = strlen(s);
    57     st.push(k);
    58     for(int i = 0; i < n; ++i)
    59     {
    60         int t = st.top(); st.pop();
    61         if(s[i] == '2') for(int j = 1; j <= 4; ++j) st.push(t - 1);
    62         else if(s[i] == '0') ans += quickpow(2, t) * quickpow(2, t);
    63     }
    64     printf("%.0lf
    ", ans);
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    android SD卡文件的读写
    Android Junit 单元测试步骤
    android xml解析之SAX
    android赚钱 植入有米广告方法
    android广告平台介绍
    读、写其他应用的SharedPreferences
    SQL数据基本操作
    res/raw下的资源文件读写
    容易吗?
    写给实习生的邮件
  • 原文地址:https://www.cnblogs.com/mrclr/p/9553373.html
Copyright © 2011-2022 走看看