hihocoder 1154 Spring Outing

传送门

#1154 : Spring Outing

时间限制:20000ms

单点时限:1000ms

内存限制:256MB

描述

You class are planning for a spring outing. N people are voting for a destination out of K candidate places.

The voting progress is below:

First the class vote for the first candidate place. If more than half of the class agreed on the place, the place is selected. The voting ends.

Otherwise they vote for the second candidate place. If more than half of the class agreed on the place, the place is selected. The voting ends.

Otherwise they vote for the third candidate place in the same way and go on.

If no place is selected at last there will be no spring outing and everybody stays at home.

Before the voting, the Chief Entertainment Officer did a survey, found out every one‘s preference which can be represented as a permutation of 0, 1, ... K. (0 is for staying at home.) For example, when K=3, preference "1, 0, 2, 3" means that the first place is his first choice, staying at home is the second choice, the second place is the third choice and the third place is the last choice.

The Chief Entertainment Officer sends the survey results to the class. So everybody knows the others‘ preferences. Everybody wants his more prefered place to be selected. And they are very smart, they always choose the optimal strategy in the voting progress to achieve his goal.

Can you predict which place will be selected?

输入

The first line contains two integers, N and K, the number of people in your class and the number of candidate places.

The next N lines each contain a permutation of 0~K, representing someone‘s preference.

For 40% of the data, 1 <= N, K <= 10

For 100% of the data, 1 <= N, K <= 1000

输出

Output the selected place. Or "otaku" without quotes if no place is selected.

样例提示

In the sample case, if the second peoson vote against the first place, no place would be selected finally because the first person must vote against the second place for his own interest. Considering staying at home is a worse choice than the first place, the second person‘s optimal strategy is voting for the first place. So the first place will be selected.

样例输入
2 2
1 0 2
2 1 0
样例输出
1

分析这道题出自微软2016校园招聘在线笔试第二场。首先要明确题意。这道题像是一道博弈论问题(有“纳什均衡的即视感”--某君语),并且确是一道博弈论问题。但没有博弈论的知识也可以入手分析。


我最初的思路也不是正解,赛后讨论区有人询问思路,答曰“倒推”。我想了一周左右,中间还和同学讨论了一次,才明白如何倒推。


假设投票进行到了最后一轮(表决第K个也是最后一个地点)。此轮投票前,众人都知道(每个人都足够聪明)结果只有两种:去第K个地点 or 呆在家。此时,显然地,每个人都将投更(最)想去的地方。将要进行第K轮投票时,大家已经知道最后结果了。因此事实上第K轮投票是没必要的。


假设第K轮投票结果是R(K),R(K)=0或K,那么第K-1轮投票的结果是K-1或R(K),或者说第K-1轮投票实际上是众人在第K-1和R(K)之间选择,因而结果R(K-1)也是预先知道的,第K-1轮投票也没必要。


按此思路可推知R(1),这就是答案。


我们看到在本题中,无需投票众人就知到最后结果了,当然这是由于投票前preference list已成为common knowledge,在实际中这往往是不可能的。


代码不难写,复杂度是O(N*K),已是读入复杂度了,也没有优化的必要。



#include<bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int MAX_N=1e3+10, MAX_K=1e3+10;
int pref[MAX_N][MAX_K];
typedef pair<int, int> pii;
int N, K;
bool cmp(const int &x, const int &y){
    int a=x%(K+1), b=y%(K+1);
    int cnt1=0, cnt2=0;
    for(int i=0; i<N; i++){
        pref[i][a]<pref[i][b]?cnt1++:cnt2++;
    }
    return cnt1==cnt2?x>y:cnt1>cnt2;
}
int main(){
    //freopen("in", "r", stdin);
    int place;
    scanf("%d%d", &N, &K);
    for(int i=0; i<N; i++){
        for(int j=0; j<=K; j++){
            scanf("%d", &place);
            pref[i][place]=j;
        }
    }
    int last=K+1, pre=K;
    while(pre){
        if(cmp(last, pre)) --pre;
        else last=pre, --pre;
        //printf("%d %d\n", pre, last);
    }
    last==K+1?puts("otaku"):printf("%d\n", last);
    return 0;
}
				
时间: 2024-08-06 20:07:30

hihocoder 1154 Spring Outing的相关文章

PAT Spring Outing

首先题目大意为一个班级N位同学投票春游地点,K个候选地,若某个地点投票票数大于一半人数,则选择这个地点,若所有地点都没有被选择则在家呆着. 且每个人都很聪明,投票时会做出最有利与自己的选择. 输入例子为: 2 21 0 22 1 0 第一行为N,K下面N行K+1个数为他们的投票次序很容易陷入一个误区,就是每个人的次序即为他们的喜好次序. In the sample case, if the second peoson vote against the first place, no place

微软2016校招笔试 第二场部分题目个人思路

A. Lucky Substrings 这道题并不难,由于字符串长度只有100,那么它的子串肯定不超过1w个,枚举出所有字串都是没有问题的,至于检验一个子串里面不同的字母数量是不是斐波那契数,我们只需要事先把斐波那契数列小于1w的项都生成出来,然后枚举一个子串之后,统计出不同字母的数量(边找边统计,如果当前字母之前出现过就不加,如果没出现过就记住并+1),去这个里面找就行了.斐波那契数列推不了几项就到1w了-- 最后要字典序从小到大排列,并且还要去重,那就用string然后直接sort+uniq

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小时连题解都出来了··· A-烤肉拌饭 ( uva 572) 就是求联通块的数量啊,刚学dfs的时候做的那种! 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <ios

spring注解配置quartz应用

项目中会经常用到定时器,因此,其quartz的使用,我们必须要掌握.下面就以例子来讲解如何在spring中整合quartz, 使用注解配置的方式来实现定时执行任务. 一.引入jar包 项目中需引入quartz的jar包,由于整合到spring中,肯定也引入了spring的相关jar包. 例子中引用的是quartz-2.1.1版本,使用maven管理项目,pom文件需引入quartz依赖 二.spring配置文件中配置 (applicationContext.xml) 1)      xmlns和

Spring事务管理(详解+实例)

写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: Spring事务机制详解 Spring事务配置的五种方式 Spring中的事务管理实例详解 1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是要么都执行要么都

SSM整合(spring,spirngmvc,mybatis)

整合思路   准备环境:导入jar包(spring mybatis  dbcp连接池  mysql驱动包 log4j) 工程结构: --------------------------- 1.  整合dao mybatis和spring进行整合   applicationContext-dao.xml 配置: 1.数据源 2.SqlSessionFactory 3.mapper扫描器 创建po以及mapper(通过逆向工程,这里不再演示) 针对综合查询mapper,一般情况会有关联查询,建议自定

Spring Boot 热部署

需要在pom.xml文件中加如下代码: 1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-devtools</artifactId> 5 <optional>true</optional> 6 </dependency> 7 </depe

Spring多线程

Spring是通过TaskExecutor任务执行器来实现多线程和并发编程的.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一般是非阻碍的,即异步的,所以我们要在配置类中通过@EnableAsync开启对异步的支持,并通过在实际执行的Bean的方法中使用@Async注解来声明其是一个异步任务. 实例代码: (1)配置类 package com.lwh.highlight_spring4.ch3.taskexecutor; /**

Spring与JavaMail

JavaMail与Spring集成开发 spring框架集成JavaMail的主要包 2.mail.properties mail.smtp.host=smtp.163.com mail.smtp.auth=true mail.username=15511111111 mail.password=123 [email protected] 3.使用spring配置(applicationContext-mail.xml) <?xml version="1.0" encoding=