CodeForces 689A -Mike and Cellphone

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=412142

题目大意: 给定一个0-9数字键盘,随后输入一个操作序列,即按键手势,问该操作序列在键盘上形成的手势是否是唯一的,是YES,否NO。手势可往上下左右平移。

解题思路:

由于数字很少,可以把数字的位置表示成坐标,按这个手势的顺序匹配其它的数字(即把这些数字统一上或下或左或右平移),看匹配后的数字还在不在这个键盘上;

#include <cstdio>
#include <iostream>
using namespace std;
int n,coordinate[10][2]={2,1,1,4,2,4,3,4,1,3,2,3,3,3,1,2,2,2,3,2};
int dir[4][2]={-1,0,1,0,0,-1,0,1};
char str[10];
int  find(int d)
{
 for(int i=0;i<n;i++)
  {
  int x=str[i]-‘0‘;
  if(x==0&&(d==0||d==1||d==2)) return 0;
  if(coordinate[x][0]+dir[d][0]<1||coordinate[x][0]+dir[d][0]>3||(x!=8&&coordinate[x][1]+dir[d][1]<2)||coordinate[x][1]+dir[d][1]>4)
        return 0;
  }
  return 1;
}
void out()
{
  int flag=0;
  for(int i=0;i<4;i++)
    if(find(i)) {flag=1;if(flag) break;}
  printf(flag?"NO\n":"YES\n");
}
int main()
{
 while(cin>>n)
 {
  scanf("%s",str);
  out();
 }
 return 0;
}

 
时间: 2024-08-05 07:29:50

CodeForces 689A -Mike and Cellphone的相关文章

CoderForces 689A Mike and Cellphone (水题)

题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现. 析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8,如果有数字变成小于等于0了,那么就是不可以,同理,下是+3,8可以变成0,其他的也是这样, 注意左右平移是147,和369,是不能平移,然后就AC了.再简化一下就是如果有123,就不能上移,如果有79就不能下移,如果有147就不能左移,如果有369就不能右移,如果有0就不能下左右移. 代码如下: #

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

A - Mike and Cellphone

While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the foll

Codeforces 798D Mike and distribution(贪心或随机化)

题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2}$,$p_{3}$,...,$p_{m}$满足 $a_{p1}+a_{p2}+a_{p3}+...+a_{p_{m}}>a_{1}+a_{2}+a_{3}+...+a_{n}$ $b_{p1}+b_{p2}+b_{p3}+...+b_{p_{m}}>b_{1}+b_{2}+b_{3}+...+b_

CodeForces 689C Mike and Chocolate Thieves (二分)

原题: Description Bad news came to Mike's village, some thieves stole a bunch of chocolates from the local factory! Horrible! Aside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of choco

codeforces 798C Mike and gcd problem

C.Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beau

【算法系列学习】codeforces D. Mike and distribution 二维贪心

http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二维的贪心我们可以先让它变成其中一维有序,这样只需要重点考虑另一维,就会简单很多. 首先,对于题目要求的选择元素之和两倍大与所有元素之和,我们可以转化为选择元素之和大于剩下的.然后我们可以将下标按照a从大到小排序.然后选择第一个,之后每两个一组,选择b大的一个,如果n是偶数再选择最后一个. 至于这样写

【算法系列学习】codeforces C. Mike and gcd problem

C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 8 using namespace std; 9 const

Codeforces 547B Mike and Feet(单调栈)

题目链接:http://codeforces.com/problemset/problem/547/B 题目大意:有一个长度为n的序列,序列有长度为1...n的连续子序列,一个连续子序列里面最小的值称作这个子序列的子序列的strength,要求出每种长度的连续子序列的最大的strength.解题思路:可以用栈求出每个点的l[i],表示值小于当前位置并且在左侧的最接近这个点的位置.同理可以求出r[i],表示值小于当前位置并且在右侧侧的最接近这个点的位置.求l[i]过程如下:stack s // i