Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)

 C. The Phone Number

题解:

把n分成m块,块内递减,块间递增,这样和就是(m + (n / m)向上取整),问题转换为求m == ? 这个式子最小。

答案是m == sqrt(n),能感觉到,是基本不等式还是啥。。

传说中的瞎搞题吧,想不到分块

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#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 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)1e5 + 5;
using namespace std;

int main()
{
	int n; cin >> n;
	int m = sqrt(n);
	int cnt = 0;
	for(int i = 1; i <= n; i += m){
		if(i + m > n) for(int j = n; j >= i; j--) cout << j << ' ';
		else for(int j = m + i - 1; j >= i; j--) cout << j << ' ';
	}
	cout << endl;
	return 0;
}

 

D. The Wu

所有01串的长度都是n,第i位的价值是wi,一个多重集合里面有m个01串,现在有q个询问,每个询问给一个01串和一个价值w,求这个串和多重集合里面的串匹配价值不超过wi的有多少个

匹配价值计算方法:对于每一位,相同就加该位的wi

数据范围: 1 <= n <= 12, 1 <= q, m <= 5e5

n很小,m,q比较大。首先想到预处理,可以枚举出每一个可能的01串,状态压缩成相应的二进制数,预处理出来任意两个01串的价值(当然,没出现过的就不用了,这也算是剪枝吧),询问的时候直接查询即可

考虑如何预处理:先对01串进行状态压缩,用数组cnt来计数,用一个二维数组sum记录价值,比如sum[i][j]就表示询问中i(状态压缩后)和多重数组中匹配价值为j的个数,举例sum[1][2] = 3就表示1与多重数组中的01串匹配价值为2的串有3个(随便举的一个)

考虑如何查询:对于一个给定的max_w,一层循环枚举1到max_w,把个数加起来即可

考虑时间复杂度:预处理cnt:m * n,不谈

预处理sum:2 ^12 * 2 ^ 12 * 12= 2 ^ 24 * 12 ~= 2e8,当然,通过剪枝没有这么高&&题目给了两秒&&不会有这么丧心病狂的数据(应该?)&&2e8极限也能过&&……

询问:q * (n + k)不谈

所以时间复杂度小于2e8,其实也挺暴力的,只不过用了状压+预处理优化了一些,应该有更优秀的解法,不谈。xixixi

AC Code:

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#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 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)1e5 + 5;
using namespace std;

int w[15];
int cnt[1 << 12], sum[1 << 12][105];
char s[15];

int main()
{
	int n, m, q; scanf("%d %d %d", &n, &m, &q);
	for(int i = 1; i <= n; i++) scanf("%d", w + i);
	for(int i = 1; i <= m; i++){
		scanf("%s", s);
		int t = 0;
		for(int j = 0; j < n; j++){
			if(s[j] == '1') t += 1 << (n - j - 1);
		}
		cnt[t]++;
	}
	for(int i = 0; i < (1 << 12); i++){
		for(int j = 0; j < (1 << 12); j++){
			if(!cnt[j]) continue;
			int t = 0;
			for(int k = 0; k < n; k++){
				if((i & (1 << k)) == (j & (1 << k))) t += w[n-k];
			}
			if(t <= 100) sum[i][t] += cnt[j];
		}
	}
	while(q--){
		int k; scanf("%s %d", s, &k);
		int t = 0;
		for(int i = 0; i < n; i++){
			if(s[i] == '1') t += 1 << (n - i - 1);
		}
		int ans = 0;
		for(int i = 0; i <= k; i++){
			ans += sum[t][i];
		}
		printf("%d\n", ans);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值