【codeforces题解】CodeCraft-21 and Codeforces Round #711 (Div. 2)

A. GCD Sum
在这里插入图片描述
模拟即可,代码如下

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<unordered_set>
#include<set>
#include<map>
#include<sstream>
using namespace std;

#define int long long
const int INT = 0x3f3f3f3f;

int gcd(int a, int b) {
	return (b == 0) ? a : gcd(b, a % b);
}

int getSum(int n) {
	int res = 0;
	while (n) {
		res += n % 10;
		n /= 10;
	}
	return res;
}

unsigned main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int res = n;
		while (gcd(res, getSum(res))==1) {
			++res;
		}
		cout << res << endl;
	}
}

B. Box Fitting
在这里插入图片描述
在这里插入图片描述
一开始的想法:从大到小,贪心:矩形从大到小排序,逐个加矩形的过程中,对某个矩形,放在满足条件的最下一层,最后统计层数。
结果test 3超时了,原代码如下:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<unordered_set>
#include<set>
#include<map>
#include<sstream>
using namespace std;

#define int long long
const int INT = 0x3f3f3f3f;

bool cmp(int a, int b) {
	return a > b;
}

unsigned main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int t;
	cin >> t;
	while (t--) {
		int n, w;
		cin >> n >> w;
		vector<int>nums;
		for (int i = 0; i < n; ++i) {
			int x; cin >> x;
			nums.push_back(x);
		}
		sort(nums.begin(), nums.end(), cmp);
		vector<int>m(n, w);
		int cnt = 0;
		for (int i = 0; i < n; ++i) {
			int j = 0;
			while (m[j] - nums[i] < 0) {
				++j;
			}
			m[j] -= nums[i];
			cnt = (j < cnt) ? cnt : j;
		}
		cout << cnt+1 << "\n";
	}
}

反思一下:没注意到条件,矩形长度为2的幂

tutial的方法不错,贴一下
在这里插入图片描述

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int t;
    cin >> t;

    while (t--) {
        int n, box_width, w;
        cin >> n >> box_width;

        vector<int> counts(20);
        for (int i = 0; i < n; i++) {
            cin >> w;
            counts[log2(w)]++;
        }

        int height = 1, space_left = box_width;

        for (int iter = 0; iter < n; iter++) {
            int largest = -1;

            for (int size = 19; size >= 0; size--) {
                if (counts[size] and (1 << size) <= space_left) {
                    largest = size;
                    break;
                }
            }

            if (largest == -1) {
                space_left = box_width;
                height++;
                for (int size = 19; size >= 0; size--) {
                    if (counts[size] and (1 << size) <= space_left) {
                        largest = size;
                        break;
                    }
                }
            }

            counts[largest] -= 1;
            space_left -= 1 << largest;
        }

        cout << height << endl;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值