題目 Problem#
題目連結:https://zerojudge.tw/ShowProblem?problemid=c421
敘述 Description#
雲端列印服務公司提出一個新型服務。
該公司有 n 台 3D 印表機,
其中印表機 P1,P2, …, Pk 用以優先服務最為重要客戶,
印表機 Pk+1, Pk+2, …, Pn 列印速度較慢,用以優先服務一般客戶。
每個客戶依該年度所選擇服務等級及所繳交費用可有不同的列印優先權, 以 1, …, 10000 表示之;
10000 代表最高列印優先權,1 代表最低列印優先權。
為了不讓低列印優先權的客戶永無止盡的等待,
印表機 P1, P2, …, Pk 一旦有空,等待的工作中優先權最高的工作就會被交付列印;
而印表機 Pk+1, Pk+2, …, Pn一旦有空, 等待的工作中優先權最低的工作就會被交付列印。
請寫一個程式列舉交付列印工作的順序。
輸入 Input#
輸入只有一行,共有不定數量的整數,
整數可為{-2, -1, 0, 1, 2, …, 10000},兩整數之間以一個空白隔開。
-2 表示印表機 P1, P2, …, Pk其中一台有空, 可以列印最高優先權的工作;
-1 表示印表機 Pk+1, Pk+2, …, Pn其中一台有空, 可以列印最低優先權的工作;1, 2, …, 10000
代表新增一個優先權為該數字之工作;
0 則代表輸入結束。
若輸入為 -1 或 -2但無等待列印的工作,則不列印,需等待下一個 -1 或 -2 才再列印新的工作。
輸出 Output#
請依被列印工作的順序,輸出該工作的優先權代號,之後緊接著一個空白。
尚未交付列印的工作不需輸出。
範例輸入 Sample Input#
// Example 1
20 15 10 -2 -1 -1 0
// Example 2
1 2 3 -2 4 5 6 -1 7 0
範例輸出 Sample Output#
// Example 1
20 10 15
// Example 2
3 1
提示 Hint#
本題共有五組測試資料。每組可有多個輸入檔案,全部答對該組才得分。 第一組 15 分,最多 11 個工作需要被列印,且只有一次交付列印指令。 第二組 15 分,最多 20 個工作需要被列印,且只有二次交付列印指令。 第三組 20 分,最多 50 個工作需要被列印,且最多有 25 次交付列印指令。 第四組 20 分,最多 15,000 個工作需要被列印或交付列印。 第五組 30 分,最多 500,000 個工作需要被列印或交付列印。
使用 C++作答的同學,請在程式碼開頭加上#include,並利用 scanf讀入資料。
使用cin讀入資料可能會因為讀入效率太差以致於程式執行時間超過限制。
題解 Solution#
multiset 的 erase 如果是代入數字,他不會是刪除一個,而是那個數字全部刪除 = =
所以要代入的是 iterator,也就是位置
multiset 會自動幫我們排序好,由小至大
m.begin(),就是最小。
m.rbegin(),就是反向的開始,就是最尾端,也就是最大。
可是 erase 只能代入 iterator,不能是 reverse_iterator
所以還要再把位址轉過來(或者你直接 end(),做遞減運算子也可以)
程式碼 Accepted Code#
/*
Author: Jerry Yang C.H. (tico88612)
Date: 2019/11/22
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<double,double> pdd;
#define SQ(i) ((i)*(i))
#define MEM(a, b) memset(a, (b), sizeof(a))
#define SZ(i) int(i.size())
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define REP1(i,j) FOR(i, 1, j+1, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define ALL(_a) _a.begin(),_a.end()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define X first
#define Y second
#ifdef tmd
#define debug(...) do{\
fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
_do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<" ,";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
_s<<"{";
for(It _it=_ita;_it!=_itb;_it++)
{
_s<<(_it==_ita?"":",")<<*_it;
}
_s<<"}";
return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,deque<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
#endif
const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int iNF = 0x3f3f3f3f;
// const ll MAXN =
/********** Good Luck :) **********/
int main()
{
IOS();
int enter;
int fir = 1;
multiset<int> m;
while(cin >> enter && enter){
if(enter > 0){
m.insert(enter);
}
else if(enter == -1){
if(SZ(m) > 0){
if(!fir)
cout << " ";
else
fir = 0;
cout << *m.begin();
m.erase(m.begin());
}
}
else if(enter == -2){
if(SZ(m) > 0){
if(!fir)
cout << " ";
else
fir = 0;
cout << *m.rbegin();
m.erase(next(m.rbegin()).base());
}
}
}
cout << '\n';
return 0;
}
後記 Afterword#
這題也可以用 multiset 完成!