codeforces 887B Cubes for Masha 两种暴力
1 second
256 megabytes
standard input
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.
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.
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.
3
0 1 2 3 4 5
6 7 8 9 0 1
2 3 4 5 6 7
87
3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9
98
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 两种暴力的更多相关文章
- codeforces 792CDivide by Three(两种方法:模拟、动态规划
传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...
- 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 ...
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- Sql Server 聚集索引扫描 Scan Direction的两种方式------FORWARD 和 BACKWARD
最近发现一个分页查询存储过程中的的一个SQL语句,当聚集索引列的排序方式不同的时候,效率差别达到数十倍,让我感到非常吃惊 由此引发出来分页查询的情况下对大表做Clustered Scan的时候, 不同 ...
- 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 ...
- 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 ...
- linux尝试登录失败后锁定用户账户的两种方法
linux尝试登录失败后锁定用户账户的两种方法 更新时间:2017年06月23日 08:44:31 作者:Carey 我要评论 这篇文章主要给大家分享了linux尝试登录失败后锁定用户账 ...
- 寒假集训——搜索 D - Cubes for Masha
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...
- ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板)
ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板) 题意 题意:给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000 这 ...
随机推荐
- MySQL复制之理论篇
一.MySQL复制概述 MySQL支持两种复制方式:基于行的复制和基于语句的复制(逻辑复制).这两种方式都是通过在主库上记录 二进制日志.在备库重放日志的方式来实现异步的数据复制,其工作原理如下图: ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 一起来学linux:SSH远程登陆
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 在最早的远程连接技术,主要是telnet和RSH为主.缺点也很明显,就是明文传输.在 ...
- .NET Core 使用RSA算法 加密/解密/签名/验证签名
前言 前不久移植了支付宝官方的SDK,以适用ASP.NET Core使用支付宝支付,但是最近有好几位用户反应在Linux下使用会出错,调试发现是RSA加密的错误,下面具体讲一讲. RSA在.NET C ...
- Asp.net MVC4高级编程学习笔记-模型学习第五课MVC表单和HTML辅助方法20171101
MVC表单和HTML辅助方法 一.表单的使用. 表单中的action与method特性.Action表示表单要提交往那里,因此这里就有一个URL.这个URL可以是相对或绝对地址.表单默认的method ...
- github和本地仓库关联
1.安装git 2.在github上注册账号 3.github上创建repository 4.克隆github上的repository 5.在target directory中右键打开git gui
- Leetcode题解(30)
98. Validate Binary Search Tree 题目 分析:BST按照中序遍历之后所得到的序列是一个递增序列,因此可以按照这个思路,先中序遍历,保存好遍历的结果,然后在遍历一遍这个序列 ...
- hdu 2089 不要62(入门数位dp)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 使用sklearn进行数据挖掘-房价预测(6)—模型调优
通过上一节的探索,我们会得到几个相对比较满意的模型,本节我们就对模型进行调优 网格搜索 列举出参数组合,直到找到比较满意的参数组合,这是一种调优方法,当然如果手动选择并一一进行实验这是一个十分繁琐的工 ...
- Phonegap开发相关问题
环境搭建:参考http://www.phonegapcn.com/start/zh/1.3/#android 调试: 1.在线远程调试 http://debug.phonegap.com/ 通过USB ...