poj 2664

简介:

没有什么特别好的方法优化,最好的就是把结果存储在数组里面,通过数组下标索引,用空间换时间

直接上代码:

#include <iostream>
using namespace std;

int choice[102];  //Freddie's choice

int main()
{
	int k,m,c,r;
	int flag; //标记是否合格,必须存在,即使提前知道不合格,也需要把输入完毕
	int tempCourse; //保存每次必修输入课程号
	while (scanf("%d",&k)!=EOF)
	{
		if(0 == k)
			break;
		flag=1;
		cin>>m;

		for (int i=0;i<k;++i)
			cin>>choice[i];

		for (int i=0;i<m;++i)
		{
			cin>>c>>r;
			int count=0; //计数看看最后等不等于r
			for (int j=0;j<c;++j)
			{
				cin>>tempCourse;
				if (0 == flag)
					continue;
				
				//遍历学过的课程
				for (int it=0;it<k;++it)
				{
					if( choice[it]==tempCourse ){
						++count;
						break;
					}
				}
			}
			if(count<r) //代表已经不合格了
				flag=0;
		}
		if(flag == 0)
			cout<<"no"<<endl;

		else
			cout<<"yes"<<endl;
	}

	return 0;
}


空间换时间:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define maxn 10000

int k, m;
bool sel[maxn];

void input()
{
    scanf("%d", &m);
    memset(sel, 0, sizeof(sel));
    for (int i = 0; i < k; i++)
    {
        int a;
        scanf("%d", &a);
        sel[a] = true;
    }
    bool ok = true;
    for (int i = 0; i < m; i++)
    {
        int c, r;
        scanf("%d%d", &c, &r);
        int temp = 0;
        for (int i = 0; i < c; i++)
        {
            int a;
            scanf("%d", &a);
            if (sel[a])
                temp++;
        }
        if (temp < r)
            ok = false;
    }
    if (ok)
        printf("yes\n");
    else
        printf("no\n");
}

int main()
{
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &k), k)
    {
        input();
    }
    return 0;
}




相关文章
|
8月前
|
容器
POJ 3640 Conformity
POJ 3640 Conformity
40 0
poj 3620
题意:给出一个矩阵,其中有些格子干燥、有些潮湿。       如果一个潮湿的格子的相邻的四个方向有格子也是潮湿的,那么它们就可以构成更大       的湖泊,求最大的湖泊。       也就是求出最大的连在一块儿的潮湿的格子的数目。
553 0
|
人工智能 BI
|
JavaScript
|
并行计算 网络架构
poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned ...
938 0
|
机器学习/深度学习
|
机器学习/深度学习