Codeforces Round #515 (Div. 3)

题目链接:D - Boxes Packing

题目大意:说了一堆,其实就是给你n个物品,每个物品重量w[i],m个箱子,每个箱子的体积是k。现在以这种算法开始装:能装就装下,不能就换箱子。问你一个最小的l,使能装下[l, n]这个区间的物品。输出最多的物品的数量。

解题思路:**题,实际应用哪个**会把简单的说这么复杂,欺负非英文母语选手?被题意安排的明明白白,直接倒序模拟即可。

Ac Code:

#pragma GCC diagnostic error "-std=c++11"
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define lowbit(x) x&(-x)
#define PII  pair<int, int> 
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
using namespace std;
using __gnu_cxx::crope;

int n, m, k;
int w[maxn];

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 1; i <= n; i++) scanf("%d", w + i);
	ll t = 0;
	int cnt = 1;
	for(int i = n; i >= 1; i--){
		if(t + w[i] > k){
			if(cnt == m){
				return 0 * printf("%d\n", n - i);
			}
			t = 0;
			cnt++;
		}
		t += w[i];
	}
	printf("%d\n", n);
	return 0;
}

官方给出的解法是二分,好久没写二分了,练练手:

#pragma GCC diagnostic error "-std=c++11"
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define lowbit(x) x&(-x)
#define PII  pair<int, int> 
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
using namespace std;
using __gnu_cxx::crope;

int w[maxn];
int n, m, k;

bool check(int l){
	int cnt = 1;
	int t = 0;
	for(int i = l; i <= n; i++){
		if(t + w[i] > k){
			if(cnt == m){
				return false;
			}
			t = 0, cnt++;
		}
		t += w[i];
	}
	return true;
}

int main()
{
	scanf("%d %d %d", &n, &m ,&k);
	for(int i = 1; i <= n; i++) scanf("%d", w + i);
	int l = 1, r = n;
	while(l < r){
		int mid = (l + r) >> 1;
		if(check(mid)){
			r = mid;
		}
		else l = mid + 1;
	}
	printf("%d\n", n - l + 1);
	return 0;
}

E - Binary Numbers AND Sum

不想说,很容易想到前缀和

注意开ll就行了 

Ac Code:

#pragma GCC diagnostic error "-std=c++11"
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define lowbit(x) x&(-x)
#define PII  pair<int, int> 
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)998244353;
const int maxn = (int)2e5 + 5;
using namespace std;
using __gnu_cxx::crope;

#define int ll

int a[maxn], b[maxn];
int sum[maxn];

int32_t main()
{
	int n, m; scanf("%lld %lld", &n, &m);
	for(int i = 1; i <= n; i++) scanf("%1lld", &a[i]);
	for(int i = 1; i <= m; i++) scanf("%1lld", &b[i]);
	for(int i = 1; i <= m; i++){
		sum[i] = sum[i-1] + b[i];
	}
	int ans = 0;
	int cur = 1;
	for(int i = n; i >= 1; i--){
		if(a[i]) ans = (ans + (sum[max(0ll, m - n + i)] * cur % mod)) % mod;
		cur = (2 * cur) % mod;
	}
	printf("%lld\n", ans);
	return 0;
}

over

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值