当前位置:网站首页>【练习-10】 Unread Messages(未读消息)
【练习-10】 Unread Messages(未读消息)
2022-07-06 09:26:00 【火焰车】
题目描述
There is a group of people in an internet email message group. Messages are sent to all members of the group, and no two messages are sent at the same time.
Immediately before a person sends a message, they read all their unread messages up to that point.
Each sender also reads their own message the moment it is sent. Therefore, a person’s unread messages are exactly the set of messages sent after that person’s last message.
Each time a message is sent, compute the total number of unread messages over all group members.
输入
The first line of input contains two integers n (1 ≤ n ≤ 109 ) and m (1 ≤ m ≤ 1,000), where n is the number of people in the group, and m is the number of messages sent. The group members are
identified by number, 1 through n.
Each of the next m lines contains a single integer s (1 ≤ s ≤ n), which is the sender of that message. These lines are in chronological order.
输出
Output m lines, each with a single integer, indicating the total number of unread messages over all group members, immediately after each message is sent.
样例输入
【样例1】
2 4
1
2
1
2
【样例2】
3 9
1
2
3
2
1
3
3
2
1
样例输出
【样例1】
1
1
1
1
【样例2】
2
3
3
4
3
3
5
4
3
题目大意:
有n个人发m次消息(每次只有一个人发),接下来m行没行输入是第几个人发的。
当一个人发消息时,其他所有人都会有一条未读消息,当一个人发消息时,他的未读消息会清0.
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define CLEAR(a) memset(a,0,sizeof a);
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
map<ll,ll> mp;
int main()
{
ll n,m,x,res=0;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>x;
res+=(n-(i-mp[x]));
mp[x]=i;
cout<<res<<endl;
}
return 0;
}
思路:
因为发一条消息时,除了自己每个人的未读消息都会+1。那不妨假设每个人都会+1,也就是说未读消息总数+n。
然后我们再减去发消息的人已经看了的消息,用map记录上一次看消息是什么时候,用i-mp[x]就是已经看了的消息(不需要额外-1,因为A发消息时A也+1了)。
那么只要加上n-(i-mp[x])就是未读消息的量了。
边栏推荐
- Cost accounting [15]
- SSM框架常用配置文件
- Opencv learning log 33 Gaussian mean filtering
- JS --- detailed explanation of JS DOM (IV)
- LeetCode#36. Effective Sudoku
- C语言数组的概念
- 信息安全-威胁检测引擎-常见规则引擎底座性能比较
- ucore lab 6
- Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
- 学习记录:STM32F103 时钟系统概述工作原理
猜你喜欢

Nodejs+vue网上鲜花店销售信息系统express+mysql

Learning record: understand systick system timer and write delay function

C语言是低级和高级的分水岭

ucore lab7

学习记录:TIM—电容按键检测

LeetCode#19. Delete the penultimate node of the linked list

学习记录:使用STM32外部输入中断

Matlab example: two expressions of step function

JS --- BOM details of JS (V)

Learning record: Tim - Basic timer
随机推荐
Eslint--- error: newline required at end of file but not found (EOL last) solution
Research Report on shell heater industry - market status analysis and development prospect forecast
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Opencv learning log 19 skin grinding
Cost accounting [13]
UCORE Lab 1 system software startup process
Optimization method of path problem before dynamic planning
China's PCB connector market trend report, technological innovation and market forecast
学习记录:如何进行PWM 输出
Cost accounting [16]
Indonesian medical sensor Industry Research Report - market status analysis and development prospect forecast
数据在内存中的存储&载入内存,让程序运行起来
Research Report on market supply and demand and strategy of China's earth drilling industry
LeetCode#19. Delete the penultimate node of the linked list
STM32学习记录:玩转按键控制蜂鸣器和LED
Cost accounting [20]
Opencv learning log 16 paperclip count
学习记录:TIM—电容按键检测
JS --- detailed explanation of JS DOM (IV)
Research Report on medical toilet industry - market status analysis and development prospect forecast