首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> C++ 入門

HDU 5071 Chat:即時通訊模擬題完整解法

     編輯:C++ 入門

題目大意

模擬聊天列表操作:Add、Close、Chat、Rotate、Prior、Choose、Top、Untop。每個好友有優先級 U、聊天值 chat。需按指令維護列表,最後對有聊天記錄的好友輸出 Bye。

數據結構設計

  • 結構體 node { int p, y; long long x; }:p=優先級,y=是否刪除,x=聊天值。
  • 數組模擬隊列,mark 為隊尾,top 為置頂標記。
  • 所有操作均為 O(N) 掃描,N ≤ 50000,指令數 ≤ 50000,總複雜度可接受。

關鍵操作邏輯

指令處理
Add U隊尾插入,判重
Close U標記刪除,輸出聊天值,清除 top
Chat x當前聊天對象(top 或隊首)chat += x
Rotate x第 x 個移到隊首,調整 top
Prior最大優先級移到隊首
Choose U指定優先級移到隊首
Top U標記置頂,始終與其聊天
Untop取消置頂

收尾輸出

先輸出 top 的 Bye(若有聊天),再按隊列順序輸出未刪除且 chat>0 的好友。

完整代碼

#include <stdio.h>
#include <string.h>
struct node { int p, y; __int64 x; } data[50010];
int mark, top;
void Add() { ... }
void Close() { ... }
void Chat() { ... }
void Rotate() { ... }
void Prior() { ... }
void Choose() { ... }
void Top() { ... }
void Untop() { ... }
int main() {
    int Case, ii, i, n; char str[101];
    scanf("%d", &Case);
    while (Case--) {
        scanf("%d", &n); mark = 0; top = 0; memset(data, 0, sizeof(data));
        for (ii = 1; ii <= n; ii++) {
            scanf("%s", str);
            printf("Operation #%d: ", ii);
            if (strcmp(str, "Add") == 0) Add();
            else if (strcmp(str, "Close") == 0) Close();
            else if (strcmp(str, "Chat") == 0) Chat();
            else if (strcmp(str, "Rotate") == 0) Rotate();
            else if (strcmp(str, "Prior") == 0) Prior();
            else if (strcmp(str, "Choose") == 0) Choose();
            else if (strcmp(str, "Top") == 0) Top();
            else if (strcmp(str, "Untop") == 0) Untop();
            printf(".\n");
        }
        if (top && data[top].x) printf("Bye %d: %I64d\n", data[top].p, data[top].x);
        for (i = 1; i <= mark; i++)
            if (!data[i].y && data[i].x) printf("Bye %d: %I64d\n", data[i].p, data[i].x);
    }
    return 0;
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved