UVA 230 Borrowers

题目链接:https://vjudge.net/problem/UVA-230

题目翻译摘自《算法禁赛入门经典》

题目大意

  你的任务是模拟一个图书管理系统。首先输入若干图书的标题和作者(标题各不相同, 以END结束),然后是若干指令:BORROW 指令表示借书,RETURN 指令表示还书,SHELVE 指令表示把所有已归还但还未上架的图书排序后依次插入书架并输出图书标题和插入位置(可能是第一本书或者某本书的后面)。

  图书排序的方法是先按作者从小到大排,再按标题从小到大排。在处理第一条指令之 前,你应当先将所有图书按照这种方式排序。

分析

  有序表加二分。

代码如下

UVA 230 Borrowers
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49 
 50 inline int gc(){
 51     static const int BUF = 1e7;
 52     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 53     
 54     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 55     return *bg++;
 56 } 
 57 
 58 inline int ri(){
 59     int x = 0, f = 1, c = gc();
 60     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 61     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 62     return x*f;
 63 }
 64 
 65 template<class T>
 66 inline string toString(T x) {
 67     ostringstream sout;
 68     sout << x;
 69     return sout.str();
 70 }
 71 
 72 inline int toInt(string s) {
 73     int v;
 74     istringstream sin(s);
 75     sin >> v;
 76     return v;
 77 }
 78 
 79 //min <= aim <= max
 80 template<typename T>
 81 inline bool BETWEEN(const T aim, const T min, const T max) {
 82     return min <= aim && aim <= max;
 83 }
 84  
 85 typedef long long LL;
 86 typedef unsigned long long uLL;
 87 typedef pair< double, double > PDD;
 88 typedef pair< int, int > PII;
 89 typedef pair< int, PII > PIPII;
 90 typedef pair< string, int > PSI;
 91 typedef pair< int, PSI > PIPSI;
 92 typedef set< int > SI;
 93 typedef set< PII > SPII;
 94 typedef vector< int > VI;
 95 typedef vector< double > VD;
 96 typedef vector< VI > VVI;
 97 typedef vector< SI > VSI;
 98 typedef vector< PII > VPII;
 99 typedef map< int, int > MII;
100 typedef map< int, string > MIS;
101 typedef map< int, PII > MIPII;
102 typedef map< PII, int > MPIII;
103 typedef map< string, int > MSI;
104 typedef map< string, string > MSS;
105 typedef map< PII, string > MPIIS;
106 typedef map< PII, PII > MPIIPII;
107 typedef multimap< int, int > MMII;
108 typedef multimap< string, int > MMSI;
109 //typedef unordered_map< int, int > uMII;
110 typedef pair< LL, LL > PLL;
111 typedef vector< LL > VL;
112 typedef vector< VL > VVL;
113 typedef priority_queue< int > PQIMax;
114 typedef priority_queue< int, VI, greater< int > > PQIMin;
115 const double EPS = 1e-8;
116 const LL inf = 0x7fffffff;
117 const LL infLL = 0x7fffffffffffffffLL;
118 const LL mod = 1e9 + 7;
119 const int maxN = 1e4 + 7;
120 const LL ONE = 1;
121 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
122 const LL oddBits = 0x5555555555555555;
123 
124 struct Book{
125     string title, author;
126     
127     Book() {}
128     Book(string s) {
129         title = s.substr(0, s.find('\"', 1) + 1);
130         author = s.substr(s.find('\"', 1) + 5);
131     }
132     
133     bool operator< (const Book &x) const {
134         return author < x.author || author == x.author && title < x.title;
135     }
136     
137     bool operator> (const Book &x) const {
138         return x < *this;
139     }
140 };
141 vector< Book > books;
142 
143 struct MyCmp{
144     bool operator() (const int &x, const int &y) const {
145         return books[x] < books[y];
146     }
147 };
148 
149 bool cmp(const int &x, const int &y) {
150     return books[y] < books[x];
151 }
152 
153 string tmp;
154 set< int, MyCmp > sb; // 记录在架子上的书id 
155 set< int, MyCmp > bb; // 记录已归还但未上架的书id 
156 MSI tid; // title->book_id
157 
158 int main(){
159     //freopen("MyOutput.txt","w",stdout);
160     //freopen("input.txt","r",stdin);
161     //INIT();
162     while(getline(cin, tmp)) {
163         if(tmp == "END") break;
164         books.PB(Book(tmp));
165         tid[books.back().title] = books.size() - 1;
166         sb.insert(books.size() - 1);
167     }
168     
169     while(getline(cin, tmp)) {
170         if(tmp == "END") break;
171         
172         if(tmp[0] == 'B') {
173             int id = tid[tmp.substr(tmp.find(' ') + 1)];
174             sb.erase(id);
175         }
176         else if(tmp[0] == 'R'){
177             int id = tid[tmp.substr(tmp.find(' ') + 1)];
178             bb.insert(id);
179         }
180         else {
181             foreach(i, bb) {
182                 auto it = lower_bound(sb.rbegin(), sb.rend(), *i, cmp); // 查找第一本小于等于books[*i]的书 
183                 if(it == sb.rend()) printf("Put %s first\n", books[*i].title.c_str());
184                 else printf("Put %s after %s\n", books[*i].title.c_str(), books[*it].title.c_str());
185                 sb.insert(*i);
186             }
187             bb.clear();
188             printf("END\n");
189         }
190     }
191     return 0;
192 }
View Code

 

上一篇:【UVA - 10474 】Where is the Marble?(排序)


下一篇:UVA 548 Tree