Alignment of Code

UVA-1593

题意:给你一段代码,让你进行代码缩进,每列字符得尽量靠左对齐,两段单词间最少一个空格

题解:记录每个字符长度,取列中最长字符串作为宽度格式化输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*********************************************************
Flie Name: 1593.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 16时26分56秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

string s[1005][1005];
int cw[1005], cn[1005];

int main(){
int c = 0, r = 0;
string tem;
while(getline(cin, tem)){
istringstream it(tem);
while(it >> tem){
if(int(tem.length()) > cw[c])
cw[c] = tem.length();
s[r][c] = tem;
c++;
}
cn[r++] = c;
c = 0;
}

for(int i = 0; i < r; i++){
for(int j = 0; j < cn[i] - 1; j++)
cout << left << setw(cw[j]) << s[i][j] << " ";
cout << s[i][cn[i] - 1] << '\n';
}


return 0;
}

Ducci Sequence

UVA-1594

题意:给你n个数字,每次操作把前一个数字和后一个数字的差作为当前数字,第n个位第n个和第一个的差,让你判断这样操作下去是否会出现循环,或者全为零。

题解:直接模拟,用set判重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*********************************************************
Flie Name: 1594.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 17时06分39秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
scanf("%d", &n);
while(n--){
set<vector<int> >s;
vector<int>v;
int t;
scanf("%d", &t);
for(int i = 0; i < t; i++){
int tem;
scanf("%d", &tem);
v.emplace_back(tem);
}
while(!s.count(v)){
s.insert(v);
vector<int>vv;
for(int i = 0; i < t; i++)
vv.emplace_back(abs(v[i] - v[(i + 1) % t]));
v = vv;
}
if(count(v.begin(), v.end(), 0) == t)puts("ZERO");
else puts("LOOP");
}

return 0;
}

Throwing cards away I

UVA-10935

题意:给你一个数字,让你完成一个操作,将第一张牌丢掉并输出,把新的第一张牌放在最后,一直操作下去,直到只剩最后一张牌并输出

题解:直接用队列模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*********************************************************
Flie Name: 10935.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 17时26分39秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
while(cin >> n && n){
cout << "Discarded cards:";
int cnt = n;
queue<int>q;
for(int i = 1; i <= n; i++)
q.push(i);
while(cnt > 1){
cout << ' ' << q.front();
q.pop();
q.push(q.front());
q.pop();
cnt--;
if(cnt != 1)cout << ",";
}
putchar('\n');
cout << "Remaining card:";
cout << ' ' << q.front() << '\n';

}

return 0;
}

Foreign Exchange

UVA-10763

题意:给你n个请求,a想去b做交换生,只有当b学校也有人想来a学校才成立。问你最后是否能达成所有请求

题解:multiset直接模拟, 之后我又观了dalao的代码,学到了一个骚气的读入挂,有人想去那个学校加一,想去的学校减一,最后如果全为零就行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*********************************************************
Flie Name: 10763.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 18时38分42秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
while(~scanf("%d", &n) && n){
multiset<pair<int, int> >s;
for(int i = 0; i < n; i++){
int a, b;
scanf("%d%d", &a, &b);
if(s.count(make_pair(b, a)))
s.erase(s.find(make_pair(b, a)));
else s.insert(make_pair(a, b));
}
if(s.empty())puts("YES");
else puts("NO");
}

return 0;
}

利用数组模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*********************************************************
Flie Name: 10763plus.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月17日 星期三 14时13分34秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

char B[1 << 26], *S = B;
int s[500100];

inline int rd(){
for(; *S < '-'; S++);
register int x = int(*S - '0');
for(S++; *S >= '0'; S++)x = (x << 3) + (x << 1) + int(*S - '0');
return x;
}

int main(){
fread(B, sizeof(char), 1 << 26, stdin);
int n;
while((n = rd())){
memset(s, 0, sizeof(s));
for(int i = 0; i < n; i++)
s[rd()]++, s[rd()]--;
if(count(s, s + 500010, 0) == 500010)puts("YES");
else puts("NO");
}

return 0;
}

Compound Words

UVA-10391

题意:按字典序给你一堆单词,问你里面能被分为其他两个单词的单词有哪些,并按字典序输出

题解:注意给你的时候就已经是字典序了,,我先是把每个单词尝试用其他单词去拼,好险卡过去了,说明给的单词太多,这样并不优,接着我把每个单词拆开来看是否能在表中找到,这样就能过了,使用unorderset能更省,而且因为unorderedset是hash实现,所以find比count快多了

险些超时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*********************************************************
Flie Name: 10391.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 18时49分03秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

string str[120100];

int main(){
set<string>s;
int cnt = 0;
while(cin >> str[cnt])
s.insert(str[cnt++]);
for(auto i : s)
for(int j = 0; j < cnt; j++)
if(i.find(str[j]) == 0 && s.count(i.substr(str[j].length()))){
cout << i << '\n';
break;
}

return 0;
}

使用第二种想法,并用unorderedset加速

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*********************************************************
Flie Name: 10391.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 18时55分03秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

char str[120100][100];

int main(){
unordered_set<string>s;
int cnt = 0;
while(cin >> str[cnt])
s.insert(string(str[cnt++]));
for(int i = 0; i < cnt; i++){
int t = int(strlen(str[i]));
for(int j = 0; j < t; j++){
char tem = str[i][j];
str[i][j] = 0;
if(s.find(string(str[i])) == s.end()){
str[i][j] = tem;
continue;
}else {
str[i][j] = tem;
if(s.find(string(str[i] + j)) != s.end()){
puts(str[i]);
break;
}
}
}
}

return 0;
}

Symmetry

UVA-1595

题意:给你n个点,问你能否找到一条竖线让他们对称

题解:因为对称,所以最左最右中间那条就是那条线,然后判断能否全局符合,注意很多点都在那条线上的情况, 或者,取最左和最右点判断每个点能否找到相同高度,宽度和位最左最右和的点,这样想更好想更方便

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*********************************************************
Flie Name: 1595.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 20时09分30秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
int mid;

typedef pair<int, int> pa;

bool cmpp(pa a, pa b){
int x1 = a.first, y1 = a.second, x2 = b.first, y2 = b.second;
if(x1 != x2)return x1 < x2;
if(x1 <= mid)return y1 > y2;
else return y2 > y1;
}

int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d" ,&n);
vector<pa>v;
int l = inf, r = -inf;
for(int i = 0; i < n; i++){
int x, y;
scanf("%d%d", &x, &y);
l = min(l, x);
r = max(r, x);
v.emplace_back(pa(x, y));
}
mid = (l + r) / 2;
sort(v.begin(), v.end(), cmpp);
bool can = true;
for(int i = 0; can && i < n / 2 + (n & 1); i++)
if((mid - v[i].first != v[n - i - 1].first - (mid + (((r - l) & 1) ? 1 : 0)) || v[i].second != v[n - i - 1].second) && (v[i].first != mid || v[n - i - 1].first != mid))can = false;
if(can)puts("YES");
else puts("NO");
}

return 0;
}

第二种想法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*********************************************************
Flie Name: 1595plus.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月17日 星期三 15时31分54秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pa;

pa s[1010];

bool ok(pa a, int n, int all){
bool ans = false;
for(int i = 0; i < n; i++)
if(a.second == s[i].second && a.first + s[i].first == all)ans = true;
return ans;
}

int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d" ,&n);
int l = 20000, r = -20000;
for(int i = 0; i < n; i++){
int a, b;
scanf("%d%d", &a, &b);
l = min(l, a);
r = max(r, a);
s[i].first = a, s[i].second = b;
}
bool can = true;;
for(int i = 0; can && i < n; i++)
if(!ok(s[i], n, l + r))can = false;
if(can)puts("YES");
else puts("NO");
}

return 0;
}

Printer Queue

UVA-12100

题意:给你n个打印任务和一个关注对象,任务有优先级,优先级高的先打印,在它前面的到对尾,问你关注对象什么时候能打印

题解:直接模拟,循环数组模拟即可,但这里为了凸显stl,用了队列模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*********************************************************
Flie Name: 12100.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月16日 星期二 21时32分08秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, bool> pa;

int main(){
int T;
scanf("%d", &T);
while(T--){
int n, m;
scanf("%d%d", &n, &m);
priority_queue<int>s;
queue<pa>q;
for(int i = 0; i < n; i++){
int tem;
scanf("%d", &tem);
bool ca = (i == m);
q.push(pa(tem, ca));
s.push(tem);
}
int time = 1;
while(1){
while(q.front().first != s.top()){
q.push(q.front());
q.pop();
}
if(q.front().second){
break;
}
q.pop();
s.pop();
time++;
}
printf("%d\n", time);
}

return 0;
}

Borrowers

UVA-230

题意:让你模拟一个借书系统,还书时输出把书还到哪儿

题解:先为书建立书库,再直接模拟即可, 注意输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*********************************************************
Flie Name: 230.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月17日 星期三 16时54分26秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

inline void Put(string a, string b){
if(b == ""){
cout << "Put " << a << " first\n";
return;
}
cout << "Put " << a << " after " << b << '\n';
}

struct Book{
string title;
string author;
bool bow;
Book() : bow(false){}
bool operator < (const Book b){
if(author != b.author)return author < b.author;
return title < b.title;
}
}book[12000];

string FindPre(int aim){
int tt = aim;
while(aim > 0){
if(!book[--aim].bow)
break;
}
if((aim == 0 && book[aim].bow))return book[tt].bow = false, "";
book[tt].bow = false;
return book[aim].title;
}

struct cmpp{
bool operator () (const int a, const int b){
return book[b] < book[a];
}
};


int main(){
string tem;
int cnt = 0;
map<string, int>m;
while(getline(cin, tem) && tem != "END"){
int t = tem.find("\" by ");
book[cnt].title = tem.substr(0, t + 1);
book[cnt++].author = tem.substr(t + 5);
}
sort(book, book + cnt);
for(int i = 0; i < cnt; i++)
m[book[i].title] = i;

string aim;
priority_queue<int, vector<int>, cmpp>need;
while(getline(cin, tem) && tem != "END"){
auto t = tem.find(' ');
if(t == string::npos)aim = tem;
else aim = tem.substr(0, t), tem = tem.erase(0, t + 1);
if(aim == "BORROW")book[m[tem]].bow = true;
else if(aim == "RETURN")need.push(m[tem]);
else {
while(!need.empty()){
Put(book[need.top()].title, FindPre(need.top()));
need.pop();
}
cout << "END\n";
}
}


return 0;
}

巧用lower_bound可以优化代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*********************************************************
Flie Name: 230plus.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月17日 星期三 20时14分52秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

struct Book{
string ti, au;
Book(string a, string b) : ti(a), au(b) {}
bool operator < (const Book &bt) const{
return au < bt.au || (au == bt.au && ti < bt.ti);
}
};

set<Book>k;
set<Book>pr;
map<string, string>mem;

int main(){
string tem;
while(getline(cin, tem) && tem[0] != 'E'){
auto t = tem.find(" by ");
mem[tem.substr(0, t)] = tem.substr(t + 4);
k.insert(Book(tem.substr(0, t), tem.substr(t + 4)));
}

while(getline(cin, tem) && tem[0] != 'E'){
if(tem[0] == 'S'){
for(auto i : pr){
cout << "Put " << i.ti;
auto t = k.lower_bound(i);
if(k.empty() || t == k.begin())cout << " first\n";
else cout << " after " << (--t)->ti << '\n';
k.insert(i);
}
cout << "END\n";
pr.clear();
}else {
string s = tem.substr(7);
if(tem[0] == 'B')k.erase(Book(s, mem[s]));
if(tem[0] == 'R')pr.insert(Book(s, mem[s]));
}
}

return 0;
}

Bug Hunt

UVA-1596

题意:给你一段代码,问你能不能找出bug,有定义语句于赋值语句,错误只可能是下标越界或者调用未初始化变量

题解:模拟,对于定义语句记录数组大小,初始化的各个值,赋值语句递归检查是否出席那未初始化或者越界情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*********************************************************
Flie Name: 1596.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月17日 星期三 21时33分17秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

struct Arrage{
int size;
map<int, int>vec;

Arrage(){remove();}
void remove(){size = -1, vec.clear();}
bool exists(){return size >= 0;}
void init(int n){size = n, vec.clear();}
bool setValue(int index, int val){
if(index >= size)return false;
vec[index] = val;
return true;
}
bool getVal(int index, int &val){
assert(exists());
if(vec.count(index)){
val = vec[index];
return true;
}else return false;
}
};

const int Ma = 200;
Arrage arr[Ma];

bool eve(const char *base, int len, int &val){
if(isdigit(*base)){
sscanf(base, "%d", &val);
return true;
}
char aim = *base;
if(!arr[aim].exists())return false;
assert(isalpha(aim));
int ne;
if(!eve(base + 2, len - 3, ne))return false;
else return arr[aim].getVal(ne, val);
}

int main(){
char line[1000];
int lineNum = 0, bugNum = 0;
while(~scanf("%s", line)){
char *fi;
if(line[0] == '.'){
if(lineNum)printf("%d\n", bugNum);
for(int i = 0; i < Ma; i++)arr[i].remove();
lineNum = 0;
bugNum = 0;
continue;
}
if(bugNum)continue;
if((fi = strchr(line, '='))){
int index, val, len = strlen(line);
if(arr[line[0]].exists()
&& eve(fi + 1, len - (fi - line) - 1, val)
&& eve(line + 2, fi - line - 3, index)
&& arr[line[0]].setValue(index, val))
lineNum++;
else
bugNum = lineNum + 1;
}else {
char aim;
int val;
sscanf(line, "%c[%d]%", &aim, &val);
arr[aim].init(val);
lineNum++;
}

}


return 0;
}

Searching the Web

UVA-1597

题意:给你n篇文章,有几种询问 直接询问,输出包含这个单词的文章中的相关语句,每篇文章分割开 AND询问,输出同时包含这两个单词的文章 OR询问,输出包含a或者b的文章 NOT询问,输出不包含单词的文章

题解:为每篇文章建立对象,直接模拟查询即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*********************************************************
Flie Name: 1597.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月18日 星期四 20时54分29秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

set<int>emptySet;

struct Doc{
vector<string>store;
map<string, set<int> >cous;

void build(string &tem){
store.emplace_back(tem);
string cc;
int tt = tem.length();
for(int i = 0; i < tt; i++){
if(isalpha(tem[i]))cc += tolower(tem[i]);
else if(!cc.empty()){
cous[cc].insert(store.size() - 1);
cc.clear();
}
}
if(!cc.empty())cous[cc].insert(store.size() - 1);
}

const set<int>& find(const string& aim){
if(cous.count(aim))return cous[aim];
else return emptySet;
}
};

Doc dic[110];
int n;

void seprat(string tem, vector<string>& qwq){
istringstream is(tem);
while(is >> tem)
qwq.emplace_back(tem);
}

void Output(vector<string>& need, const set<int>& mb){
for(auto i : mb)
cout << need[i] << '\n';
}

bool see;

void only(const string& aim){
bool first = false;
for(int i = 0; i < n; i++){
Doc& a = dic[i];
const set<int>& fid = a.find(aim);

if(!fid.empty()){
if(first)cout << "----------\n";
Output(a.store, fid), first = true, see = true;
}
}
}

void nodes(const string& aim){
bool first = false;
for(int i = 0; i < n; i++){
Doc& a = dic[i];
const set<int>& fid = a.find(aim);

if(fid.empty()){
if(first)cout << "----------\n";
for(auto i : a.store)
cout << i << '\n';
first = true;
see = true;
}
}
}

void Orit(const string& A, const string& B){
bool first = false;
for(int i = 0; i < n; i++){
Doc& a = dic[i];
const set<int>& fid = a.find(A);
const set<int>& fidd = a.find(B);
set<int> ans;

if(!fid.empty() || !fidd.empty()){
if(first)cout << "----------\n";
first = true;
set_union(fid.begin(), fid.end(), fidd.begin(), fidd.end(), inserter(ans, ans.begin()));
Output(a.store, ans);
see = true;
}
}
}

void Andit(string A, string B){
bool first = false;
for(int i = 0; i < n; i++){
Doc& a = dic[i];
const set<int>& fid = a.find(A);
const set<int>& fidd = a.find(B);
set<int> ans;

if(!fid.empty() && !fidd.empty()){
if(first)cout << "----------\n";
first = true;
see = true;
set_union(fid.begin(), fid.end(), fidd.begin(), fidd.end(), inserter(ans, ans.begin()));
Output(a.store, ans);
}
}
}

void putart(vector<string>& qwq){
see = false;
if(qwq.size() == 1)only(qwq[0]);
else if(qwq.size() == 2)nodes(qwq[1]);
else if(qwq[1] == "OR")Orit(qwq[0], qwq[2]);
else if(qwq[1] == "AND")Andit(qwq[0], qwq[2]);
else assert(false);
if(!see)cout << "Sorry, I found nothing.\n";
cout << "==========\n";
}

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
cin.ignore();
for(int i = 0; i < n; i++){
string tem;
while(getline(cin, tem) && tem != "**********")
dic[i].build(tem);
}
int T;
cin >> T;
cin.ignore();
for(int i = 0; i < T; i++){
string tem;
getline(cin, tem);
vector<string> qwq;
seprat(tem, qwq);
putart(qwq);
}

return 0;
}

Updating a Dictionary

UVA-12504

题意:给你一个字典,然后给你更新后的字典, 问你删去了哪些值,添加了哪些值,那些key的val改变了

题解:直接模拟,利用正则表达式可简化这个过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*********************************************************
Flie Name: 12504.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月19日 星期五 13时37分36秒
*********************************************************/
#include<bits/stdc++.h>
//#define DEBUG
using namespace std;

void readDic(map<string, string>& dic){
char s[110];
fgets(s, sizeof(s), stdin);
int len = strlen(s) - 1;
s[len - 1] = ',';
char key[110], val[110];
int now = 0;
while(now < len - 1 && sscanf(s + now, "%[^:]:%[^,]", key, val)){
now += strlen(key) + strlen(val) + 2;
dic[string(key)] = string(val);
}
}

int main(){
int T;
scanf("%d\n", &T);
while(T--){
map<string, string>old, xin;
getchar();
readDic(old);
getchar();
readDic(xin);
if(old == xin)puts("No changes");
else {
bool first = true;
for(auto i : xin){
if(!old.count(i.first)){
printf("%c%s", first ? first = false, '+' : ',', i.first.c_str());
}
}
if(!first)putchar('\n');
first = true;
queue<string>change;
for(auto i : old){
if(!xin.count(i.first)){
printf("%c%s", first ? first = false, '-' : ',', i.first.c_str());
}else if(i.second != xin[i.first])change.push(i.first);
}
if(!first)putchar('\n');
if(change.size()){
putchar('*');
printf("%s", change.front().c_str());
change.pop();
while(!change.empty()){
printf(",%s", change.front().c_str());
change.pop();
}
putchar('\n');
}
}
putchar('\n');
}

return 0;
}

Do You Know the Way to San Jose?

UVA-511

题意:给你数张地图,再给你数个地点,然后问你地点在某个等级的地图是否存在,如不存在那个等级的地图,就找到等级最小的包含他的地图,不存在包含他的地图就输出不存在包含他的地图,不存在地点就输出不存在地点

题解:对每个地图建立对象,对每个查询如果地点存在则对包含它的地图进行排序,输出序列中的要求等级的地图,如不存在,按要求处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*********************************************************
Flie Name: 511.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月19日 星期五 14时59分30秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

const double eps = 1e-7;

int cmpp(double a){if(fabs(a) < eps)return 0; return a > 0 ? 1 : -1;}
int cmp(double a, double b){return cmpp(a - b);}

struct Map{
string name;
double x1, y1, x2, y2;
double x0, y0, yx, yy;
double ratio;
double size;
Map(string s, double x, double y, double xx, double yy) : name(s), x1(x), y1(y), x2(xx), y2(yy) {
x0 = (x1 + x2) / 2.0;
y0 = (y1 + y2) / 2.0;
yx = max(x1, x2);
yy = min(y1, y2);
ratio = fabs(fabs(y1 - y2) / fabs(x1 - x2) - 0.75);
size = fabs(y1 - y2) * fabs(x1 - x2);
}

double Center(double x, double y){
return (x0 - x) * (x0 - x) + (y0 - y) * (y0 - y);
}
double ToRight(double x, double y){
return (yx - x) * (yx - x) + (yy - y) * (yy - y);
}
bool inHere(double x, double y){
return (abs(cmp(x, x1) + cmp(x, x2)) < 2 && abs(cmp(y, y1) + cmp(y, y2)) < 2);
}
};

vector<Map>base;

struct Loc{
double x, y;
Loc(double xx, double yy) : x(xx), y(yy) {}
bool operator () (int a, int b){
int c = cmp(base[a].size, base[b].size);
if(c == 1)return true;
if(c == -1)return false;

c = cmp(base[a].Center(x, y), base[b].Center(x, y));
if(c == 1)return true;
if(c == -1)return false;

c = cmp(base[a].ratio, base[b].ratio);
if(c == 1)return true;
if(c == -1)return false;

c = cmp(base[a].ToRight(x, y), base[b].ToRight(x, y));
if(c == -1)return true;
if(c == 1)return false;

return base[a].x0 >= base[b].x0;
}
};

map<string, pair<double, double> >loc;

void findQuee(vector<int>& smap, vector<int>& level, double x, double y){
int all = base.size();
for(int i = 0; i < all; i++)
if(base[i].inHere(x, y))smap.emplace_back(i);
sort(smap.begin(), smap.end(), Loc(x, y));
all = smap.size();
level.assign(all, 1);
for(int i = 1; i < all; i++){
int c = cmp(base[smap[i]].size, base[smap[i - 1]].size);
assert(c != 1);
level[i] = level[i - 1] - c;
}
}

void querry(string name, int level){
cout << name << " at detail level " << level << ' ';
if(!loc.count(name)){
cout << "unknown location\n";
return;
}
vector<int>smap, lv;
findQuee(smap, lv, loc[name].first, loc[name].second);
if(smap.empty())
cout << "no map contains that location\n";
else if(level > lv.back())
cout << "no map at that detail level; using " << base[smap.back()].name << '\n';
else {
int fid = upper_bound(lv.begin(), lv.end(), level) - lv.begin() - 1;
cout << "using " << base[smap[fid]].name << '\n';
}
}

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string tem;
cin >> tem;
assert(tem == "MAPS");
while(cin >> tem && tem != "LOCATIONS"){
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
base.emplace_back(Map(tem, x1, y1, x2, y2));
}

while(cin >> tem && tem != "REQUESTS"){
double x, y;
cin >> x >> y;
loc[tem] = make_pair(x, y);
}

while(cin >> tem && tem != "END"){
int level;
cin >> level;
querry(tem, level);
}


return 0;
}

Queue and A

UVA - 822

题意:给你n个类型的请求,和m个客服,让客服来处理这些请求,问你要处理多久

题解:将类型的请求全处理为单个请求,将空闲客服也可处理成相应请求,然后按时间发展将请求一个个入相应集合,并进行匹配,每匹配一个客服,就把下一个时间客服空闲时压入请求队列中,模拟解决。 注意事件请求可能有许多相同类型事件等待处理,需要用multiset来处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*********************************************************
Flie Name: 822plus.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月20日 星期六 19时59分29秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

struct Topic{
int pid, num, begin, cost, betw;
Topic(int id, int nu, int be, int co, int bet) :
pid(id), num(nu), begin(be), cost(co), betw(bet){}
};

vector<Topic>job;

struct Event{
int time, idx;
bool yg;
Event(int ti, int id, bool is = false) : time(ti), idx(id), yg(is){
}
bool operator < (const Event b) const {
return time > b.time;
}
};

struct Man{
int pid, last;
vector<int>job;
bool operator < (const Man b)const {
return last < b.last || (last == b.last && pid < b.pid);
}
}man[10];

priority_queue<Event>ev;


struct juWay{
bool operator () (const int a, const int b) const {
return man[b] < man[a];
}
};

int T, n;

set<int, juWay>mjob[220];
set<int> freeMan;
multiset<int> work;

inline void clean(){
for(int i = 0; i < n; i++)
mjob[i].clear();
}

void solve(){
clean();
int time = ev.top().time;
while(!ev.empty() && ev.top().time == time){
auto& s = ev.top();
if(s.yg)freeMan.insert(s.idx);
else work.insert(s.idx);
ev.pop();
}

while(!freeMan.empty() && !work.empty()){
bool can = false;
for(auto i : freeMan){
for(auto j : man[i].job){
if(work.count(j)){
if(!can)can = true;
mjob[j].insert(i);
break;
}
}
}

if(!can)break;

for(int i = 0; i < n; i++){
while(work.count(i) && !mjob[i].empty()){
work.erase(work.lower_bound(i));
auto b = mjob[i].begin();
freeMan.erase(*b);
man[*b].last = time;
ev.push(Event(time + job[i].cost, *b, true));
mjob[i].erase(b);
}
}
}

if(ev.empty()) cout << "Scenario "<< T << ": All requests are serviced within "<< time << " minutes.\n";
}

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
map<int, int>mp;
T = 0;
while(cin >> n && n){
mp.clear(), job.clear(), freeMan.clear(), work.clear();
++T;
for(int i = 0; i < n; i++){
int pid, num, begin, cost, bew;
cin >> pid >> num >> begin >> cost >> bew;
mp[pid] = i;
job.emplace_back(Topic(i, num, begin, cost, bew));
for(int j = 0; j < num; j++)
ev.push(Event(begin + j * bew, i));
}

int m;
cin >> m;
assert(m < 10);
for(int i = 0; i < m; i++){
int pid, jobn;
cin >> pid >> jobn;
man[i].pid = i;
man[i].job.clear();
for(int j = 0; j < jobn; j++){
cin >> pid;
assert(mp.count(pid));
man[i].job.emplace_back(mp[pid]);
ev.push(Event(0, i, true));
}
man[i].last = 0;
}

while(!ev.empty())solve();
}

return 0;
}

Exchange

UVA-1598

题意:模拟商品交易,有BUY订单和SELL订单,和CANXEL取消订单,每次命令后都得输出当前最高的bid price及这个价位的规模, 最低的ask price及这个价位规模,对每个buy和sell,订单进来时如果能交易(能交易定义为buy的价格不小于最低的ask price,或者sell的价格不大于最高的bid price),就进行交易,并输出交易的订单金额和交易规模

题解:包含在题意中,按题意模拟即可,运用cacel数组来标记那些被cacel了,对于cacel命令不计入,所以用order数组来为各个命令重新标记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*********************************************************
Flie Name: 1598plus.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月29日 星期一 09时10分33秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

const int Ma = 1e4 + 100;

struct Order{
bool buy;
int size;
int price;
};

vector<Order>orders;
int orderIndex[Ma];
bool cancleIndex[Ma];

template<typename OrderCmp>
struct PriorityQue{
set<int, OrderCmp>my;
map<int, int>allsize;

void push(int ind){my.insert(ind); Order& a = orders[ind]; allsize[a.price] += a.size;}
bool empty(){return my.empty();}
int topsize(){int i = *my.begin(); return allsize[orders[i].price];}
int topval(){int i = *my.begin(); return orders[i].price;}
void clear(){my.clear(); allsize.clear();}
void erase(int ind){allsize[orders[ind].price] -= orders[ind].size; my.erase(ind);}
int pop(){int ans = *my.begin(); erase(ans); return ans;}
};

struct BuyCmp{
bool operator () (const int a, const int b){
const Order &i = orders[a], &j = orders[b];
return i.price > j.price || (i.price == j.price && a < b);
}
};

struct SellCmp{
bool operator () (const int i, const int j){
const Order &a = orders[i], &b = orders[j];
return a.price < b.price || (a.price == b.price && i < j);
}
};

PriorityQue<BuyCmp>buyQuerry;
PriorityQue<SellCmp>sellQuerry;

void Cancle(int ind){
int index = orderIndex[ind];
if(cancleIndex[index])return ;
if(orders[index].buy)buyQuerry.erase(index);
else sellQuerry.erase(index);
cancleIndex[index] = true;
}

void trade(int sl, int val){
printf("TRADE %d %d\n", sl, val);
}

void py(int indx){
auto& it = orders[indx];
if(it.buy){
while(!sellQuerry.empty() && it.size &&
sellQuerry.topval() <= it.price){
int sind = sellQuerry.pop();
Order& se = orders[sind];
int msize = min(it.size, se.size);
trade(msize, se.price);
it.size -= msize;
se.size -= msize;
if(se.size)sellQuerry.push(sind);
else cancleIndex[sind] = true;
}
if(it.size)buyQuerry.push(indx);
else cancleIndex[indx] = true;
}else {
while(!buyQuerry.empty() && it.size &&
buyQuerry.topval() >= it.price){
int bind = buyQuerry.pop();
Order& be = orders[bind];
int msize = min(it.size, be.size);
trade(msize, be.price);
it.size -= msize;
be.size -= msize;
if(be.size)buyQuerry.push(bind);
else cancleIndex[bind] = true;
}
if(it.size)sellQuerry.push(indx);
else cancleIndex[indx] = true;
}
}

void repo(){
int ask = 0, ap = 0;
int sell = 0, sp = 99999;
if(!buyQuerry.empty()){
ask = buyQuerry.topsize();
ap = buyQuerry.topval();
}
if(!sellQuerry.empty()){
sell = sellQuerry.topsize();
sp = sellQuerry.topval();
}
printf("QUOTE %d %d - %d %d\n", ask, ap, sell, sp);
}

void init(int n){
orders.clear();
fill_n(orderIndex, n, -1);
fill_n(cancleIndex, n, false);
buyQuerry.clear();
sellQuerry.clear();
}

int main(){
int n;
bool to = false;;
while(~scanf("%d", &n)){
if(to)putchar('\n');
else to = true;
char s[100];
init(n);
for(int i = 0; i < n; i++){
scanf("%s", s);
if(s[0] == 'C'){
int ind;
scanf("%d", &ind);
ind--;
Cancle(ind);
repo();
continue;
}

Order o;
o.buy = (s[0] == 'B');
scanf("%d%d", &o.size, &o.price);
orderIndex[i] = orders.size();
orders.emplace_back(o);
py(orderIndex[i]);
repo();
}
}

return 0;
}

Revenge of Fibonacci

UVA-12333

题意:给你n个fibonacci数的前几位数,问你他属于第几个fibonacci数

题解:因为数据范围很小,只有100000个fibonacci数,所以我们打表记录下各个fibonacci数的前40个,注意似乎前40个,所以我们只要高精度模拟,超过40位时,我们只多取后面十几位的数,维护前40位就可以了,对这些fibonacci数可建立一颗字典数,方便查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*********************************************************
Flie Name: SolutionPlus.cpp
Author: Liupo
Email: lanzongwei541@gmail.com
Creat time: 2019年07月29日 星期一 17时19分54秒
*********************************************************/
#include<bits/stdc++.h>
using namespace std;

const int Ma = 1e7 + 100;

struct Node{
int next[10];
int id;
}node[Ma];

int glo;

void build(char* s, int id){
int cur = 0;
for(int i = 0; s[i]; i++){
int v = s[i] - '0';
if(node[cur].next[v] == -1)
node[cur].next[v] = ++glo;
cur = node[cur].next[v];
if(node[cur].id == -1)node[cur].id = id;
}
}

void db(){
char a[2][100000];
memset(a, 0, sizeof(a));
memset(node, -1, sizeof(node));
a[0][0] = '1';
a[1][0] = '1';
build(a[0], 0);
int l = 0, r = 1;
for(int i = 2; i < 100000; i++){
int p = i & 1, q = !p;
for(int pos = l; pos < r; pos++){
if(a[p][pos])a[p][pos] += a[q][pos] - '0';
else a[p][pos] = a[q][pos];
if(a[p][pos] > '9'){
a[p][pos] -= 10;
if(a[p][pos + 1])a[p][pos + 1]++;
else a[p][pos + 1] = '1';
}
}
if(a[p][r])r++;
if(r - l > 56)l++;
char n[100];
int len = min(r - l, 45);
for(int j = 0; j < len; j++)n[j] = a[p][r - j - 1];
n[len] = 0;
build(n, i);
}
}

int ele(char* s){
int cur = 0;
for(int i = 0; s[i]; i++){
int v = s[i] - '0';
cur = node[cur].next[v];
if(cur == -1)return -1;
}
return node[cur].id;
}

int main(){
int n, k = 0;
db();
scanf("%d", &n);
while(n--){
char aim[100];
scanf("%s", aim);
printf("Case #%d: %d\n", ++k, ele(aim));
}

return 0;
}

Use of Hospital Facilities

UVA-212

题意:医院有n个手术室, m个休息室,病人做完手术就要去休息室,每天开始时间给出,腾出一个手术室,休息室,病人走到休息室的时间也给出,再给你病人的预约名单,问你明天的时间表和床的利用效率

题解:先按病人的顺序计算出手术完成的时间,然后根据这个计算出病人的休息房间安排,注意,时按手术完成的时将进行安排,而不是走到休息室进行安排,也就是说,如果手术完了哪间休息室就安排给他了,即便他走到休息室时序号更小的休息室空出来了,他也不能去,在这里wa了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*********************************************************
flie name: 212.cpp
author: liupo
email: lanzongwei541@gmail.com
creat time: 2019年07月30日 星期二 11时15分01秒
*********************************************************/
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define seg(i) i.begin(), i.end()
#define expa(i) i.first, i.second
using namespace std;

typedef pair<int, int> pr;

struct patient{
string name;
int opn, ren;
int opbe, rebe;
int opr, rer;
patient(string s, int op, int re) : name(s), opn(op), ren(re) {}
};

vector<patient>pa;

struct encmp{
bool operator () (const int a, const int b){
return pa[a].rebe < pa[b].rebe || (pa[a].rebe == pa[b].rebe && pa[a].opr < pa[b].opr);
}
};

int opuse[50], reuse[50];
bool order[50];
int opm, rem, bt, otop, top, tre, n;
int last = 0;

int getmin(){
rep(i, rem)if(!order[i])return i + 1;
assert(false);
return rem;
}

pr gettime(int minc){
return make_pair(bt + minc / 60, minc % 60);
}

void putlist(){
puts(" Patient Operating Room Recovery Room");
puts(" # Name Room# Begin End Bed# Begin End");
puts(" ------------------------------------------------------");
rep(i, n){
pr opbegin = gettime(pa[i].opbe);
pr opend = gettime(pa[i].opbe + pa[i].opn);
pr rebegin = gettime(pa[i].rebe);
pr reend = gettime(pa[i].rebe + pa[i].ren);
printf("%2d %-9s %2d %2d:%02d %2d:%02d %2d %2d:%02d %2d:%02d\n",
i + 1, pa[i].name.c_str(), pa[i].opr, expa(opbegin), expa(opend), pa[i].rer, expa(rebegin), expa(reend));
}
}

void putfac(){
puts("Facility Utilization");
puts("Type # Minutes % Used");
puts("-------------------------");
rep(i, opm){
printf("Room %2d %4d %6.2f\n", i + 1, opuse[i + 1], last == 0 ? 0 : double(100.0 * opuse[i + 1] / (1.0 * last)));
}
rep(i, rem){
printf("Bed %2d %4d %6.2f\n", i + 1, reuse[i + 1], last == 0 ? 0 : double(100.0 * reuse[i + 1] / (1.0 * last)));
}
}

void init(){
memset(opuse, 0, sizeof(opuse));
memset(reuse, 0 , sizeof(reuse));
memset(order, 0, sizeof(order));
last = 0;
pa.clear();

}

int main(){
while(~scanf("%d%d%d%d%d%d%d", &opm, &rem, &bt, &otop, &top, &tre, &n)){
init();
set<int, encmp>been;
priority_queue<pr, vector<pr>, greater<pr> >enop;
priority_queue<pr, vector<pr>, greater<pr> >enre;
int opuid = 1;
rep(i, n){
char name[100];
scanf("%s", name);
int op, re;
scanf("%d%d", &op, &re);
pa.emplace_back(patient(string(name), op, re));
if(int(enop.size()) < opm){
pa[i].opbe = 0;
pa[i].opr = opuid;
pa[i].rebe = op + otop;
enop.push(pr(top + op, opuid++));
}else {
pa[i].opbe = enop.top().first;
int uid = enop.top().second;
pa[i].rebe = op + pa[i].opbe + otop;
pa[i].opr = uid;
enop.pop();
enop.push(pr(pa[i].opbe + top + op, uid));
}
opuse[pa[i].opr] += op;
been.insert(i);
}

for(int i : been){
while(!enre.empty() && enre.top().first <= pa[i].rebe - otop){
order[enre.top().second - 1] = false;
enre.pop();
}
pa[i].rer = getmin();
enre.push(pr(pa[i].rebe + pa[i].ren + tre, pa[i].rer));
order[pa[i].rer - 1] = true;
reuse[pa[i].rer] += pa[i].ren;
last = max(last, pa[i].ren + pa[i].rebe);
}

putlist();
putchar('\n');
putfac();
putchar('\n');
}

return 0;
}

至此,算法入门经典语言篇第五章完结,下面进入第六章基础篇的学习

Comments

⬆︎TOP