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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值