PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】
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%的人给回了电话,那么这个人就是诈骗犯;
当相互有通话的诈骗犯是认为同一组;
输出按组内升序,组间按第一个组员升序的格式输出。
使用邻接矩阵记录通过时间【累计通话时间】,然后分别记录每个人满足要求的打出的电话人数以及回电话的人数,最后将相互通话的嫌疑犯视为一组。
注重细节哦。
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
int k, n, m, t;
cin >> k >> n >> m;
vector<int>call(n + , ), back(n + , );//短电话打出去的记录和这些人打回进来的记录
vector<vector<int>>v(n + , vector<int>(n + , ));//电话记录,记住记录的是累计的通话时间
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
v[a][b] += c;//算累加通话时间的
}
for (int i =; i <= n; ++i)
{
for (int j = ; j <= n; ++j)
{
if (v[i][j]> && v[i][j] <= )//这通话时间算是短电话
{
++call[i];//计算i打出去的人数
if (v[j][i] > )//这是j回给了i的
++back[i];//计算回给i的人数
}
}
}
vector<int>suspect, team(n + , );//统计诈骗犯的人数和初始化他们的团队号
for (int i = ; i <= n; ++i)
{
if (call[i] > k && call[i] >= back[i]*)//打出去的人数大于阈值k,回电话的人数不多于打出去的20%,注意
{
team[i] = i;//用来算同谋的
suspect.push_back(i);
}
}
for (int i = ; i < suspect.size(); ++i)//这里虽然是双重循环,但是诈骗犯的数量级很小的,所以这里应该不会超时间的,当然,超了的话,就用DFS即可
for (int j = i + ; j < suspect.size(); ++j)
if (v[suspect[i]][suspect[j]] > && v[suspect[j]][suspect[i]] > )//这两个诈骗人相互打过电话
team[suspect[j]] = team[suspect[i]];
map<int, vector<int>>res;
for (int i = ; i <= n; ++i)
if (team[i] > )
res[team[i]].push_back(i);//将属于同一伙的诈骗犯放一组
if (suspect.size() == )
printf("None");
else
{
for (auto ptr = res.begin(); ptr != res.end(); ++ptr)
{
for (int i = ; i < ptr->second.size(); ++i)//由于遍历是从小到大的,所以不用排序
printf("%s%d", i == ? "" : " ", ptr->second[i]);
printf("\n");
}
}
return ;
}
PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】的更多相关文章
- PAT甲级【2019年3月考题】——A1157 Anniversary【25】
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...
- PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】
7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...
- PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】
7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...
- PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】
7-2 Merging Linked Lists (25 分) Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n L1=a1→a ...
- PAT甲级【2019年9月考题】——A1160 Forever【20】
7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...
- PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】
Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “si ...
- PAT甲级2019冬季考试题解
A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
随机推荐
- LeetCode #237. Delete Node in a Linked List 删除链表中的节点
https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 非常巧妙的一道题. 题目没有给head,心想没有head我怎么才能找到要删 ...
- Java 8实战之读书笔记四:高效Java 8编程
三.高效Java 8编程 第8章 重构.测试和调试 Java 8的新特性也可以帮助提升代码的可读性: 使用Java 8,你可以减少冗长的代码,让代码更易于理解 通过方法引用和S ...
- LuaLuaMemorySnapshotDump-master
https://codeload.github.com/yaukeywang/LuaMemorySnapshotDump/zip/master
- Tomcat的用途
总结: 这篇文章主要反思了Tomcat的作用.本文主要是自己的一个思考过程,不是严谨地介绍和详细总结Tomcat使用方法的文章.最后尝试利用tomcat的知识,以URL的形式来访问文件夹(在浏览器的地 ...
- shell判断/bin目录下date文件是否存在
- idea模块搭建新手党常见错误
一.搭建java和web模块会出现的错误(此篇以分布式模块为例) 1.创建空工程 1.点击file ,在弹出的窗口左侧选项中在最后有一个Empty Project选项.此处就是创建空工程. 2.在此空 ...
- python常用函数 F
filter(callable, list/tuple) 接收一个函数和一个序列,完成元素过滤. 例子: fnmatch(str,str) 使用底层操作系统的大小写敏感规则来匹配模式. 例子: fnm ...
- NodeJs初相识
一.nodeJs简介 1.Node 是一个服务器端 JavaScript 解释器. 2.Node 的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处理数万条同时连接到一个物理机的连接代码.处理高并 ...
- html5 像素模拟渐变
代码实例: <!DOCTYPE html> <html> <head> <style> canvas{ background:#eee; } </ ...
- Flutter pubspec.yaml配置文件
name: flutter_app1 # 应用名称 description: A new Flutter application. # 应用描述 # The following defines the ...