PAT甲级2019年3月考题——A1158 TelefraudDetection25

Posted zzw1024

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级2019年3月考题——A1158 TelefraudDetection25相关的知识,希望对你有一定的参考价值。

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.

A person must be detected as a suspect if he/she makes more than K short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. A makes a short phone call to B means that the total duration of the calls from A to B is no more than 5 minutes.

Input Specification:
Each input file contains one test case. For each case, the first line gives 3 positive integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤103 10^310
3
?? )?? , the number of different phone numbers), and M (≤105 10^510
5
?? )?? , the number of phone call records). Then M lines of one day’s records are given, each in the format:

caller receiver duration
where caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes in a day.

Output Specification:
Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. The numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

If no one is detected, output None instead.

Sample Input 1:
5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1
Sample Output 1:
3 5
6
Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had made 5 short phone calls and didn’t exceed the threshold 5, and therefore is not a suspect.

Sample Input 2:
5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1
Sample Output 2:
None

【声明】

  由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。

Solution:  

  判断电话诈骗分子,当一个人与>k个不同的人打短时间【<= 5分钟】通话,且这些人中只有不超过20%的人给回了电话,那么这个人就是诈骗犯;

  当相互有通话的诈骗犯是认为同一组;

  输出按组内升序,组间按第一个组员升序的格式输出。 

  使用邻接矩阵记录通过时间【累计通话时间】,然后分别记录每个人满足要求的打出的电话人数以及回电话的人数,最后将相互通话的嫌疑犯视为一组。

  注重细节哦。

  

 1 #include <iostream>
 2 #include <vector>
 3 #include <map>
 4 using namespace std;
 5 int main()
 6 {
 7     int k, n, m, t;
 8     cin >> k >> n >> m;
 9     vector<int>call(n + 1, 0), back(n + 1, 0);//短电话打出去的记录和这些人打回进来的记录
10     vector<vector<int>>v(n + 1, vector<int>(n + 1, 0));//电话记录,记住记录的是累计的通话时间
11     while (m--)
12     {
13         int a, b, c;
14         cin >> a >> b >> c;
15         v[a][b] += c;//算累加通话时间的
16     }
17     for (int i =1; i <= n; ++i)
18     {
19         for (int j = 1; j <= n; ++j)
20         {
21             if (v[i][j]>0 && v[i][j] <= 5)//这通话时间算是短电话
22             {
23                 ++call[i];//计算i打出去的人数
24                 if (v[j][i] > 0)//这是j回给了i的
25                     ++back[i];//计算回给i的人数
26             }            
27         }
28     }    
29     vector<int>suspect, team(n + 1, 0);//统计诈骗犯的人数和初始化他们的团队号
30     for (int i = 1; i <= n; ++i)
31     {
32         if (call[i] > k && call[i] >= back[i]*5)//打出去的人数大于阈值k,回电话的人数不多于打出去的20%,注意
33         {
34             team[i] = i;//用来算同谋的
35             suspect.push_back(i);
36         }
37     }    
38     for (int i = 0; i < suspect.size(); ++i)//这里虽然是双重循环,但是诈骗犯的数量级很小的,所以这里应该不会超时间的,当然,超了的话,就用DFS即可
39         for (int j = i + 1; j < suspect.size(); ++j)
40             if (v[suspect[i]][suspect[j]] > 0 && v[suspect[j]][suspect[i]] > 0)//这两个诈骗人相互打过电话
41                 team[suspect[j]] = team[suspect[i]];
42     map<int, vector<int>>res;
43     for (int i = 1; i <= n; ++i)
44         if (team[i] > 0)
45             res[team[i]].push_back(i);//将属于同一伙的诈骗犯放一组
46     if (suspect.size() == 0)
47         printf("None");
48     else
49     {
50         for (auto ptr = res.begin(); ptr != res.end(); ++ptr)
51         {
52             for (int i = 0; i < ptr->second.size(); ++i)//由于遍历是从小到大的,所以不用排序
53                 printf("%s%d", i == 0 ? "" : " ", ptr->second[i]);
54             printf("
");
55         }
56     }
57     return 0;
58 }

 

以上是关于PAT甲级2019年3月考题——A1158 TelefraudDetection25的主要内容,如果未能解决你的问题,请参考以下文章

PAT(甲级)2019年春季考试题解

PAT(甲级)2019年春季考试 7-3 Telefraud Detection

PAT(甲级)2019年秋季考试 7-3 Postfix Expression

PAT(甲级)2019年秋季考试

PAT(甲级)2019年春季考试 7-2 Anniversary

PAT(甲级)2019年冬季考试 7-4 Cartesian Tree