PAT(甲级)2021年冬季考试 7-1 Fake News

7-1
Fake News

注意点:
1.不能直接用暴力求解,将每个数和每个数比较一次(两层循环),只要计算总数-该数字出现的次数就能求得次数(一层循环),否则会超时。
2.计算数字出现的次数,一开始直接用一个int数组做hash映射,出现了错误,因为出现的数字中有负数,因此还是老老实实用map函数做映射。

(20分)
No news site is unbiased, but some do a better job of trying to balance facts and opinions. The term fake news means “news articles that are intentionally and verifiably false” designed to manipulate people’s perceptions of real facts, events, and statements. (Quoted from https://www.cits.ucsb.edu/fake-news/what-is-fake-news)

To tell if a news media is more or less likely to be fake, you can keep an eye on several different sites to see how they report on the same important events. The algorithm works as the following:

Select N news sites.
For each important event, scan every site and represent each different opinion by a distinct integer.
Define the likelihood of a site i being fake news by Fi​=ni​/N, where ni​ is the number of sites that have different opinions than site i.
Find the news site(s) which is(are) the most likely to report fake news.
For each news site, count the number of times it was the most likely to be fake, and find the one that is in most of the cases the most likely to be fake.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers: N (≤104) which is the number of news sites, and M (≤100) which is the number of events. Then M lines follow, each describes the reports of the sites in the format:

R1​ R2​ … RN​

where Ri​ is an integer in the range [−104,104], and reprensts the opinion of site i.

Output Specification:
For each test case, print in a line the index of the site which is in most of the cases the most likely to be fake. The answer is guaranteed to be unique.

Sample Input:
4 6
4 2 7 7
1 1 1 3
2 9 9 5
-1 -1 -1 -1
-2 2 -2 2
1 1 3 4
Sample Output:
4
Hint:
The Fi​’s for each event are the following:

Event 1: 3/4 3/4 2/4 2/4 --> 1 and 2 are the most likely
Event 2: 1/4 1/4 1/4 3/4 --> 4 is the most likely
Event 3: 3/4 2/4 2/4 3/4 --> 1 and 4 are the most likely
Event 4: 0 0 0 0 --> all are the most likely
Event 5: 2/4 2/4 2/4 2/4 --> all are the most likely
Event 6: 2/4 2/4 3/4 3/4 --> 3 and 4 are the most likely
Hence site 4 is the one since it has the highest likelihood for 5 times, while other sites only have 3 or 4 times.
代码

#include<stdio.h>
#include<map>
using namespace std;

const int maxn = 10011;
int main(){
    int N, M, c[maxn]={0};
    scanf("%d %d", &N, &M);
    for(int i = 0; i < M; i++){
        int temp[maxn], count[maxn] = {0};
        map<int, int> vis;
        for(int j = 0; j < N; j++){
            scanf("%d", &temp[j]);
            vis[temp[j]]++;
        }
        int mostlike = -1;

        for(int j = 0; j < N; j++){
        	count[j] = N - vis[temp[j]];
            if(count[j] > mostlike){
                mostlike = count[j];
            }
		}
        for(int j = 0; j < N; j++){
           if(count[j] == mostlike){
            	c[j]++;
			}
        }
    }

    int mostindex = 0;

    for(int i = 0; i < N; i++){
        if(c[i] > c[mostindex]){
            mostindex = i;
        }
    }
    printf("%d", mostindex + 1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值