问题 P: Unread Messages时间限制: 1 Sec 内存限制: 128 MB [

题目描述

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.

题目描述

有n个人,m天,每一天都会有人向其他所有人发送一封信件,发送信件的人会瞬间读取没有读取的信件(包括自己发出的这一封),求每一天每一个人没有读取的信件的总和

思路分析

每一天都会有人发送信件,但是同一天只有一个人发送信件, 那么每一天都会有n-1个信件发出, 所以第i天没有发出的总信件就是 i- 1 天没有读取的信件的总和 再减去,第i个人读取的信件(一个人发出信件会瞬间读取所有未读信件)

[如何存储第i个人上一次读取信件的时间(本体数据范围比较大,所以用map的映射关系)]利用map映射关系 键表示第i个人上一次读取信件的时间

样例输入 [Copy](javascript:CopyToClipboard($('#sampleinput').text()))

【样例1】
2 4
1
2
1
2
【样例2】
3 9
1
2
3
2
1
3
3
2
1

样例输出 [Copy](javascript:CopyToClipboard($('#sampleoutput').text()))

【样例1】
1
1
1
1
【样例2】
2
3
3
4
3
3
5
4
3

提交状态

#include <iostream>
#include <cstring>
#include <algorithm>
#include<map>
using namespace std;
const int N = 1e3 + 10;
map<int, int> mp;
//mp[x] = t
//代表的意思的是第x个人最后一次发送邮件的天数
int main()
{
    int n, m;
    cin >> n >> m;
    long long  ans = 0;
    //每一天需要根据上一天的ans来判断这一次的值
    //需要特判第一天的情况
    int x;
    cin >> x;
    mp[x] = 1;
    ans += n - 1;//一共有其他n-1个人没有读信件
    cout << ans << endl;
    for(int i = 2; i <= m; i ++ )
    {
        int x;
        cin >> x;//轮到第x个人发信件
        ans += n - 1;//产生了n-1封未读邮件
        
        //再减去已读邮件
        
        if(mp[x] == 0)//之前没有发送过信件
        {
            ans -= i - 1;//多减去1是因为发送信件的人会读取自己发送的邮件,下面也是一样
        }
        else 
        {
            ans -= i - mp[x] - 1;
        }
        //更新mp[数组]
        mp[x] = i;
        cout << ans << endl;
    }
    return 0;
}
posted @ 2021-04-11 20:46  梨花满地  阅读(432)  评论(0)    收藏  举报