2020 百度之星 初赛二 1004 Car

Problem - 6778
题目:给你几个车牌,然后根据车牌尾号在一周的工作日内限行,一种尾号一周只能限一次,问你一天最多有多少辆车。

思路:一开始以为是贪心和最小生成树,写到后面wa了,到后面发现是暴力dfs,吐血 1e5的数据,暴力能过

原代码,不知道问题在哪:

#include<bits/stdc++.h>
using namespace std;

int turnNum(char a) {
	return a-'0';
}

int main() {
	int o;
	scanf("%d",&o);
	while(o--) {
		int n;
		scanf("%d",&n);
		getchar();
		char a[10];
		int ans[12];
		memset(ans,0,sizeof(ans));
		priority_queue<int> q;
		for(int i=0; i<n; i++) {
			gets(a);
			ans[turnNum(a[4])]--;
		}
		int k = 0;
		for(int i=0; i<10; i++) {
			if(ans[i]!=0) {
				q.push(ans[i]);
//				printf(" %d ",ans[i]);
				k++;
			}
		}
//		printf("K:%d--|%d|--\n",k,q.top());
		if(k<5) {
			printf("%d\n",n);
		} else {
			while (!q.empty()&&k>5) {
//				printf("--|%d||\n",k); 
				int fis = q.top();
				q.pop();
				if (!q.empty()) {
					int sec = q.top();
					q.pop();
					q.push(fis+sec);
				}
				k--;
//				printf("-|%d||\n",k);
			}
			int sum = q.top();
			printf("%d\n",n+sum);
		}

	}
	return 0;
}

然后的暴力的代码(看别人能过,但是发现还是TLE,需要二分) :

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T>
void read(T &x)
{
    int s = 0, c = getchar();
    x = 0;
    while (isspace(c))
        c = getchar();
    if (c == 45)
        s = 1, c = getchar();
    while (isdigit(c))
        x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    if (s)
        x = -x;
}

template <typename T>
void write(T x, char c = ' ')
{
    int b[40], l = 0;
    if (x < 0)
        putchar(45), x = -x;
    while (x > 0)
        b[l++] = x % 10, x /= 10;
    if (!l)
        putchar(48);
    while (l)
        putchar(b[--l] | 48);
    putchar(c);
}

void DFS(int tot, int cnt[], int sum[], int &ans, int n)
{
    if (tot == 10)
    {
        int qwq = 1e9;
        for (int i = 0; i < 5; ++i)
        {
            qwq = min(qwq, sum[i]);
        }
        ans = min(ans, n - qwq);
        return;
    }
    for (int i = 0; i < 5; ++i)
    {
        sum[i] += cnt[tot];
        DFS(tot + 1, cnt, sum, ans, n);
        sum[i] -= cnt[tot];
    }
}

int main(void)
{
    int kase;
    read(kase);
    for (int ii = 1; ii <= kase; ii++)
    {
        int n;
        read(n);
        int cnt[10] = {0};
        for (int x, i = 1; i <= n; ++i)
        {
            read(x);
            cnt[x % 10]++;
        }
        int ans = 1e9 + 7;
        int sum[5] = {0};
        DFS(0, cnt, sum, ans, n);
        write(ans, '\n');
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值