B. Cubes for Masha
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Absent-minded Masha got set of n cubes for her birthday.

At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.

To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.

The number can't contain leading zeros. It's not required to use all cubes to build a number.

Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.

Input

In first line integer n is given (1 ≤ n ≤ 3) — the number of cubes, Masha got for her birthday.

Each of next n lines contains 6 integers aij (0 ≤ aij ≤ 9) — number on j-th face of i-th cube.

Output

Print single integer — maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can't make even 1.

Examples
input
3
0 1 2 3 4 5
6 7 8 9 0 1
2 3 4 5 6 7
output
87
input
3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9
output
98
Note

In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.

思路:

暴力模拟,一个数字里面每个骰子有且至多能用一次

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int weishu(int num) {
if(num>=&&num<=) return ;
if(num>=&&num<=) return ;
if(num>=&&num<=) return ;
return ;
}
bool exist(int ans[][], int rec[], int len) {
if(len==) {
int a=rec[];
if(ans[][a]||ans[][a]||ans[][a]) return true;
} else if(len==) {
int a=rec[],b=rec[];
if((ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b])
||(ans[][a]&&ans[][b]))
return true;
} else if(len==) {
int a=rec[],b=rec[],c=rec[];
if((ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c])
||(ans[][a]&&ans[][b]&&ans[][c]))
return true;
}
return false;
}
int main(){
int ans[][],n,num,bns[][],rec[];
memset(ans,,sizeof(ans));
/*read*/
scanf("%d",&n);
for(int i=;i<=n;++i) {
int flag[]={};
for(int j=;j<;++j) {
scanf("%d",&num);
if(flag[num]==) {
ans[i][num]++;
flag[num]=;
}
}
}
/*slove*/
int result=;
for(int i=;i<=;++i) {
if(i==) {
result=;
break;
}
int len=weishu(i);
int temp=i;
memcpy(bns,ans,sizeof(ans));
memset(rec,-,sizeof(rec));
for(int j=;j<=len;++j) {
int r=i%;
rec[j]=r;
i=i/;
}
i=temp;
if(!exist(ans,rec,len)) {
result=i-;
break;
}
}
printf("%d\n",result);
return ;
}

优化暴力模拟,不可能出现比99大的数字,因为如果要出现比99大的数字,那么需要有1 1,2 2,3 3,4 4,5 5,6 6,7 7,8 8,9 9这样的话已经有18个数字,即使三个骰子也只是刚好18个数字。其中必不可少的是数字0,加上就是19。所以不可能出现比99大的数字。

 #include <bits/stdc++.h>
using namespace std;
int main() {
int n,ans[][],vis[];
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<=n;++i) {
for(int j=;j<=;++j) {
scanf("%d",&ans[i][j]);
}
}
for(int i=;i<=n;++i) {
for(int j=;j<=;++j) {
vis[ans[i][j]]=;
for(int k=;k<=n;++k) {
if(k==i) continue;
for(int l=;l<=;++l) {
vis[ans[i][j]*+ans[k][l]]=;
}
}
}
}
for(int i=;i<=;++i) {
if(!vis[i]) {
printf("%d\n",i-);
break;
}
}
return ;
}

codeforces 887B Cubes for Masha 两种暴力的更多相关文章

  1. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  2. Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]

    B. Our Tanya is Crying Out Loud time limit per test 1 second memory limit per test 256 megabytes inp ...

  3. Codeforces Beta Round #13 E. Holes 分块暴力

    E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...

  4. Sql Server 聚集索引扫描 Scan Direction的两种方式------FORWARD 和 BACKWARD

    最近发现一个分页查询存储过程中的的一个SQL语句,当聚集索引列的排序方式不同的时候,效率差别达到数十倍,让我感到非常吃惊 由此引发出来分页查询的情况下对大表做Clustered Scan的时候, 不同 ...

  5. Codeforces Round #369 (Div. 2) A B 暴力 模拟

    A. Bus to Udayland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #329 (Div. 2) A. 2Char 暴力

    A. 2Char Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/problem/A De ...

  7. linux尝试登录失败后锁定用户账户的两种方法

    linux尝试登录失败后锁定用户账户的两种方法 更新时间:2017年06月23日 08:44:31   作者:Carey    我要评论   这篇文章主要给大家分享了linux尝试登录失败后锁定用户账 ...

  8. 寒假集训——搜索 D - Cubes for Masha

    #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...

  9. ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板)

    ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板) 题意 题意:给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000 这 ...

随机推荐

  1. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  2. Android插件化-RePlugin项目集成与使用

    前言:前一段时间新开源了一种全面插件化的方案-- RePlugin,之前一种都在关注 DroidPlugin 并且很早也在项目中试用了,但最终没有投入到真正的生产环节,一方面是项目中没有特别需要插件化 ...

  3. 给资源文件添加指纹(Gulp版)

    至于为什么要费尽心思地给文件添加指纹,请参看前端静态资源缓存控制策略.这次要达到的小目标就是生成的资源文件能够被客户端缓存,而在文件内容变化后,能够请求到最新的文件. 需要用到的 gulp 插件是 g ...

  4. Java 常用正则表达式,Java正则表达式,Java身份证校验,最新手机号码正则表达式

    Java 常用正则表达式,Java正则表达式,Java身份证校验,最新手机号码校验正则表达式 ============================== ©Copyright 蕃薯耀 2017年11 ...

  5. c语言的,三个工具可以使编译器生成性能更佳的代码。

    内联函数声明inline 函数有时可以非常短.短函数的每次调用可以用实现该函数功能的内联代码替代,以提高执行性能.意味着不需要给函数传递或返回一个值,要让编译器采用这种技术,可以把短函数指定为inli ...

  6. open-falcon Agent配置文件修改hostname后,还是有其他名称的endpoint

    问题 open-falcon Agent在配置文件修改hostname后,log日志中还是发现其他名称的endpoint. 原因 Graph, Gateway组件会引用goperfcounter(gi ...

  7. CodeForces - 294A Shaass and Oskols

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  8. Codeforces Round #443 (Div. 2) C. Short Program

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. java线程池ThreadPool

    package com.java.concurrent; import java.util.concurrent.ExecutorService; import java.util.concurren ...

  10. JavaSE高级1

    内部类 内部类概念: 所谓内部类(Inner Class),顾名思义,就是将一个类定义在另一个类的内部.内部的类称之为内部类. 内部类的主要特点: 内部类可以很好的实现隐藏,可以使用protected ...