您的位置:首页 > 其它

电话聊天狂人 【STL】

2018-03-26 21:41 274 查看
7-2 电话聊天狂人(25 分)

给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。

输入格式:

输入首先给出正整数N(≤10​5​​),为通话记录条数。随后N行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。

输出格式:

在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。

输入样例:

4

13005711862 13588625832

13505711862 13088625832

13588625832 18087925832

15005713862 13588625832

输出样例:

13588625832 3

思路

每次用MAP 标记打电话的号码 然后 每次也更新 最大值 和每个值的次数

然后最好用整型读入 因为用string 用cin 读入的话 会超时

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;

const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

int main()
{
int n;
map <ll, int> m;
map <int, int> vis;
ll n1, n2, num = 999999999999;
cin >> n;
int ans = INT_MIN;
for (int i = 0; i < n; i++)
{
scanf("%lld%lld", &n1, &n2);
m[n1]++;
m[n2]++;
vis[m[n1]]++;
vis[m[n2] - 1]--;
vis[m[n2]]++;
vis[m[n1] - 1]--;
if (m[n1] > ans || (m[n1] == ans && n1 < num))
{
ans = m[n1];
num = n1;
}
if (m[n2] > ans || (m[n2] == ans && n2 < num))
{
ans = m[n2];
num = n2;
}
}
printf("%011lld %d", num, ans);
if (vis[ans] > 1)
printf(" %d", vis[ans]);
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: