hihoCoder #1426 : What a Ridiculous Election(总统夶选)

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

Description - 题目描述

  In country Light Tower, a presidential election is going on. There are two candidates,  Mr. X1 and Mr. X2, and both of them are not like good persons. One is called a liar and the other is called a maniac. They tear(Chinese English word, means defame) each other on TV face to face, on newspaper, on internet.......on all kinds of media. The country is tore into two parts because the people who support X1 are almost as many as the people who support X2.

  After the election day, X1 and X2 get almost the same number of votes. No one gets enough votes to win. According to the law of the country, the Great Judge must decide who will be the president. But the judge doesn't want to offend half population of the country, so he randomly chooses a 6 years old kid Tom and authorize him to pick the president. Sounds weird? But the democracy in Light Tower is just like that.

  The poor or lucky little kid Tom doesn't understand what is happening to his country. But he has his way to do his job. Tom's ao shu(Chinese English word, means some kind of weird math for kids) teacher just left him a puzzle a few days ago, Tom decide that he who solve that puzzle in a better way will be president. The ao shu teacher's puzzle is like this:

  Given a string which consists of five digits('0'-'9'), like "02943", you should change "12345" into it by as few as possible operations. There are 3 kinds of operations:

   1. Swap two adjacent digits.

   2. Increase a digit by one. If the result exceed 9, change it to it modulo 10.

   3. Double a digit. If the result exceed 9, change it to it modulo 10.

  You can use operation 2 at most three times, and use operation 3 at most twice.

  As a melon eater(Chinese English again, means bystander), which candidate do you support? Please help him solve the puzzle.

    在灯塔国,一场总统选举正在举行。两个候选人为Mr. X1 与 Mr. X2,看起来都不咋的。一个疯子,一个骗子。他们在电视上、报纸上、网上……一切媒体上各种花式撕逼。因为双方的支持者数量相当,国家分成了两大派系。
选举日之后,X1与X2拥有几乎相同的选票。没人拥有足够多的票数来赢得选举。根据本国法律,谁为总统将由大法官定夺。然而法官并不想得罪任何一半国民,因此他随机挑了个6岁的小朋友Tom,由他来选。神奇吗?灯塔国的民主就是这么神奇!
伫足在命运车轮前的Tom不明觉厉。灵机一动,想起奥数老师几天前给自己留了道难题,Tom决定让能给出较优解的人当总统。奥数题如下:
给定一个由五个数字(''-'')组成的字符串,比如"",你需要把""通过尽可能少的操作变成给定字符串。3种操作如下:
.交换相邻两位数。
.将一个数加一。如果结果大于9,则模10。
.将一个数乘二。如果结果大于9,则模10。
你最多只能使用操作2三次,操作3两次。
作为一个吃瓜群众,你选谁,就帮谁搞定这个问题吧。

CN

Input - 输入

  There are no more than 100,000 test cases.

  Each test case is a string which consists of 5 digits.

    测试用例不超过10W组。
每个测试用例为一串由5个数字组成的字符串。

CN

Output - 输出

  For each case, print the minimum number of operations must be used to change "12345" into the given string. If there is no solution, print -1.

    对于每个测试用例,输出从""转换到给定串的最小操作数。若无解,则输出-。

CN

Sample Input - 样例输入

12435
99999
12374

Sample Output - 样例输出

1
-1
3

题解

  花式枚举,次序全排列 + 数值枚举 + 位置枚举。
  因为操作有顺序问题,把最多5次操作压入数组,用全排列枚举次序。
  根据上面枚举出来的全排列,DFS搜索各个位置上可能出现的数值情况,压入队列。
  (友情提示某人一开始想写5层循环,然后写到第二层就弃坑了……)
  这个时候队列中已经有不少初始元素了,相邻位置两两交换,BFS搜索可能的情况,并更新。
  (队列 == BFS ? 雾 : 雾)

代码 C++

 #include<cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
std::queue<int> q;
int cnt[], wta[][], ts[] = { , , , , }; void pushQ(int a, int c){
if (c >= cnt[a]) return;
cnt[a] = c;
q.push(a);
}
void DFS(int iw, int it){
if (iw == ){
int sum = , j;
for (j = ; j < ; ++j) sum = sum * + wta[][j] % ;
pushQ(sum, it);
return;
}
if (iw) memcpy(wta[iw], wta[iw - ], sizeof ts);
for (DFS(iw + , it); it < ; DFS(iw + , ++it)){
if (ts[it]) wta[iw][iw] <<= ;
else ++wta[iw][iw];
}
}
void rdy(){
memset(cnt, , sizeof(cnt));
int i, j, now, c, tmp[];
do{
for (i = ; i < ; ++i) wta[][i] = i + ;
DFS(, );
} while (std::next_permutation(ts, ts + )); while (!q.empty()){
c = now = q.front(); q.pop();
for (i = ; ~i; --i){ tmp[i] = now % ; now /= ; }
for (i = ; i < ; ++i){
tmp[i] ^= tmp[i + ]; tmp[i + ] ^= tmp[i]; tmp[i] ^= tmp[i + ];
for (j = now = ; j < ; ++j) now = now * + tmp[j];
pushQ(now, cnt[c] + );
tmp[i] ^= tmp[i + ]; tmp[i + ] ^= tmp[i]; tmp[i] ^= tmp[i + ];
}
}
}
int main(){
rdy();
int opt;
while (~scanf("%d", &opt)){
if (cnt[opt] == cnt[]) puts("-1");
else printf("%d\n", cnt[opt]);
}
return ;
}

hihoCoder 1426 : What a Ridiculous Election(总统夶选)的更多相关文章

  1. What a Ridiculous Election UVALive - 7672 (BFS)

    题目链接: E - What a Ridiculous Election  UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...

  2. 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)

    Description In country Light Tower, a presidential election is going on. There are two candidates,   ...

  3. The 2016 ACM-ICPC Asia Beijing Regional Contest E - What a Ridiculous Election

    https://vjudge.net/contest/259447#problem/E bfs,k个限制条件以数组的额外k维呈现. #include <bits/stdc++.h> usi ...

  4. [译]ZOOKEEPER RECIPES-Leader Election

    选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...

  5. TED #05# How we can face the future without fear, together

    Rabbi Lord Jonathan Sacks: How we can face the future without fear, together 1. what was it like bei ...

  6. The 2016 ACMICPC Asia Beijing Regional Contest

    A. Harmonic Matrix Counter (3/19) B. Binary Tree (1/14) C. Asa's Chess Problem (21/65) [ Problem ] 给 ...

  7. Zookeeper全解析——Paxos作为灵魂(转)

    原计划在介绍完ZK Client之后就着手ZK Server的介绍,但是发现ZK Server所包含的内容实在太多,并不是简简单单一篇Blog就能搞定的.于是决定从基础搞起比较好. 那么ZK Serv ...

  8. Zookeeper全解析——Paxos作为灵魂

    原文地址: http://www.spnguru.com/2010/08/zookeeper%E5%85%A8%E8%A7%A3%E6%9E%90%E2%80%94%E2%80%94paxos%E7% ...

  9. ZooKeeper之(三)工作原理

    3.1 系统架构 ZooKeeper集群是由多台机器组成的,每台机器都充当了特定的角色,各种角色在协作过程中履行自己的任务,从而对外提供稳定.可靠的服务. 由上图可知,ZooKeeper集群由多台机器 ...

随机推荐

  1. EF 延迟加载和预先加载

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨延迟加载和预先加载 Entity Frame ...

  2. working with fitnesse wiki pages

    fitnesse提供一个简单易用的wiki创建一个web页面用于测试.测试页面有一个button,允许所有的测试在这个页面运行,因此任何人在任何时间都可以去这个页面点击这个按钮,查看测试是否通过.fi ...

  3. Java绘图

    paintComponent(Graphics g)方法: 1.不调用super.paintComponent(g)的话,rePaint()的时候就会把你写的paintComponent中的内容绘制上 ...

  4. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  5. web 安全的前期准备哦

    学习web安全  需要的软件  和 基础 基础会在以后发出来  软件  在网上都可以下 首先   你要有一款虚拟机软件 虚拟系统通过生成现有操作系统的全新虚拟镜像,它具有真实windows系统完全一样 ...

  6. 浅谈Java中的引用

    在Java语言中,引用是指,某一个数据,代表的是另外一块内存的的起始地址,那么我们就称这个数据为引用. 在JVM中,GC回收的大致准则,是认定如果不能从根节点,根据引用的不断传递,最终指向到一块内存区 ...

  7. 纳尼,java可以在接口中实现非抽象方法了?

    纳尼,接口中可以定义实例方法了?! 纳尼,接口中还可以定义静态方法了?! 没错,在Java8中新增了很多新的特性,其中就包括可以在接口中添加方法和变量. 首先我们来看下代码 public interf ...

  8. Cassandra 键空间(keyspace),表(table)

    查看用户下信息: describe cluster; desc cluster;   查看所有keyspace: describe keyspaces; desc keyspaces;   查看key ...

  9. python学习笔记系列----(六)错误和异常

    python至少有2类不同的错误:语法错误(Syntax Errors)和异常(Exceptions). 8.1 语法错误 这个单词应该还是很有必要认识的,呵呵,语法错误,也叫解析错误,是我们最不愿意 ...

  10. .net之工作流工程展示及代码分享(三)数据存储引擎

    数据存储引擎是本项目里比较有特色的模块. 特色一,使用接口来对应不同的数据库.数据库可以是Oracle.Sqlserver.MogoDB.甚至是XML文件.采用接口进行对应: public inter ...