首页 > 试题广场 >

Spring Outing

[编程题]Spring Outing
  • 热度指数:3397 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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
示例1

输入

2 2
1 0 2
2 1 0

输出

1

对于每一个地点,我们可以把人分成三类:

1、当前地点在这类人的眼里优先级是最高的;

2、当前地点在这类人的眼里优先级不是最高,但是比蹲在家里好;

3、宁愿蹲家里也不去当前地点的人;

 

那么当给某个地点投票时,13类人的意愿是很明确的,优先级最高的肯定投同意,家里蹲的肯定投反对,剩下的2类人就是要考虑投与不投的,而这类人考虑的因素就是后面能否确定得到【而不是存在】更好的选择。并且我们还知道,如果某个地点的优先级比0还低的话那么这个地点也是肯定被投反对的。

 

所以剩下的问题就是怎样去求“后面能否确定得到【而不是存在】更好的选择”。先模拟一下样例的选择过程(从后往前推,到达最后一个地点进行投票时,结果是确定的,因为这时只有两个选择了):

1、当对p2投票时,学生s1明显选择家里蹲也不会选择去地点p2



2、因此我们知道了对p2的投票结果,回到p1,如果2X票的话,那么会造成家里蹲的结果。


由此,我们可以知道,假设某个地点pk一定会通过投票,那么对于pnn > k)就是不可能被选择到的,因为到达pk时投票结束。

那么对于当前点pi(i < k)的投票,2类人要考虑的是:对于 pk 点,如果pk 优先级高于pi ,那么必定对pi 投反对票,因为过了pi点,下一个会被赞同的点必然是pk点,根据题意每个人尽量想去优先级高的地点。

例如上面的选择过程,对p1进行投票时,我们知道过了p1下一个pk点是home,而对学生s2来说,p1的优先级是高于home的,并且没有其它优先级高于p1的可达点(当然,有的话home就不是pk点了)

而对于最后一个点的投票是确定的,所以从最后的点往前搜索,记录pk点,最后得到的pk点就是问题所求。

=-= 同样,代码就不贴了

发表于 2015-05-02 00:36:31 回复(9)
import java.util.*;
public class Main{
    public static void main(String[] args){
        new Main().process();
    }
    
    private int N;
    
    private int K;
    
    private void process(){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            N=sc.nextInt();//N represents number of persons
            K=sc.nextInt();//K represents number of destinaitons
            pArray=new int[N][];
            for(int i=0;i<N;i++){
                pArray[i]=new int[K+1];
                for(int j=0;j<K+1;j++){
                    pArray[i][j]=sc.nextInt();
                }
            }
            voteRecords=new int[K+2];
            for(int i=0;i<=K;i++){voteRecords[i]=-1;}//Initiate recoards
            int dest=vote(1);//Get result
            String result=null;
            if(dest==0) result="otaku";
            else result=dest+"";
            voteRecords=null;//Release memory
            System.out.println(result);
        }
        sc.close();
    }
    
    private int[] voteRecords;
    
    private int vote(int dest){
        if(dest==K+1){
            voteRecords[dest]=0; 
            return 0;//Stay at home
        }
        if(voteRecords[dest+1]==-1){
            vote(dest+1);
        }
        voteRecords[dest]=voteIn(dest,voteRecords[dest+1]);
        return voteRecords[dest];
    }
    
    private int[][] pArray;
    
    private int voteIn(int dest1,int dest2){
        int counter=0;
        for(int i=0;i<N;i++){
            int p1=0,p2=0;
            for(int j=0;j<K+1;j++){
                if(pArray[i][j]==dest1){p1=j;continue;}
                if(pArray[i][j]==dest2){p2=j;continue;}
            }
            if(p1<p2) counter++;
        }
        if(counter>N/2) return dest1;
        else return dest2;
    }
}
本题采用递归:
vote(int k)函数的含义:前k-1次投票结果都是反对的情况下,产生的最终投票结果
题目条件:N个人投票,K个可选目的地
递归过程:
(1) 如果前K次投票结果都是反对,那么呆在家里。
即,
k=K+1时vote(K+1)=0;
(2) 如果前k次投票结果都是反对,那么最终结果取决于第k次以后的投票结果。
即,
vote(k)=vote(k+1)         ,半数以上的人反对去k
vote(k)=k                     , 半数以上的人同意去k

2 2
1 0 2
2 1 0
以题目给的输入为例
vote(3)=0,
    因为前两次投票的结果都是反对
vote(2)=0,
    目的地1的投票结果已经是反对,如果此次投票结果为反对,那么vote(2)=vote(3)=0;否则vote(2)=2
    对于第一个人来说,他更希望呆在家里,所以他会投反对票,这样就有超过一半的人反对了,因此vote(2)=vote(3)=0
vote(1)=1
    如果第一次投票的结果是反对,那么vote(1)=vote(2)=0
    否则vote(1)=1
    与呆在家里相比,两个人都更希望去1,所以两个人都赞成
最终结果为vote(1)=1
发表于 2016-10-06 17:58:07 回复(0)
 import java.util.*;

public class Main{
	static int[][] p;
    public static boolean judge(int row,int k,int pi,int pj){
        for(int i=0;i<k+1;i++){
            if(p[row][i]==pi)
                return true;
            if(p[row][i]==pj)
                return false;
        }
        return true;
    }
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
        int n,k;
        while(sc.hasNext()){
            n=sc.nextInt();
            k=sc.nextInt();
            p=new int[n][k+1];
            for(int i=0;i<n;i++)
                for(int j=0;j<k+1;j++)
                	p[i][j]=sc.nextInt();
            
            int pk=0;
            int vote;
            for(int i=k;i>0;i--){
                vote=0;
                for(int j=0;j<n;j++){
                    if(p[j][0]==i)
                        vote++;
                    else if(judge(j,k,i,0)){
                        if(judge(j,k,i,pk)){
                            vote++;
                        }
                    }
                }
                if(vote>n/2)
                    pk=i;
            }
            if(pk==0)
                System.out.println("otaku");
            else
                System.out.println(pk);
        }
      
       sc.close();
	}
}


/*您的代码已保存
答案错误:您提交的程序没有通过所有的测试用例
case通过率为5.88%

测试用例:
10 6
5 6 4 3 1 0 2
6 3 2 0 5 4 1
4 1 2 0 6 3 5
1 2 5 6 0 3 4
3 5 1 0 4 6 2
2 4 6 5 0 3 1
4 1 5 6 3 0 2
2 6 0 4 3 1 5
2 5 4 0 3 6 1
0 3 1 2 4 5 6

对应输出应该为:

1

你的输出为:

6讲道理,第2 6  8 9 10号 选家里蹲也不会选地点1啊?1能胜出?
*/

编辑于 2016-09-20 19:56:59 回复(2)

好彩我啊,题目虽然看懂了,就是没思路,5555
看了大神的答案之后,加上自己的理解,和自己爬过的坑

package com.special.first;

import java.util.Scanner;

/**
 * 微软03-Spring Out
 *
 * 技巧一:我们的二维数组用哈希的思想,地点作为索引,而值作为优先级
 *
 * 算法的第一次理解,先假设一个地点为预选,然后每次对新地点投票时都会与上一个局部优解比较,取局部最优
 * 刚开始我想了想这个算法,这样是不对的,0其实与其他地点是无区别,
 * 这样的结果会严重依赖初始的地点为0
 *
 * 后来我又看了看题,这个题其实是默认了一个条件就是,一个人对于低于家里蹲的所有的地点都会投反对
 * 这个题带了点生活上的意思,就是我想出去玩,只要比家里蹲好就行。
 * 但是我不想出去玩,敲代码很累啊,还不休息休息,Mdzz, 你们提出的地点我肯定反对
 *
 * 还有一点就是之所以我们倒着来,是因为假设前k - 1前都不通过,那么再对K投票时,大家只会考虑k和家里蹲的权衡了
 * 然后我们再对k - 1投票时,就可以依据k的结果,来对比选择,这样每次都只需比较一个地点就行,而这个地点恰巧是
 * 之后的地点的局部最优。这样的做的原因是有些人对于当前地点不是最优或者比家里蹲好时,他会想之后可不可能存在
 * 比当前地点更好的选择呢。我们从后投票倒推就是要告诉这些人不要脑补了,我们帮你计算了如果你否定当前节点,你之后会得到
 * 什么地点,这时在面对当前节点,他们就只权衡当前节点和之后的最优比较就行了。
 * 这思想,很好,赞一个。
 * Create by Special on 2018/2/14 17:25
 */
public class Pro006 {

    public static int getPlace(int[][] votes, int n, int k){
        int selected = 0, count = 0;
        for(int i = k; i > 0; i--){
            count = 0;
            for(int j = 0; j < n; j++){
                if(votes[j][i] < votes[j][selected]){
                    count++;
                }
            }
            if(count > n / 2){
                selected = i;
            }
        }
        return selected;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            int n = input.nextInt();
            int k = input.nextInt(), place;
            int[][] votes = new int[n][k + 1];
            for(int i = 0; i < n; i++){
                for(int j = 0; j <= k; j++){
                    place = input.nextInt();
                    votes[i][place] = j;
                }
            }
            int selected = getPlace(votes, n, k);
            System.out.println(selected == 0 ? "otaku" : selected);
        }
    }
}
编辑于 2018-02-14 18:39:32 回复(0)
 #include <iostream>

using namespace std;

const int MAX = 1010;
int n,k,level;

void Fun(int a[MAX][MAX], int m)
{
    int vote=0;
    for(int i=0;i<n;i++)
    {
        if(a[i][m] < a[i][level])
            vote++;
    }
    if(vote > n/2)
        level = m;
}

int main()
{
    while(cin>>n>>k)
    {
        int a[MAX][MAX],t;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<k+1;j++)
            {
                cin>>t;
                a[i][t] = j;
            }
     
        }
        level = 0;
        for(int i=k;i>0;i--)
            Fun(a,i);
        if(level == 0)
            cout<<"otaku"<<endl;
        else
            cout<<level<<endl;
    }
    return 0;
} 

发表于 2017-12-17 00:32:57 回复(0)
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        while (s.hasNext()) {
            print(s);
        }
    }

    private static void print(Scanner s) {
        int n = s.nextInt(), k = s.nextInt();
        int A[][] = new int[n + 1][k + 2], B[] = new int[k + 2];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= k + 1; j++) {
                A[i][j] = s.nextInt();
            }
        }

        for (int i = k; i >= 1; i--) {
            int c = 0;
            for (int x = 1, p1 = 0, p2 = 0; x <= n; x++) {
                for (int y = 1; y <= k + 1; y++) {
                    if (A[x][y] == i)
                        p1 = y;
                    if (A[x][y] == B[i + 1])
                        p2 = y;
                }
                if (p1 < p2)
                    c++;
            }
            if (c > n / 2)
                B[i] = i;
            else
                B[i] = B[i + 1];
        }
        System.out.println(B[1] == 0 ? "otaku" : B[1]);
    }
}

发表于 2017-04-26 23:07:01 回复(0)
考虑班上有N个人,有K个候选地,组成N*K矩阵
X11,X12,...,X1K
X21,X22,...,X2K
...
XN1,XN2,...XNK

从第一列开始遍历到最后一列,对每个候选地计数,第一个超过一半的候选地为答案;如果同时有多个候选地符合标准,选候选地下标最大的。
如:
student1:120
student2:210

分析:第1次对候选地1投票,student1必定同意,student2如果同意,就此打住。如果student2不同意,第2次对候选地2投票,student1必定同意,student2必定同意,可见,由于候选地投票有先后顺序,导致喜欢候选地排在后面的人更占优势。
   
编辑于 2015-09-08 10:55:13 回复(0)
/*题目应该逆向考虑,因为每个人要达到自己的最优解,即尽可能的让自己优先级
在前的地点成为最终目的地。例如
6 5 
4 3 5 2 1 0 
4 1 5 0 2 3 
3 1 5 4 0 2 
3 1 2 4 0 5 
5 1 2 0 4 3 
0 3 2 4 1 5
先考虑假如之前的四个地方都没通过,那给5投票时,每个人只需和0(家里蹲)比较。
自然第一个人 5优先级在0前,故投一票,第二个同理,此刻投票地点优先级在前,
则投票,在后则反对不投。最终4票过半,地点5投票成功。
第二轮对地点4投票,此时只需和5比较优先级。规则同上,
此刻优先级 比较 当前最优解优先级。因为出题人假设每个人是聪明的,
即使对于第6个人,家里蹲0的优先级最高,可当投票进行到地点4他依然会给4投支持票,
因为即使把4pass掉,5也会选中,还是轮不到家里顿0,因此聪明的人退而求其次,
把票投给了4.
好了,思路清楚了,这题就非常好做,贴上AC代码,抛砖引玉。*/
#include<iostream>
using namespace std;
int n,k,kk;
void PD(int a[1000][1000],int mk){
    int vote=0,i,j;
    for(i=0;i<n;i++){
        if(a[i][mk] < a[i][kk])
            vote++;
    }
    if( vote > n/2 )
        kk = mk;
}
int main(){
    while( cin >> n >> k ){
        int i,j,temp,a[1000][1000];
        for(i=0;i<n;i++){
            for(j=0;j<k+1;j++){
                cin >> temp;
                a[i][temp] = j;
            }
        }
        kk =0 ;
        for(i=k;i>0;i--)
            PD(a,i);
        if(kk == 0)
            cout <<"otaku"<<endl;
        else
            cout <<kk<<endl;
    }
}

编辑于 2017-07-24 20:12:34 回复(5)
def vote(a):
    global con
    vote = 0
    for i in range(0,n):
        if lst[i][a]<lst[i][con]:
            vote+=1
    if vote>n//2:
        con = a
try:
    while True:
#        lst = np.zeros((1001,1001),dtype=np.int)
        n,k = map(int,input().split())
        lst,con = [[0 for i in range(0,k+1)] for i in range(0,n)],0
        for i in range(0,n):
            tem = list(map(int,input().split()))
            for j in range(0,k+1):
                lst[i][tem[j]] = j     
        for i in range(k,0,-1):
            vote(i)
        if con==0:
            print('otaku')
        else:
            print(con)      
except:
    pass

发表于 2018-11-29 22:03:14 回复(1)
import java.util.Scanner;
/*
*
*这道题目的关键点不是题目本身有多难,而是第一要能理解题意、第二要能把题目意思捋清楚:
*题目意思是投票是一轮接一轮进行的,每有一个地点就会进行一轮投票,每个人只会根据自己的‘意愿list’去确定
*本轮投票是投赞成票还是投反对票。但是每一个人都很聪明,他们必须知道如果自己对当前地点投否决票、后面几个地点的几轮投票是否会产生更好的结果,
*所以需要从后往前推,先假设自己对当前地点投否决票、那么如果后面有更好的结果、他会真的投否决票、否则投赞成票。
*需要取一个特殊的地点(K(大写K)+1),代表哪儿也不去、就待家里。
本题采用递归:
vote(int k)函数的含义,前i(1<=i<=k-1)次投票没有确定要去任何地点i的情况下,投票确定是否去第k(k=i+1)个地点
题目条件:N个人投票,K个可选目的地
递归过程:
(1) 如果前K(大写K)次投票结果都是反对去对应地点,那么呆在家里。
即,
k(小写k)=K(大写K)+1时vote(K+1)=0;
(2) 如果前k(小写k)次投票结果都是反对去对应地点k,那么最终结果取决于第k次以后的投票结果。
即,
vote(k)=vote(k+1)         ,半数及以上的人反对去k
vote(k)=k                 , 半数以上的人同意去k(同意的人数超过一半该地点才当选,等于一半或少于一半都不会当选)

2 2
1 0 2
2 1 0
以题目给的输入为例

    1.投票者全部假定自己前两次投票的结果都是反对票,那么vote(3)=0,实际上只有两个地点、3是假设出来的地点、代表哪儿也不去

    2.投票者全部假定自己对目的地1的投票结果是反对,如果此次投票结果再为反对,那么vote(2)=0,vote(3)=0;否则vote(2)=2,vote(3)=0;
    对于第一个人来说,比较与待在家里、他更希去地点2,所以他会投否决票,这样就有不少于一半的人反对了,因此vote(2)=0,vote(3)=0,
    
    3.对第一个地点投票,
    第一个人已经预见如果自己本轮投否决票、那么后面自己只能待在家里(因为已经知道vote(2)=0,vote(3)=0),因为他第地点1的意愿高于待家里,所以他会投赞成票
     第二个人已经预见如果自己本轮投否决票、那么后面自己只能待在家里(因为已经知道vote(2)=0,vote(3)=0),因为他第地点1的意愿高于待家里,所以他也会投赞成票
最终结果为vote(2)=0,vote(3)=0,vote(1)=1;
补充:有可能会出现两个非零地点的优先级的比较情况
*/
public class Main{
    static int[] voteRecords;
    static int[][] list;
    static int N;
    static int K;
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
        N=in.nextInt();
        K=in.nextInt();
         list=new int[N][K+1];
        for(int i=0;i<N;i++)
            for(int j=0;j<K+1;j++)
                list[i][j]=in.nextInt();
        voteRecords=new int[K+2];//投票记录
        for(int i=1;i<=K;i++)
            voteRecords[i]=-1;//初始化投票记录
        int res=vote(1);
        String result;
        if(res==0)
            result="otaku";
        else
            result=""+res;
        System.out.println(result);
            }
    }
    public static int vote(int dest){//从后往前推,根据已经预料到的结果确定每一个当前地点是选中还是不选中
        if(dest==(K+1)){
            voteRecords[dest]=0;
            return 0;
            }
        if(voteRecords[dest+1]==-1)
            vote(dest+1);
        voteRecords[dest]=voteIn(dest,voteRecords[dest+1]);
        return voteRecords[dest];
    }
    public static int voteIn(int dest1,int dest2){//比较两个地点的优先级
        int p1=0,p2=0;
        int counter=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<K+1;j++){
                if(list[i][j]==dest1) {p1=j;continue;}
                if(list[i][j]==dest2) {p2=j;continue;}
            }
            if(p1<p2) counter++;
        }
        if(counter>N/2) return dest1;
        else 
            return dest2;
    }
}
发表于 2017-10-13 10:30:48 回复(0)
讲道理,题目是个啥意思啊!!!啊啊啊,英语不好是个硬伤啊!

发表于 2017-03-29 13:22:31 回复(0)
#Python版

# -*- coding:utf-8 -*-
import sys


def isOk(nums, n, ans, ks):
    cnt = 0
    for i in range(n):
        if nums[i][ks] < nums[i][ans] :
            cnt +=1
    if cnt > n/2:
        return True
    return False

if __name__ == '__main__':
    while 1:
        nk = sys.stdin.readline().strip()
        if not nk:
            break
        n,k =[int(i) for i in nk.split(' ')]
        nums =[[0]*(k+1) for i in range(n)]
        for i in range(n):
            places = [int(j) for j in sys.stdin.readline().strip().split(' ')]
            for ind,v in enumerate(places):
                nums[i][v] = ind
        ans = 0
        for ks in range(k,0,-1):
            if isOk(nums,n,ans,ks):
                ans = ks
        if ans ==0:
            print 'otaku'
        else:
            print ans

发表于 2017-03-20 16:42:44 回复(0)
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

int selectP(int N,int K){
    vector<vector<int> > v;
    v.reserve(N);
    int m;
    for(int i=0;i<N;++i){
        vector<int> vv;
        vv.reserve(K+1);
        for(int j=0;j<K+1;++j){
            cin>>m;
            vv.push_back(m);
        }
        v.push_back(vv);
    }
    vector<int> cont(K+1,0);
    for(int i=0;i<N;++i){
        for(int j=0;j<K+1;++j){
            if(v[i][j]>0){
                if(++cont[v[i][j]]>=(N/2+1))
                    return v[i][j];
            }
            else
                break;
        }
    }
    return 0;
}

int main()
{
    int N,K;
    cin>>N>>K;
    int m = selectP(N,K);
    if(m)
        cout<<m;
    else
        cout<<"otaku";
    return 0;
}

测试用例:
6 1
0 1
1 0
1 0
0 1
0 1
1 0

对应输出应该为:
otaku

你的输出为:

Q
时间 得分 结果 题目 语言 用时(ms) 内存(KB)
2016-10-12 0 答案错误 Spring Outing C++ 0 8552
为什么不没有输出呢,在电脑上运行没问题啊

发表于 2016-10-12 21:43:01 回复(2)
/* 关键是要模拟投票过程,并且要从后往前想,从第K次投票开始往前推,可以逐级确定每一轮投票的结果,有点递归地思想
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader stdin = new BufferedReader(new InputStreamReader(
				System.in));
		String str = stdin.readLine();
		while (str != null && str.length() > 0) {
			String[] ss = str.split(" ");
			int N = Integer.parseInt(ss[0]);
			int K = Integer.parseInt(ss[1]);
			int[][] favor = new int[N][K + 1];//favor[i][j]表示第i個人对第j个景点的喜欢程度,值越小表示越喜欢
			for (int i = 0; i < N; i++) {
				str = stdin.readLine();
				ss = str.split(" ");
				for (int j = 0; j < K + 1; j++) {
					favor[i][Integer.parseInt(ss[j])] =j ;
				}
			}
			int votes=0;
			//从最后一轮投票开始往回找,因为到了后面没有别的选择了,每个人可以清楚自己是否投票,有点递归的思想
			int finalChoice=0;
			for(int i=K;i>=1;i--){
				votes=0;
				for(int j=0;j<N;j++){
					//比较每一个人对当前方案和下一方案的喜欢程度,来决定是否投票
					if(favor[j][i]<favor[j][finalChoice]){
						votes++;
					}
				}
				if(votes>N-votes){//投票超过一半,更新最终方案
					finalChoice=i;
				}
			}
			if(finalChoice==0){
				System.out.println("otaku");
			}else{
				System.out.println(finalChoice);
			}
			str = stdin.readLine();
		}
	}
}

发表于 2016-09-11 21:39:28 回复(2)
//综合了Ghost和胡磊的思路,采用从后往前推的方法,
//在本地电脑C-Free5.0编译器可以通过运行,但是网上提交,出现了和牛客560236号相同的问题:
//测试用例:
//6 1
//0 1
//1 0
//1 0
//0 1
//0 1
//1 0
//
//对应输出应该为:
//otaku
//
//你的输出为:
//(无输出??但在本地编译器上可以输出otaku)
//
#include<stdio.h>
#include<stdlib.h>
int isVote(int *preferVec, int voteId, int referId){
int i;
for(i=0;;i++){
if(*(preferVec+i)==voteId) return 1;
if(*(preferVec+i)==referId) return 0;
}
}

int VoteResult(int **preferMat,int row, int col){
int voteId,referId,voteCount;
int i;
referId=0;
for(voteId=col-1;voteId>0;voteId--){
voteCount=0;
for(i=0;i<row;i++)
   voteCount+=isVote(*(preferMat+i),voteId,referId);
        if(2*voteCount>row)
            referId=voteId;
  }
    return referId;
}

int main(){
int **preferMat,row,col,i,j,k;
scanf("%d%d",&row,&col);
col++;
preferMat=(int **)malloc(row*sizeof(int *));
for(i=0;i<row;i++){
*(preferMat+i)=(int *)malloc(col*sizeof(int));
for(j=0;j<col;j++)
   scanf("%d",*(preferMat+i)+j);
}
k=VoteResult(preferMat,row,col);
if(k==0)
  printf("otaku");
    else
       printf("%d",k);
}

发表于 2016-08-31 10:37:35 回复(0)
#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;
int main() {
 int n, k;
 cin >> n >> k;
 vector<vector<int>> survey(n, vector<int>(k + 1, 0));
 vector<int> p(k + 1, 0);
 int i, j, m;
 for (i = 0; i < n; i++) {
  bool z = false;
  for (j = 0; j < k + 1; j++) {
   cin >> m;
   survey[i][j] = m;
   if (z == false) {
    if (m == 0)
     z = true;
    else {
     p[m]++;
    }
   }   
  }
 }
 vector<bool> pp(k + 1, false);
 for (i = 1; i <=k; i++) {
  if (p[i] / (double)n > 0.5) {
   pp[i] = true;
  }
 }
 for (i = n - 1; i >= 0; i--) {
  j = 0;
  
  while (j < k + 1 && survey[i][j] != 0) {
   if (pp[survey[i][j]] == false)
    survey[i][j] = -1;
   j++;
   
  }
  
 }
 vector<int> count(k+1, 0);
 for (i = 1;i<= k; i++) {
  int t = 0;
  if (pp[i] == true) {
   for (j = n-1; j >=0; j--) {
    
    m = 0;
    for (; m < k + 1; m++) {
     if (survey[j][m] == 0) {
      break;
     }
     if (survey[j][m] == i) {
      t++;
      survey[j][m] = -1;
      break;
     }
     else {
      if (survey[j][m] > i)
       break;
     }
    } 
   }
   if ((double)t / n > 0.5) {
    cout << i << endl;
    break;
   }
  }
 }
 if (i > k)
  cout << "otaku" << endl;
 system("pause");
 return 0;
}
数组越界的问题怎么解决? 

发表于 2016-05-16 19:49:47 回复(0)
 对于地点k,如果玩家i对于k的喜好程度更高(相较于在家),那么他就会在选择k的时候支持。若这样的i超过半数,则现在的默认地点就变为k
 * 当然,如果小于半数,默认地点不变 然后就依次向前面扫
发表于 2016-04-06 23:11:14 回复(0)
这是什么问题?我用了网上的编译器,output是正确的。为什么这里显示没有output?

测试用例:
6 1
0 1
1 0
1 0
0 1
0 1
1 0

对应输出应该为:
otaku

你的输出为:



import java.util.Scanner;
import java.io.*;
    
public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner in = new Scanner(System.in);
          
        int N = in.nextInt();
        int K = in.nextInt();
 
        int[][] priorityMaps = new int[N][K+1];
 
        for(int i=0; i<N; i++) {
            for(int j=0; j<=K; j++) {
                int priorityIndex = in.nextInt();
                priorityMaps[i][priorityIndex] = j;
            }
        }
 
        int candidate = 0;
        for(int i=K; i>0; i--) {
            int votes = 0;
            for(int j =0; j<N; j++) {
                if(priorityMaps[j][i] < priorityMaps[j][candidate]) {
                    votes++;
                }
            }
            if(votes > N/2) {
                candidate = i;
            }
        }
        if(candidate == 0) {
            System.out.print("otaku");
        } else {
        	System.out.print(candidate);
        }
    }
}

发表于 2016-03-11 00:56:27 回复(1)
答案错误:您提交的程序没有通过所有的测试用例

测试用例:
6 5
4 3 5 2 1 0
4 1 5 0 2 3
3 1 5 4 0 2
3 1 2 4 0 5
5 1 2 0 4 3
0 5 2 4 1 3

对应输出应该为:

5

你的输出为:

1



为什么是5不是1呢
发表于 2016-03-04 21:33:16 回复(1)
"otaku" 有一个点输出是这个,答案也是这个,但是却报错了
发表于 2015-11-22 14:14:49 回复(0)

问题信息

难度:
21条回答 8967浏览

热门推荐

通过挑战的用户

查看代码