本文是我对第五章16道习题的练习总结,建议配合紫书——《算法竞赛入门经典(第2版)》阅读本文。
另外为了方便做题,我在VOJ上开了一个contest,欢迎一起在上面做:第五章习题contest
如果想直接看某道题,请点开目录后点开相应的题目!!!
思路
此题的重点在于读入数据部分,读取每一行我用了getline,然后再用stringstream将该行数据分割成字符串vector数组。请读者自行看代码体会。
输出就比较简单了,不足最大长度的用空格补齐。
代码
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1000;
const int LEN = 180;
int n;
vector<string> vs[N];
int w[LEN];
int main(void)
{
n = 0;
string line, s;
memset(w, 0, sizeof(w));
while (getline(cin, line)) {
stringstream stm(line);
int i = 0;
while (stm >> s) {
w[i] = max(w[i], (int)(s.size()));
i ++;
vs[n].push_back(s);
}
n ++;
}
for (int i = 0; i < n; i ++) {
int k = vs[i].size();
for (int j = 0; j < k-1; j ++) {
cout << vs[i][j];
for (int r = 0; r <= w[j]-vs[i][j].size(); r ++)
printf(" ");
}
cout << vs[i][k-1] << endl;
}
return 0;
}
思路
没看出来本题和本章主题C++与STL有什么联系。
思路很简单,循环1000步,每步检查是否到终止条件(全0),提前达到终止条件则为ZERO,否则为LOOP。
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 15;
int n, a[N];
bool is_zero()
{
for (int i = 0; i < n; i ++) {
if (a[i]) return false;
}
return true;
}
void change()
{
int tmp = a[0];
for (int i = 0; i < n-1; i ++)
a[i] = abs(a[i] - a[i+1]);
a[n-1] = abs(a[n-1] - tmp);
}
int main(void)
{
int t;
cin >> t;
while (t --) {
cin >> n;
for (int i = 0; i < n; i ++)
scanf("%d", &a[i]);
int loop = 1001;
while (loop--) {
if (is_zero()) break;
change();
}
if (is_zero()) puts("ZERO");
else puts("LOOP");
}
return 0;
}
思路
看题目描述就能知道是一个队列,具体细节看代码吧。
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int main(void)
{
int n;
while (cin >> n && n) {
queue<int> q;
for (int i = 1; i <= n; i ++)
q.push(i);
printf("Discarded cards:");
while (q.size() > 2) {
printf(" %d,", q.front());
q.pop();
q.push(q.front());
q.pop();
}
if (q.size() == 2) {
printf(" %d", q.front());
q.pop();
}
printf("\n");
printf("Remaining card:");
printf(" %d\n", q.front());
}
return 0;
}
思路
记P(A,B)表示一个想从A校换到B校的学生,数组a[N]和b[N](元素类型为P)表示所有学生集合(初始内容相同)。
定义两种比较函数:cmp1表示以元素first为第一排序依据,second为第二排序依据,排序顺序都是从小到大;cmp2则是以second为第一依据,first为第二依据。
a和b分别用cmp1和cmp2排序后,如果a和b的任意第i个元素互相能对应起来,也就是满足代码中的条件:
a[i].first == b[i].second && a[i].second == b[i].first
则交换可以进行。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int N = 500000;
int n;
P a[N], b[N];
bool cmp1(P p1, P p2)
{
if (p1.first != p2.first) return p1.first < p2.first;
return p1.second < p2.second;
}
bool cmp2(P p1, P p2)
{
if (p1.second != p2.second) return p1.second < p2.second;
return p1.first < p2.first;
}
int main(void)
{
while (cin >> n && n) {
for (int i = 0; i < n; i ++)
scanf("%d%d", &a[i].first, &a[i].second);
memcpy(b, a, sizeof(a));
sort(a, a+n, cmp1);
sort(b, b+n, cmp2);
bool flag = true;
for (int i = 0; i < n; i ++) {
if (!(a[i].first == b[i].second && a[i].second == b[i].first))
{ flag = false; break;}
}
if (flag) puts("YES");
else puts("NO");
}
return 0;
}
思路
有两种方法可以寻找复合词:
1、枚举所有两个词的组合,查找是否则词典中;
2、枚举所有词,拆分词为s1和s2查找是否都在词典中。
词典的规模是120000,显然第二种时间复杂度低。
尽管思路很清晰,但我还是WA了很多次,最后发现是因为在打印复合词之后未break,这可能导致某个复合词被打印多次。而这个错误在示例数据中检测不出来。
这个题也可以用set,代码写起来会简单一些。
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int N = 120001;
int n;
string s[N];
int main(void)
{
n = 0;
while (cin >> s[n]) n++;
s[n] = "";
string s1, s2;
for (int i = 0; i < n; i ++) {
int m = s[i].size();
for (int j = 1; j < m; j ++) {
s1 = s[i].substr(0, j);
s2 = s[i].substr(j);
int k1 = lower_bound(s, s+n, s1) - s;
int k2 = lower_bound(s, s+n, s2) - s;
if (s[k1] == s1 && s[k2] == s2) {
cout << s[i] << endl;
break;
}
}
}
return 0;
}
思路
与5-4题有相似之处,也是两个cmp函数分别排序,检查排序后的数组是否匹配。详见代码中cmp函数以匹配的写法,读者自行体会。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int N = 1000;
int n;
P a[N], b[N];
bool cmp1(P p1, P p2)
{
if (p1.first != p2.first) return p1.first < p2.first;
return p1.second < p2.second;
}
bool cmp2(P p1, P p2)
{
if (p1.first != p2.first) return p1.first > p2.first;
return p1.second < p2.second;
}
int main(void)
{
int t;
cin >> t;
while (t --) {
cin >> n;
for (int i = 0; i < n; i ++)
scanf("%d%d", &a[i].first, &a[i].second);
memcpy(b, a, sizeof(a));
sort(a, a+n, cmp1);
sort(b, b+n, cmp2);
int mid2 = a[0].first + a[n-1].first;
bool flag = true;
for (int i = 0; i <= n/2; i ++) {
if (!(a[i].first + b[i].first == mid2 && a[i].second == b[i].second))
{ flag = false; break;}
}
if (flag) puts("YES");
else puts("NO");
}
return 0;
}
思路
这个题毫无疑问可以用队列实现,问题是如何判断某任务是否能够打印。
根据题意,某任务能够打印的条件是队列中没有更优先的任务。由于优先级只有9个,我们可以定义一个元素个数为10的数组jobs,jobs[i]表示队列中优先级为i的任务的个数。每个任务打印后,其优先级i对应的jobs[i]减1。而优先级为i的任务能够打印得条件是对于所有大于i的j,job[j]均为0。
还有一个小问题是每个任务如何在队列中表示?优先级信息k肯定是需要的,而且还需要知道任务的id(事实上我们只关心要求的那个id)。所以每个任务的信息可以用id*10+k表示。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int jobs[10];
queue<int> q;
void init()
{
memset(jobs, 0, sizeof(jobs));
while(q.size()) q.pop();
}
bool can_print(int k)
{
for (int i = k+1; i <= 9; i++) {
if (jobs[i]) return false;
}
return true;
}
int main()
{
int kase, n, m, k, t;
scanf("%d", &kase);
while (kase--) {
scanf("%d%d", &n, &m);
init();
for (int i = 0; i < n; i++) {
scanf("%d", &k);
q.push(i*10 + k);
jobs[k]++;
}
t = 0;
while(q.size()) {
k = q.front();
q.pop();
if (can_print(k%10)) {
t++;
jobs[k%10]--;
if (k/10 == m) break;
} else
q.push(k);
}
printf("%d\n", t);
}
return 0;
}
思路
本题涉及大量的有序插入和删除操作,用堆结构的set能够使时间复杂度最低(O(logN))。
介绍一下代码中使用的数据结构:
set bks1表示架上图书集合;
set bks2表示已归还但未上架的图书集合;
map< string, string > mp表示标题与作者的映射;
其它内容均体现在代码中。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
struct Book {
string name, author;
bool operator< (Book b) const {
if (author != b.author) return author < b.author;
return name < b.name;
}
};
map<string, string> mp;
set<Book> bks1;
set<Book> bks2;
int main()
{
string s;
Book b;
while (getline(cin, s) && s != "END") {
int k = s.find('"', 1);
b.name = s.substr(0, k+1);
b.author = s.substr(k+5);
bks1.insert(b);
mp[b.name] = b.author;
}
while (getline(cin, s) && s != "END") {
if (s[0] == 'S') {
set<Book>::iterator it1, it2;
for (it2 = bks2.begin(); it2 != bks2.end(); it2++) {
it1 = bks1.lower_bound(*it2);
cout << "Put " << it2->name;
if (bks1.empty() || it1 == bks1.begin())
cout << " first" << endl;
else
cout << " after " << (--it1)->name << endl;
bks1.insert(*it2);
}
bks2.clear();
cout << "END" << endl;
} else {
b.name = s.substr(7);
b.author = mp[b.name];
if (s[0] == 'B')
bks1.erase(b);
else if (s[0] == 'R')
bks2.insert(b);
}
}
return 0;
}
思路
几个数据结构的说明:
lens表示每个id(即数组)的长度;
ids表示数组名对应的id;
arrs表示每个数组中元素的赋值情况。
对于题意的说明:
1、数组一定是定义过而且只定义过一次的;
2、数组定义行的size部分是一个数字常量,数组中某元素的赋值行则不一定;
3、bug只有两种情况:一是下标index越界,二是使用未初始化的变量(index和value都可能出现这种情况)
代码
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
map<int, int> lens;
map<string, int> ids;
vector<map<int, int> > arrs;
void init_datas()
{
lens.clear();
ids.clear();
arrs.clear();
}
int ID(string s)
{
if (ids.count(s)) return ids[s];
map<int, int> mp;
arrs.push_back(mp);
return ids[s] = arrs.size()-1;
}
int get_val(string s)
{
int p = s.find('[');
if (p == string::npos) return atoi(s.c_str());
int sid = ID(s.substr(0, p));
int id = get_val(s.substr(p+1, s.size()-p-2));
if (id >= 0 && id < lens[sid] && arrs[sid].count(id))
return arrs[sid][id];
return -1;
}
bool have_bug(string s)
{
int k = s.find('=');
int sid, id, val;
int p = s.find('[');
sid = ID(s.substr(0, p));
if (k == string::npos) {
stringstream ss(s.substr(p+1));
ss >> val;
lens[sid] = val;
return false;
}
id = get_val(s.substr(p+1, k-p-2));
val = get_val(s.substr(k+1));
if (id >= 0 && id < lens[sid] && val >= 0) {
arrs[sid][id] = val;
return false;
}
return true;
}
int main(void)
{
string s;
while (cin >> s && s != ".") {
init_datas();
int bug = 0, line = 0;
do {
line++;
if (!bug && have_bug(s)) bug = line;
} while (cin >> s && s != ".");
printf("%d\n", bug);
}
return 0;
}
思路
本题直接按string查找会超时,可以将string映射到int后查找。
除了NOT A的查找需要输出整篇文章,其它查找都是需要输出文章的对应行。
由于查找时需要忽略大小写,可以定义两个变量数组,一个存储处理前的文章内容,一个存储处理后的。提取关键字有一个小技巧:可以把字符串中所有非英文字母化为空格。
根据查询的关键字分别处理的过程,可以用到stl中的set_union和set_intersection函数进行求交集和并集。
另外,这题有一个很可笑的问题:
原题中的样例输出竟然是错的,其中的’-‘只有九个,实际上能AC的程序应该是十个。
准确的输出应该是:———-
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int N = 100;
int n;
vector<string> docs[N];
vector<set<int> > docs_ids[N];
map<string, int> str2id;
vector<string> id2str;
int ID(string s)
{
if (str2id.count(s)) return str2id[s];
id2str.push_back(s);
return str2id[s] = id2str.size()-1;
}
int include_id(vector<set<int> >& ids, int id)
{
for (int j = 0; j < ids.size(); j++) {
if (ids[j].count(id)) return j;
}
return ids.size();
}
void print_doc(vector<string>& doc)
{
for (int i = 0; i < doc.size(); i++)
cout << doc[i] << endl;
}
bool comb(bool a, bool op, bool b)
{
if (op) return a && b;
return a || b;
}
int main()
{
string s, s1, s2;
cin >> n;
getchar();
for (int i = 0; i < n; i++) {
while (getline(cin, s) && s != "**********") {
docs[i].push_back(s);
for (int j = 0; j < s.size(); j++) {
if (isalpha(s[j])) s[j] = tolower(s[j]);
else s[j] = ' ';
}
stringstream ss(s);
set<int> st;
while (ss >> s1)
st.insert(ID(s1));
docs_ids[i].push_back(st);
}
}
int m, first;
cin >> m;
getchar();
for (int i = 0; i < m; i++) {
getline(cin, s);
first = 1;
if (s[0] == 'N') { //NOT A
s = s.substr(4);
for (int i = 0; i < n; i++) {
vector<set<int> >& ids = docs_ids[i];
int id = ID(s);
if (include_id(ids, id) == ids.size()) {
if (!first) printf("----------\n");
print_doc(docs[i]);
first = 0;
}
}
} else if (s.find(' ') == string::npos) { //key A
for (int i = 0; i < n; i++) {
vector<set<int> >& ids = docs_ids[i];
int id = ID(s);
int j = include_id(ids, id);
if (j != ids.size()) {
if (!first) printf("----------\n");
for (; j < ids.size(); j++) {
if (ids[j].count(id))
cout << docs[i][j] << endl;
}
first = 0;
}
}
} else {
s1 = s.substr(0, s.find(' '));
s2 = s.substr(s.rfind(' ')+1);
bool op = (s.find("AND") != string::npos); //A AND B
for (int i = 0; i < n; i++) {
vector<set<int> >& ids = docs_ids[i];
int id1 = ID(s1), id2 = ID(s2);
int j1 = include_id(ids, id1);
int j2 = include_id(ids, id2);
if (comb(j1 != ids.size(), op, j2 != ids.size())) {
if (!first) printf("----------\n");
for (int j = min(j1, j2); j < ids.size(); j++) {
if (ids[j].count(id1) || ids[j].count(id2))
cout << docs[i][j] << endl;
}
first = 0;
}
}
}
if (first) printf("Sorry, I found nothing.\n");
printf("==========\n");
}
return 0;
}
思路
此题重点有两个地方:
1、输入的处理,我的方式详见代码;
2、新增、删除、修改的判断,使用两个map分别表示两个字典,旧字典中键值在新字典中找不到表示删除,找到但修改了表示修改,新字典中键值在旧字典中找不到了表示新增。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef map<string, string> DICT;
DICT dict1, dict2;
void input_dict(DICT& dict)
{
dict.clear();
string s, key, value;
getline(cin, s);
for (int i = 0; i < s.size(); i++) {
if (!isdigit(s[i]) && !isalpha(s[i]))
s[i] = ' ';
}
stringstream ss(s);
while (ss >> key >> value)
dict[key] = value;
}
void print_keys(char ch, vector<string> vs)
{
printf("%c", ch);
for (int i = 0; i < vs.size(); i++)
cout << vs[i] << ((i == vs.size()-1) ? '\n' : ',');
}
int main()
{
int kase;
cin >> kase;
getchar();
while (kase--) {
input_dict(dict1);
input_dict(dict2);
vector<string> add, del, modi;
DICT::iterator it1, it2;
for (it1 = dict1.begin(); it1 != dict1.end(); it1++) {
it2 = dict2.find(it1->first);
if (it2 == dict2.end()) del.push_back(it1->first);
else if (it2->second != it1->second) modi.push_back(it1->first);
}
for (it2 = dict2.begin(); it2 != dict2.end(); it2++) {
it1 = dict1.find(it2->first);
if (it1 == dict1.end()) add.push_back(it2->first);
}
if (add.size()) print_keys('+', add);
if (del.size()) print_keys('-', del);
if (modi.size()) print_keys('*', modi);
if (!add.size() && !del.size() && !modi.size()) printf("No changes\n");
printf("\n");
}
return 0;
}
思路
题目并不难,但细节比较多,排序的依据有很多,比较容易出错。考差C++和STL也比较全面,比如结构体、构造函数、map、pair、vector、sort、unique等等,代码规范化会对该题有较大的帮助 。另外注意浮点数大小的比较方法。
我的总体思路是先找出包含该地点的所有地图,对其area进行排序去重,找出详细等级为i对应的area(小于i则报错,这时也需要输出最详细的地图)。然后将包含该地点同时为相应area的地图按照题目描述进行排序,第一个就是所求。
另外注意,书中对该题的描述有一处错误:最后一行的“或者包含它的地图总数超过i”应当改成“或者包含它的不同面积的地图种数低于i”。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
const double EPS = 1e-7;
typedef pair<double, double> P;
struct Map {
string name;
double x1, y1, x2, y2, area, d1, d2, d3;
Map(){}
Map(string _name, double _x1, double _y1, double _x2,
double _y2) : name(_name), x1(_x1), y1(_y1), x2(_x2), y2(_y2) {
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
area = (x2 - x1) * (y2 - y1);
}
void set_dis(P p)
{
double x = p.first, y = p.second;
double xm = x - (x1 + x2)/2, ym = y - (y1 + y2)/2;
d1 = xm*xm + ym*ym;
d2 = (y2 - y1) / (x2 - x1) - 0.75;
xm = x - x2, ym = y - y1;
d3 = xm*xm + ym*ym;
}
bool contain(P p)
{
double x = p.first, y = p.second;
return x1 <= x && x <= x2 && y1 <= y && y <= y2;
}
bool operator < (const Map& b) const {
if (fabs(d1 - b.d1) > EPS) return d1 < b.d1;
if (fabs(d2 - b.d2) > EPS) return d2 < b.d2;
if (fabs(d3 - b.d3) > EPS) return d3 > b.d3;
return x1 < b.x1;
}
};
vector<Map> mps;
map<string, P> locas;
void search(string s, int n)
{
cout << s << " at detail level " << n;
if (!locas.count(s)) {
cout << " unknown location\n";
return;
}
P p = locas[s];
vector<double> area;
for (int i = 0; i < mps.size(); i++) {
if (mps[i].contain(p)) {
area.push_back(mps[i].area);
}
}
if (area.empty()) {
cout << " no map contains that location\n";
return;
}
sort(area.begin(), area.end(), greater<double>());
area.erase(unique(area.begin(), area.end()), area.end());
if (area.size() < n) {
cout << " no map at that detail level;";
n = area.size();
}
vector<Map> cover;
for (int i = 0; i < mps.size(); i++) {
if (mps[i].contain(p) && fabs(mps[i].area - area[n-1]) <= EPS) {
mps[i].set_dis(p);
cover.push_back(mps[i]);
}
}
sort(cover.begin(), cover.end());
cout << " using " << cover[0].name << endl;
}
int main(void)
{
string s;
double x1, x2, y1, y2;
cin >> s;
while (cin >> s && s != "LOCATIONS") {
cin >> x1 >> y1 >> x2 >> y2;
mps.push_back(Map(s, x1, y1, x2, y2));
}
while (cin >> s && s != "REQUESTS") {
cin >> x1 >> y1;
locas[s] = P(x1, y1);
}
int n;
while (cin >> s && s != "END") {
cin >> n;
search(s, n);
}
return 0;
}
思路
模拟题,这个题给我坑坏了,先是有两个判断条件没写好WA了两次,每次查错都花好久,但改了还是WA。我用udebug来调,利用上面生成的random数据来测试,始终总是约有20%与标准AC输出不一样。然后反复阅读自己写的程序,反复跟别人的AC程序对比,却怎么也找不到原因。后来发现udebug上给的标准AC输出竟然是错的!!!因为其输出跟别人能AC的程序的运行结果不一样!再后来我发现其实我的程序没注意的情况下,修改了一处漏掉的变量初始化部分后,其实提交后已经能够AC了!!这时候已经花了我将近两天时间!。。
牢骚完毕,讲一下这个题的整体思路:
这是一道并不复杂的流程模拟题,注意看题抠细节,此题关键在于安排任务时是以人为主体还是以请求为循环主体。
题目讲多个人同时选中某个请求时,选出上次处理时间最早的,否则就选出id最小的。乍一看感觉是要以请求为主体来找人的。但在头脑中想想会发现很复杂,因为在找到一个available的人的时候, 还需要跟其他available的人作对比,也就是说先要找出所有available的人, 再从这些人当中选出一个真正available的人来安排给他请求。
解决这个问题的方案就是, 以人为循环主体来找请求(其实题目里也有明确的暗示了, 但是说的非常不明确)。按照题目的要求(两个要求, 自己定义一个比较函数)把存储人的数组先进行排序,对排序后的人依次找一个请求做就行了。而结束的时机是所有人都不在工作并且所有工作都分配完了。
另外,有一个细节中文翻译交代的不清楚:不同请求的到来和处理是互相独立的,也就是说,对某一种请求,允许前一个请求还没处理完的情况下处理下一个请求(前提是下一个请求已经到来)。
最后,总结一下自己出错的地方:
1、update()函数中,np不慎写成了nt,第一次提交报RE,已经怀疑过这个错误了,但检查的时候眼花了没看到。。
2、忘记了每次主循环开始时Per结构体中tid数组的重新初始化。
3、读入人的数据时,忘记对新写入结构体中的变量tproc做初始化,也就是这一行:per[i].tproc = 0;
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
struct Topic {
int num, t0, t, dt;
bool proc(int Time)
{
if (Time < t0 || num == 0) return false;
num--;
t0 += dt;
return true;
}
bool finish(int Time)
{
return num == 0 && t0-dt+t <= Time;
}
};
struct Person {
int pid, k, last, tproc;
vector<int> ti;
bool operator < (const Person& p) const
{
if (last != p.last) return last < p.last;
return pid < p.pid;
}
void proc(int t, int Time)
{
last = Time;
tproc = t;
}
bool finish(int Time)
{
return last + tproc <= Time;
}
};
int nt, np, Time;
Topic top[20];
Person per[5];
void choose_and_proc(Person& p)
{
for (int j = 0; j < p.k; j++) {
int i = p.ti[j];
if (top[i].proc(Time)) {
p.proc(top[i].t, Time);
break;
}
}
}
void update()
{
sort(per, per+np);
for (int i = 0; i < np; i++) {
Person& p = per[i];
if (p.finish(Time)) choose_and_proc(p);
}
}
bool topics_complete()
{
//#define DEBUG
#ifdef DEBUG
printf("Time %d -----\n", Time);
for (int i = 0; i < nt; i++)
printf("top%d: num=%d, t0=%d\n", i, top[i].num, top[i].t0);
printf("-----\n");
for (int i = 0; i < np; i++)
printf("per%d: last=%d, tproc=%d\n", i, per[i].last, per[i].tproc);
printf("==========\n\n");
#endif
for (int i = 0; i < nt; i++)
if (!top[i].finish(Time)) return false;
for (int i = 0; i < np; i++)
if (!per[i].finish(Time)) return false;
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
int kase = 0;
while (scanf("%d", &nt) && nt) {
int tid;
map<int, int> index;
for (int i = 0; i < nt; i++) {
scanf("%d%d%d%d%d", &tid, &top[i].num,
&top[i].t0, &top[i].t, &top[i].dt);
index[tid] = i;
}
scanf("%d", &np);
for (int i = 0; i < np; i++) {
scanf("%d%d", &per[i].pid, &per[i].k);
per[i].ti.clear();
for (int j = 0; j < per[i].k; j++) {
scanf("%d", &tid);
per[i].ti.push_back(index[tid]);
}
per[i].last = -1;
per[i].tproc = 0;
}
Time = 0;
do {
update();
Time++;
} while (!topics_complete());
printf("Scenario %d: All requests are serviced within %d minutes.\n", ++kase, Time);
}
return 0;
}
思路
以后有时间再做吧。
代码
思路
以后有时间再做吧。
代码
思路
以后有时间再做吧。
代码