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

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决定让能给出较优解的人当总统。奥数题如下:
    给定一个由五个数字(‘0‘-‘9‘)组成的字符串,比如"02943",你需要把"12345"通过尽可能少的操作变成给定字符串。3种操作如下:
     1.交换相邻两位数。
     2.将一个数加一。如果结果大于9,则模10。
     3.将一个数乘二。如果结果大于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.

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

CN

Sample Input - 样例输入

12435
99999
12374

Sample Output - 样例输出

1
-1
3

题解

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

代码 C++

 1 #include<cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 std::queue<int> q;
 6 int cnt[100000], wta[5][5], ts[5] = { 0, 0, 0, 1, 1 };
 7
 8 void pushQ(int a, int c){
 9     if (c >= cnt[a]) return;
10     cnt[a] = c;
11     q.push(a);
12 }
13 void DFS(int iw, int it){
14     if (iw == 5){
15         int sum = 0, j;
16         for (j = 0; j < 5; ++j) sum = sum * 10 + wta[4][j] % 10;
17         pushQ(sum, it);
18         return;
19     }
20     if (iw) memcpy(wta[iw], wta[iw - 1], sizeof ts);
21     for (DFS(iw + 1, it); it < 5; DFS(iw + 1, ++it)){
22         if (ts[it]) wta[iw][iw] <<= 1;
23         else ++wta[iw][iw];
24     }
25 }
26 void rdy(){
27     memset(cnt, 127, sizeof(cnt));
28     int i, j, now, c, tmp[5];
29     do{
30         for (i = 0; i < 5; ++i) wta[0][i] = i + 1;
31         DFS(0, 0);
32     } while (std::next_permutation(ts, ts + 5));
33
34     while (!q.empty()){
35         c = now = q.front(); q.pop();
36         for (i = 4; ~i; --i){ tmp[i] = now % 10; now /= 10; }
37         for (i = 0; i < 4; ++i){
38             tmp[i] ^= tmp[i + 1]; tmp[i + 1] ^= tmp[i]; tmp[i] ^= tmp[i + 1];
39             for (j = now = 0; j < 5; ++j) now = now * 10 + tmp[j];
40             pushQ(now, cnt[c] + 1);
41             tmp[i] ^= tmp[i + 1]; tmp[i + 1] ^= tmp[i]; tmp[i] ^= tmp[i + 1];
42         }
43     }
44 }
45 int main(){
46     rdy();
47     int opt;
48     while (~scanf("%d", &opt)){
49         if (cnt[opt] == cnt[0]) puts("-1");
50         else printf("%d\n", cnt[opt]);
51     }
52     return 0;
53 }
时间: 2024-10-29 10:47:03

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

HihoCoder - 1426 What a Ridiculous Election (BFS预处理)

Problem E. What a Ridiculous Election 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 ma

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

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,

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 being in America during the recent presidential election 总统选举 2. And one way into it is to see that perhaps the most simple way into a culture and into a

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

原计划在介绍完ZK Client之后就着手ZK Server的介绍,但是发现ZK Server所包含的内容实在太多,并不是简简单单一篇Blog就能搞定的.于是决定从基础搞起比较好. 那么ZK Server最基础的东西是什么呢?我想应该是Paxos了.所以本文会介绍Paxos以及它在ZK Server中对应的实现. 先说Paxos,它是一个基于消息传递的一致性算法,Leslie Lamport在1990年提出,近几年被广泛应用于分布式计算中,Google的Chubby,Apache的Zookeep

Zookeeper白话解析

先说Paxos,它是一个基于消息传递的一致性算法,Leslie Lamport在1990年提出,近几年被广泛应用于分布式计算中,Google的Chubby,Apache的Zookeeper都是基于它的理论来实现的,Paxos还被认为是到目前为止唯一的分布式一致性算法,其它的算法都是Paxos的改进或简化.有个问题要提一下,Paxos有一个前提:没有拜占庭将军问题.就是说Paxos只有在一个可信的计算环境中才能成立,这个环境是不会被入侵所破坏的. 关于Paxos的具体描述可以在Wiki中找到:ht

Paxos分布式一致性算法简介和Apache ZooKeeper的概念映射

Paxos是一个基于消息传递的一致性算法,近几年被广泛应用于分布式计算中,Google的Chubby,Apache的Zookeeper都是基于它的理论来实现的,Paxos还被认为是到目前为止唯一的分布式一致性算法,其它的算法都是Paxos的改进或简化.Paxos只有在一个可信的计算环境中才能成立,这个环境是不会被入侵所破坏的. 由Leslie Lamport发明了Paxos算法,他目前供职于微软研究院.1998年在ACM Transactions on Computer Systems的<The

ZooKeeper: 简介, 配置及运维指南

1. 概览 ZooKeeper是一个供其它分布式应用程序使用的软件, 它为其它分布式应用程序提供所谓的协调服务. 所谓的协调服务, 是指ZooKeeper的如下能力 naming 命名 configuration management 配置管理 synchronization 同步 group service 分组服务 上面四个功能可能现在不太好说清, 但大致上目前你需要明白ZooKeeper就是为其它分布式应用程序提供一些基础功能的程序就好了. 我们以其中的配置管理为例. 假设你在写一个可横向

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

原计划在介绍完ZK Client之后就着手ZK Server的介绍,但是发现ZK Server所包含的内容实在太多,并不是简简单单一篇Blog就能搞定的.于是决定从基础搞起比较好. 那么ZK Server最基础的东西是什么呢?我想应该是Paxos了.所以本文会介绍Paxos以及它在ZK Server中对应的实现. 先说Paxos,它是一个基于消息传递的一致性算法,Leslie Lamport在1990年提出,近几年被广泛应用于分布式计算中,Google的Chubby,Apache的Zookeep

2016年总统选举的预测

ASA的美国总统竞选 在这个大选之年,美国统计协会(ASA)将学生竞赛和总统选举放在一起,将学生预测谁是2016年总统大选的赢家准确的百分比作为比赛点.详情见: http://thisisstatistics.org/electionprediction2016/ 获取数据 互联网上有很多公开的民调数据.可以下面的网站获取总统大选的相关数据: http://projects.fivethirtyeight.com/2016-election-forecast/national-polls/ 其他