热身赛 J - Automatic Control Machine Gym - 102460J

原题

Automatic Control Machine
Time limit: 2 seconds
Memory limit: 1024 megabytes
Problem Description
The company has produced an Automatic Control Machine (ACM for short) that is very popular. Due to its complete and powerful features, the company is preparing to redesign after
years of sales. The new version of the ACM is still subject to a number of tests to determine
the reliability of the product before it goes on the market. Because there are so many features,
each test dataset can only detect several of them. Of course, the product must be available
after all features have been tested. Since each test has time and material costs, they like to
do the test as less as possible. Assume that running each test dataset costs the same, your
job is finding the minimum number of test datasets that can cover the test of all features. For
example, if there are 5 features that need to be tested, and there are 6 test datasets each can
cover the features as follows:
• Test dataset a: 1
• Test dataset b: 2, 5
• Test dataset c: 2, 3, 4
• Test dataset d: 1, 3, 5
• Test dataset e: 1, 3, 4
• Test dataset f: 3, 5
Although {a, b, c} may do the job, but {c, d} will do the job better in the way of saving time
and money.
Input Format
The first line of the input file contains one positive integer T representing the number of
machines. For each machine, the first line consists of two integers n and m representing the
features of machine that has to be tested and the number of test datasets. It follows by m
lines, each line has a binary string of length n, showing whether the features can be detected
by the test dataset or not (1 means yes, 0 means no).
Output Format
Output T lines. Each of them should be the minimum number of test dataset needed to test
all features for that machine. If it is not possible to test all functions for the machine, output
-1.
19
2019 ICPC Asia Taipei-Hsinchu Regional
Technical Specification
• The number of machines 0 < T ≤ 10
• The number of functions to be tested 0 < n ≤ 500
• The number of test data 0 < m ≤ 15

解题思路

暴力枚举每一种组合,然后判断是否合法,选出合法的最小的数量

AC代码

//来自队员的写法

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
int eps(int nn)

{
	int anss=1;
	for(int i=1;i<=nn;i++) anss*=2;
	return anss;
}

int main()
{
    int ans,n,m;
    char ss[505][505],temp[505][505];
	int t;
	cin>>t;
	while(t--){
		cin>>n>>m;int minn=1000;
		for(int i=0;i<m;i++)
		scanf("%s",ss[i]);
	//	cout<<"s"<<endl;
		for(int i=1;i<eps(m);i++)
	{   int p=0,numm=0;
		for(int j=0;j<m;j++)
		if(((i>>j)&1)==1)  /*{
			for(int mm=0;mm<n;mm++)
			temp[p][mm]=ss[j][mm];
			p++;
		}//*/
		strcpy(temp[p++],ss[j]);
		
			
		/*	for(int jj=0;jj<p;jj++) 
			printf("%d %s\n",jj,temp[jj]);
			printf("\n");*/
			for(int x=0;x<n;x++)
			{    
			 		for(int o=0;o<p;o++)
		     {
		    if(temp[o][x]=='1')  {
		    	numm++;break;
			}
		     }  
			} 
		 if(numm==n)  minn=min(minn,p);
	}
	if(minn!=1000) cout<<minn<<endl;
	else cout<<"-1"<<endl;
	} 
	
	
	return 0;
}

//利用bitset的写法,更简洁
#include <bits/stdc++.h>
using namespace std;
bitset<500> number[30];
char str[1001];
int main(){
    int T;
    scanf("%d",&T);
    while (T--){
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i=0;i<m;++i){
            scanf("%s",str);
            number[i]=bitset<500>(str);
        }
        int len=1<<m;
        int ans=m+1;
        
        for (int i=1;i<len;++i){
            int t=i;
            int s=0;
            bitset<500> now(0);
            for (int j=0;j<m && t>0;++j){
                if (t&1){ now=now|number[j]; s++;}
                t>>=1;
            }
            if (now.count()==n) ans=min(ans,s);
        }
        if (ans==m+1) printf("-1\n");
        else printf("%d\n",ans);
    }
}

bitset的用法

附上一个大佬的整理博客
https://blog.csdn.net/weixin_43792276/article/details/96831163?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.nonecase
来自大佬的整理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值