AtCoder Beginner Contest 045補題

AtCoder Beginner Contest 045補題

C

題目

在這裏插入圖片描述

題目大意

給一個字符串 往中間加入+號,得出結果之後相加

思路

陷入了盲區,之前一直在想,怎麼分數字,卻沒想過,我可從插入符號入手,狀壓枚舉+號存在位置即可
通過這題,明白了從多個角度去思考問題

代碼

#include <iostream>
#include <cstdio>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
//#include <unordered_map>
using namespace std;
#define dbg(x) cerr << #x " = " << x << endl;
typedef pair<int, int> P;
typedef long long ll;

int main()
{
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string op;
    cin >> op;
    int n = op.size();
    int stat = (1 << (n - 1)) - 1;
    ll ans = 0;
    for(int i = 0; i <= stat; i++)
    {
        vector<int> v;
        int tmp = 1;
        int cnt = 0;
        while(tmp  <= i)
        {
            if(tmp & i)
            {
                v.push_back(cnt);
            }
            cnt++;
            tmp <<= 1;
        }
        
        v.push_back(n-1);
        /*for(int j = 0; j < v.size(); j++)
        {
            cout << v[j] << ' ';
        }
        cout << endl;*/
        ll num = 0;
        for(int j = 0; j < v.size(); j++)
        {
            ll tmp = 0;
            //dbg(v[j]);
            if(!j)
            {
                for(int k = 0; k <= v[j]; k++)
                {
                    tmp = tmp * 10 + op[k] - '0';
                    //dbg(op[k] - '0');
                }
            }
            else
            {
                for(int k = v[j-1]+1; k <= v[j]; k++)
                {
                    tmp = tmp * 10 + op[k] - '0';
                }
            }
            num += tmp;
            //dbg(tmp);
        }
        ans += num;
    }
    cout << ans << endl;
}

D

題目

在這裏插入圖片描述

思路

黑塊的數據範圍比較小,從黑塊入手,對於每一個黑塊,9次查找位置的貢獻,然後計算就行了

代碼

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9 + 7;
const int N = 1000000 + 5;
const int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
using namespace std;
LL bucket[10];
pair<int, int> node[N];
map<pair<int, int>, int> mp;
int main()
{
    LL h, w, n;
    scanf("%lld%lld%lld", &h, &w, &n);
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        scanf("%d%d", &node[i].first, &node[i].second);
        mp[node[i]] = 1; //塗黑的格子貢獻度爲1
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < 8; j++)
        { //統計塗黑的格子周圍格子的貢獻度
            int nx = node[i].first + dx[j];
            int ny = node[i].second + dy[j];
            if (nx >= 1 && nx <= h && ny >= 1 & ny <= w)
            { //越界判斷
                pair<int, int> temp(nx, ny);
                mp[temp]++;
            }
        }
    }

    LL sum = 0;
    LL tot = (h - 2) * (w - 2); //總共的矩形數
    map<pair<int, int>, int>::iterator it;
    for (it = mp.begin(); it != mp.end(); it++)
    { //枚舉所有涉及到的格子
        int x = it->first.first;
        int y = it->first.second;
        int val = it->second;
        if (x >= 2 && x <= h - 1 && y >= 2 & y <= w - 1)
        { //統計除去最外一圈的格子
            bucket[val]++;
            sum++;
        }
    }
    bucket[0] = tot - sum; //爲0的數量是總共的矩形減去所有非零的矩形

    for (int i = 0; i <= 9; i++)
        printf("%lld\n", bucket[i]);

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章