Codeforces_582_D1_暴力枚举

题目链接:https://codeforces.com/contest/1213/problem/D1

解题思路:
刚开始拿到这道题目的时候,我的第一想法是通过直接枚举 以数组a中的每一个数为基准点;后来这个会在第5个样例wa掉,后来我发现,有可能最少的操作次数所对应的数字是 除了a数组中的数 以外的数,考虑道时间复杂度,又看到题目中ai <= 2e5,所以我的枚举方式是: 枚举 每一个位 。

代码:

#include <iostream>
#include <cstdio>
#include <cstdio>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;

int n, k, maxx;
struct Cnt {int sum, fi; }cnt[70][200005];
struct Node {int x, bit; }a[55];

inline bool cmp(Node x, Node y) {
	if(x.bit == y.bit) return x.x < y.x;
	return x.bit < y.bit;
}

int main(void) {
//	freopen("in.txt", "r", stdin);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; i ++) {
		scanf("%d", &a[i].x);
		int tmp = a[i].x, permt = 0;
		while(tmp) {
			tmp >>= 1;
			permt ++;
		}

		a[i].bit = permt;
		maxx = max(maxx, a[i].bit);
	}

	sort(a + 1, a + 1 + n, cmp);

	int fi = inf;
	for(int i = 0; i <= maxx; i ++) {

		for(int j = 1; j <= n; j ++) {

			if(a[j].bit == i) {
				if(cnt[i][a[j].x].sum == k) {
					fi = min(fi, cnt[i][a[j].x].fi);
					continue;
				}
				else {
					cnt[i][a[j].x].sum ++;
					if(cnt[i][a[j].x].sum == k) fi = min(fi, cnt[i][a[j].x].fi);
				}
			}
			else if(a[j].bit > i) {
				int tmp = a[j].bit - i;
				if(cnt[i][a[j].x >> tmp].sum == k) {
					fi = min(fi, cnt[i][a[j].x >> tmp].fi);
					continue;
				}
				else {
					cnt[i][a[j].x >> (tmp)].sum ++, cnt[i][a[j].x >> tmp].fi += tmp;
					if(cnt[i][a[j].x >> tmp].sum == k) fi = min(fi, cnt[i][a[j].x >> tmp].fi);
				}
			}
		}
	}
	
	printf("%d\n", fi);
	
	
//	fclose(stdin);
	return 0;
}

总结:通过这道题目,我发现自己读题能力还有待提高,另外思维也还需要继续锻炼,要不然这道题目应该在比赛的时候是可以过了,但由于当时时间不够,刚写完第一种思交上去就wa了;另外还要注意代码的细节,当我把第二种思路实现后发现还是没过,检查了2遍发现有2个地方少写了一点东西。。。

发布了136 篇原创文章 · 获赞 0 · 访问量 2992

猜你喜欢

转载自blog.csdn.net/weixin_42596275/article/details/102066778