C++算法
算法竞赛入门经典第 5 章:C++ 与 STL 入门代码示例
《算法竞赛入门经典(第 2 版)》第 5 章的 C++ 与 STL 入门代码笔记。内容涵盖从 C 到 C++ 的基础特性,包括引用、字符串处理、结构体重载运算符等。重点介绍了 STL 容器的应用,如 vector、set、map、queue 和 priority_queue,并通过多个经典算法题目(如大理石在哪、木块问题、丑数等)展示了具体实现。此外还包含大整数类应用及多道竞赛题目的代码解析,适合算法竞赛初学者参考学习。

《算法竞赛入门经典(第 2 版)》第 5 章的 C++ 与 STL 入门代码笔记。内容涵盖从 C 到 C++ 的基础特性,包括引用、字符串处理、结构体重载运算符等。重点介绍了 STL 容器的应用,如 vector、set、map、queue 和 priority_queue,并通过多个经典算法题目(如大理石在哪、木块问题、丑数等)展示了具体实现。此外还包含大整数类应用及多道竞赛题目的代码解析,适合算法竞赛初学者参考学习。

// C++版的'a+b 程序'
#include <cstdio>
int main() {
int a, b;
while(scanf("%d%d", &a, &b) == 2)
printf("%d\n", a + b);
return 0;
}
// 展示更多的常用 C++ 特性
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100 + 10;
int A[maxn];
int main() {
long long a, b;
while(cin >> a >> b) {
cout << min(a, b) << "\n";
}
return 0;
}
#include <iostream>
using namespace std;
void swap2(int& a, int& b) {
int t = a; a = b; b = t;
}
int main() {
int a = 3, b = 4;
swap2(a, b);
cout << a << " " << b << "\n";
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line;
while(getline(cin, line)) {
int sum = 0, x;
stringstream ss(line);
while(ss >> x) sum += x;
cout << sum << "\n";
}
return 0;
}
// 引入输入输出流库,用于控制台输入输出
#include <iostream>
// 使用标准命名空间,避免每次都要写 std::
using namespace std;
// 定义一个表示二维点的结构体
struct Point {
int x, y; // 点的 x 和 y 坐标
// 构造函数,带有默认参数
// 当创建 Point 对象时,如果没有提供参数,则使用默认值 (0,0)
Point(int x = 0, int y = 0) : x(x), y(y) {}
// 使用初始化列表来初始化成员变量,效率更高
// :x(x), y(y) 表示将参数 x 赋值给成员变量 x,参数 y 赋值给成员变量 y
};
// 重载加法运算符,实现两个 Point 对象的相加
// 参数:两个 Point 对象的常引用(const & 避免拷贝,提高效率)
// 返回:一个新的 Point 对象,其坐标为两个点坐标之和
Point operator+(const Point& A, const Point& B) {
return Point(A.x + B.x, A.y + B.y); // 创建临时 Point 对象,其 x 为 A.x+B.x,y 为 A.y+B.y
}
// 重载输出运算符 <<,用于输出 Point 对象
// 参数:输出流引用,Point 对象的常引用
// 返回:输出流引用,支持链式调用(如 cout << a << b)
ostream& operator<<(ostream &out, const Point& p) {
out << "(" << p.x << "," << p.y << ")"; // 输出格式:(x,y)
return out; // 返回输出流,支持连续输出
}
// 主函数,程序入口点
int main() {
// 创建两个 Point 对象
Point a;
;
a.x = ;
cout << a + b << ;
;
}
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10000;
int main() {
int n, q, x, a[maxn], kase = 0;
while(scanf("%d%d", &n, &q) == 2 && n) {
printf("CASE# %d:\n", ++kase);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n); // 排序
while(q--) {
scanf("%d", &x);
int p = lower_bound(a, a + n, x) - a; // 在已排序数组 a 中寻找 x
if(a[p] == x)
printf("%d found at %d\n", x, p + 1);
else
printf("%d not found\n", x);
}
}
return 0;
}
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn]; // 每个 pile[i]是一个 vector
// 找木块 a 所在的 pile 和 height,以引用的形式返回调用者
void find_block(int a, int& p, int& h) {
for(p = 0; p < n; p++)
for(h = 0; h < pile[p].size(); h++)
if(pile[p][h] == a) return;
}
// 把第 p 堆高度为 h 的木块上方的所有木块移回原位
void clear_above(int p, int h) {
for(int i = h + 1; i < pile[p].size(); i++) {
int b = pile[p][i];
pile[b].push_back(b); // 把木块 b 放回原位
}
pile[p].resize(h + 1); // pile 只应保留下标 0~h 的元素
}
// 把第 p 堆高度为 h 及其上方的木块整体移动到 p2 堆的顶部
void {
( i = h; i < pile[p].(); i++)
pile[p2].(pile[p][i]);
pile[p].(h);
}
{
( i = ; i < n; i++) {
(, i);
( j = ; j < pile[i].(); j++)
(, pile[i][j]);
();
}
}
{
a, b;
cin >> n;
string s1, s2;
( i = ; i < n; i++)
pile[i].(i);
(cin >> s1 >> a >> s2 >> b) {
pa, pb, ha, hb;
(a, pa, ha);
(b, pb, hb);
(pa == pb) ;
(s2 == ) (pb, hb);
(s1 == ) (pa, ha);
(pa, ha, pb);
}
();
;
}
#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
set<string> dict; // string 集合
int main() {
string s, buf;
while(cin >> s) {
for(int i = 0; i < s.length(); i++)
if(isalpha(s[i])) s[i] = tolower(s[i]);
else s[i] = ' ';
stringstream ss(s);
while(ss >> buf) dict.insert(buf);
}
for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
cout << *it << "\n";
return 0;
}
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
map<string, int> cnt;
vector<string> words; // 将单词 s 进行'标准化'
string repr(const string &s) {
string ans = s;
for(int i = 0; i < ans.length(); i++)
ans[i] = tolower(ans[i]);
sort(ans.begin(), ans.end());
return ans;
}
int main() {
int n = 0;
string s;
while(cin >> s) {
if(s[0] == '#') break;
words.push_back(s);
string r = repr(s);
if(!cnt.count(r)) cnt[r] = 0;
cnt[r]++;
}
vector<string> ans;
for(int i = 0; i < words.size(); i++)
(cnt[(words[i])] == )
ans.(words[i]);
(ans.(), ans.());
( i = ; i < ans.(); i++)
cout << ans[i] << ;
;
}
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
map<set<int>, int> mp;
vector<set<int>> v;
stack<int> st;
int findId(set<int> sse) {
if(!mp.count(sse)) {
v.push_back(sse);
mp[sse] = v.size() - 1;
}
return mp[sse];
}
int main() {
int t, n, i;
string s;
cin >> t;
while(t--) {
cin >> n;
while(n--) {
cin >> s;
if(s == "PUSH") {
st.push(findId(set<int>()));
} else if(s == "DUP") {
st.push(st.top());
} {
id1 = st.(); st.();
id2 = st.(); st.();
set<> sse;
(s == ) {
(v[id1].(), v[id1].(), v[id2].(), v[id2].(), (sse, sse.()));
st.((sse));
}
(s == ) {
(v[id1].(), v[id1].(), v[id2].(), v[id2].(), (sse, sse.()));
st.((sse));
}
(s == ) {
sse = v[id2];
sse.(id1);
st.((sse));
}
}
cout << v[st.()].() << endl;
}
cout << << endl;
}
;
}
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
const int maxt = 1000 + 10;
int main() {
int t, kase = 0;
while(scanf("%d", &t) == 1 && t) {
printf("Scenario #%d\n", ++kase);
// 记录所有人的团队编号 map<int,int> team; // team[x]表示编号为 x 的人所在的团队编号
for(int i = 0; i < t; i++) {
int n, x;
scanf("%d", &n);
while(n--) {
scanf("%d", &x);
team[x] = i;
}
}
// 模拟 queue<int> q, q2[maxt]; // q 是团队的队列,而 q2[i]是团队 i 成员的队列
for(;;) {
int x;
char cmd[10];
scanf("%s", cmd);
if(cmd[0] == 'S') break;
else if(cmd[0] == 'D') {
int t = q.();
(, q2[t].());
q2[t].();
(q2[t].()) q.();
} (cmd[] == ) {
(, &x);
t = team[x];
(q2[t].()) q.(t);
q2[t].(x);
}
}
();
}
;
}
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int coeff[3] = {2, 3, 5};
int main() {
priority_queue<LL, vector<LL>, greater<LL>> pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i = 1;; i++) {
LL x = pq.top();
pq.pop();
if(i == 1500) {
cout << "The 1500'th ugly number is " << x << ".\n";
break;
}
for(int j = 0; j < 3; j++) {
LL x2 = x * coeff[j];
if(!s.count(x2)) {
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}
// 答案:859963392。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int maxcol = 60;
const int maxn = 100 + 5;
string filenames[maxn];
// 输出字符串 s,长度不足 len 时补字符 extra
void print(const string &s, int len, char extra) {
cout << s;
for(int i = 0; i < len - s.length(); i++)
cout << extra;
}
int main() {
int n;
while(cin >> n) {
int M = 0;
for(int i = 0; i < n; i++) {
cin >> filenames[i];
M = max(M, (int)filenames[i].length()); // STL 的 max
}
// 计算列数 cols 和行数 rows
int cols = (maxcol - M) / (M + 2) + 1, rows = (n - 1) / cols + 1;
print("", 60, '-');
cout << ;
(filenames, filenames + n);
( r = ; r < rows; r++) {
( c = ; c < cols; c++) {
idx = c * rows + r;
(idx < n)
(filenames[idx], c == cols - ? M : M + , );
}
cout << ;
}
}
;
}
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
void parse_address(const string &s, string &user, string &mta) {
int k = s.find('@');
user = s.substr(0, k);
mta = s.substr(k + 1);
}
int main() {
int k;
string s, t, user1, mta1, user2, mta2;
set<string> addr; // 输入所有 MTA,转化为地址列表
while(cin >> s && s != "*") {
cin >> s >> k;
while(k--) {
cin >> t;
addr.insert(t + "@" + s);
}
}
while(cin >> s && s != "*") {
parse_address(s, user1, mta1); // 处理发件人地址
vector<string> mta; // 所有需要连接的 mta,按照输入顺序
map<string, vector<string>> dest; // 每个 MTA 需要发送的用户
set<string> vis;
while(cin >> t && t != "*") {
parse_address(t, user2, mta2); // 处理收件人地址
if(vis.(t)) ;
vis.(t);
(!dest.(mta2)) {
mta.(mta2);
dest[mta2] = <string>();
}
dest[mta2].(t);
}
(cin, t);
string data;
((cin, t) && t[] != )
data += + t + ;
( i = ; i < mta.(); i++) {
string mta2 = mta[i];
vector<string> users = dest[mta2];
cout << << mta1 << << mta2 << endl;
cout << << mta1 << ;
cout << ;
cout << << s << ;
cout << ;
ok = ;
( i = ; i < users.(); i++) {
cout << << users[i] << ;
(addr.(users[i])) {
ok = ;
cout << ;
}
cout << ;
}
(ok) {
cout << ;
cout << ;
cout << data;
cout << ;
cout << ;
}
cout << ;
cout << ;
}
}
;
}
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100 + 5;
struct Building {
int id;
double x, y, w, d, h;
bool operator<(const Building &rhs) const {
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
} b[maxn];
int n;
double x[maxn * 2];
bool cover(int i, double mx) {
return b[i].x <= mx && b[i].x + b[i].w >= mx;
}
// 判断建筑物 i 在 x=mx 处是否可见
bool visible(int i, double mx) {
if(!cover(i, mx)) return false;
for(int k = 0; k < n; k++)
if(b[k].y < b[i].y && b[k].h >= b[i].h && cover(k, mx))
return false;
return true;
}
int main() {
int kase = ;
((, &n) == && n) {
( i = ; i < n; i++) {
(, &b[i].x, &b[i].y, &b[i].w, &b[i].d, &b[i].h);
x[i * ] = b[i].x;
x[i * + ] = b[i].x + b[i].w;
b[i].id = i + ;
}
(b, b + n);
(x, x + n * );
m = (x, x + n * ) - x;
(kase++) ();
(, kase, b[].id);
( i = ; i < n; i++) {
vis = ;
( j = ; j < m - ; j++)
((i, (x[j] + x[j + ]) / )) {
vis = ;
;
}
(vis) (, b[i].id);
}
();
}
;
}
#include <iostream>
#include <sstream>
#include <vector>
#include <string.h>
using namespace std;
int main() {
vector<vector<string>> v;
string line, word;
int i, j, k;
int vlen[200];
while(getline(cin, line)) {
stringstream ss(line);
vector<string> vs;
while(ss >> word) vs.push_back(word);
v.push_back(vs);
}
memset(vlen, 0, 200);
for(i = 0; i < v.size(); ++i) {
for(j = 0; j < v[i].size(); ++j) {
vlen[j] = v[i][j].length() > vlen[j] ? v[i][j].length() : vlen[j];
}
}
for(i = 0; i < v.size(); ++i) {
for(j = 0; j < v[i].size(); ++j) {
cout << v[i][j];
if(j != v[i].size() - 1) {
for(k = vlen[j] - v[i][j].size(); k > 0; --k) cout << ;
cout << ;
}
}
cout << endl;
}
;
}
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int arr[1005][2005];
int n;
int judge(int i) {
int j, k;
for(j = 0; j < n; ++j) {
if(arr[i][j] != 0) break;
}
if(j == n) return 1;
for(k = 0; k < i; ++k) {
for(j = 0; j < n; ++j) {
if(arr[i][j] != arr[k][j]) break;
}
if(j == n) return 2;
}
return 0;
}
int main() {
int m, i, j, k;
int flag;
cin >> m;
while(m--) {
memset(arr, 0, 1005 * 2005);
cin >> n;
for(i = 0; i < n; ++i) cin >> arr[0][i];
i = 0;
while() {
flag = (i);
(flag != ) ;
++i;
arr[i][n - ] = (arr[i - ][] - arr[i - ][n - ]);
(j = ; j < n; ++j) arr[i][j - ] = (arr[i - ][j] - arr[i - ][j - ]);
}
(flag == ) cout << << endl;
cout << << endl;
}
;
}
#include <queue>
#include <iostream>
using namespace std;
int main() {
int n, i, j, k;
while(cin >> n && n) {
queue<int> qu;
for(i = 1; i <= n; ++i) qu.push(i);
cout << "Discarded cards:";
while(qu.size() > 1) {
cout << " " << qu.front();
if(qu.size() > 2) cout << ",";
qu.pop();
j = qu.front();
qu.pop();
qu.push(j);
}
cout << endl;
cout << "Remaining card: " << qu.front() << endl;
}
return 0;
}
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, a, b, i, j, k;
while(scanf("%d", &n) && n) {
vector<int> lv, rv;
for(i = 0; i < n; ++i) {
scanf("%d %d", &a, &b);
lv.push_back(a);
rv.push_back(b);
}
sort(lv.begin(), lv.end());
sort(rv.begin(), rv.end());
for(i = 0; i < n; ++i) {
if(lv[i] != rv[i]) break;
}
if(i != n) puts("NO");
else puts("YES");
}
return 0;
}
#include <set>
#include <iostream>
#include <string>
using namespace std;
int main() {
set<string> se;
string s;
int size, i;
bool flag;
while(cin >> s) se.insert(s);
for(auto ip = se.begin(); ip != se.end(); ++ip) {
size = ip->length() - 1;
flag = false;
for(i = 1; i < size; ++i) {
string s1 = ip->substr(0, i);
string s2 = ip->substr(i);
if(se.count(s1) && se.count(s2)) {
flag = true;
break;
}
}
if(flag == true) cout << *ip << endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct point {
int x, y;
};
bool cmp1(const point &lhs, const point &rhs) {
if(lhs.x != rhs.x) return lhs.x < rhs.x;
return lhs.y < rhs.y;
}
bool cmp2(const point &lhs, const point &rhs) {
if(lhs.x != rhs.x) return lhs.x < rhs.x;
return lhs.y > rhs.y;
}
int main() {
int t, n, i, j, k, a, b;
cin >> t;
bool cut2, flag;
while(t--) {
vector<point> v;
cin >> n;
for(i = 0; i < n; ++i) {
point p;
cin >> p.x >> p.y;
v.push_back(p);
}
sort(v.begin(), v.end(), cmp1);
int beg1 = 0, end1, beg2, end2 = n - 1, xcut;
cut2 = false;
if(n % 2 == 0) {
end1 = n / - ;
beg2 = n / ;
sum = v[beg2].x + v[end1].x;
xcut = sum / ;
(sum % != ) cut2 = ;
} {
xcut = v[n / ].x;
end1 = n / - ;
beg2 = n / + ;
}
(v.() + beg2, v.(), cmp2);
flag = ;
(i = beg1; i <= end1; ++i) {
a = i;
b = end2 - i;
(v[a].x == xcut && v[b].x == xcut) ;
(v[a].y != v[b].y) {
flag = ;
;
}
(!cut2) {
((xcut - v[a].x) != (v[b].x - xcut)) {
flag = ;
;
}
}
(cut2) {
((xcut - v[a].x) != (v[b].x - xcut - )) {
flag = ;
;
}
}
}
(flag) cout << << endl;
cout << << endl;
}
;
}
#include <stdio.h>
#include <queue>
using namespace std;
int m;
int findTime(queue<int>& qu, priority_queue<int>& qup) {
int time = 0;
int k, kt;
while(1) {
k = qu.front();
kt = qup.top();
qu.pop();
--m;
if(k == kt) {
++time;
qup.pop();
if(m == -1) return time;
} else {
qu.push(k);
if(m == -1) m = qu.size() - 1;
}
}
}
int main() {
int t, n, i, k;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
queue<int> qu;
priority_queue<int> qup;
for(i = 0; i < n; ++i) {
scanf("%d", &k);
qu.push(k);
qup.push(k);
}
(, (qu, qup));
}
;
}
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
struct book {
string title;
string author;
int has;
bool operator<(const book &rhs) {
if(this->author != rhs.author) return this->author < rhs.author;
else return this->title < rhs.title;
}
};
vector<book> v;
map<string, int> mp;
book getBook(string s) {
book b;
s = s.substr(1);
int pos = s.find("\"");
b.title = s.substr(0, pos);
b.author = s.substr(pos + 5);
b.has = 0;
return b;
}
void shelve() {
int i;
string fore = "";
for(i = 0; i < v.size(); ++i) {
if(v[i].has == ) fore = v[i].title;
(v[i].has == ) {
cout << << v[i].title << ;
(fore == ) cout << ;
cout << << fore << ;
cout << endl;
v[i].has = ;
fore = v[i].title;
}
}
cout << << endl;
}
{
string s, out;
book b;
i;
((cin, s) && s != ) {
b = (s);
v.(b);
}
(v.(), v.());
(i = ; i < v.(); ++i) mp[v[i].title] = i;
((cin, s) && s != ) {
string st = s.(, );
(st == ) {
();
;
}
string name = s.(, s.() - );
(st == ) v[mp[name]].has = ;
(st == ) v[mp[name]].has = ;
}
;
}
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stdlib.h>
#include <stack>
#define MAX 60
using namespace std;
struct Array {
int size;
map<int, int> mp;
};
Array arr[MAX];
int bug;
int getValue(string s) {
stack<int> st;
int a = 0, av = 0;
while(s.length()) {
if(s[0] >= 'A' && s[0] <= 'z') {
st.push(s[0] - 'A');
s = s.substr(2, s.length() - 3);
} else {
av = atoi(s.c_str());
break;
}
}
while(st.size()) {
a = st.top();
(av >= arr[a].size || !arr[a].mp.(av)) {
bug = ;
;
}
av = arr[a].mp[av];
st.();
}
av;
}
{
Array a;
i = line[] - ;
(i < || i > MAX) {
bug = ;
;
}
line = line.(, line.() - );
num = (line.());
a.size = num;
arr[i] = a;
}
{
cut = line.();
string front, after;
front = line.(, cut);
after = line.(cut + );
a1, v1, v2;
a1 = front[] - ;
(a1 < || a1 > MAX) {
bug = ;
;
}
v1 = (front.(, front.() - ));
(bug) ;
(v1 >= arr[a1].size) {
bug = ;
;
}
v2 = (after);
(bug) ;
arr[a1].mp[v1] = v2;
}
{
pos = line.();
(pos < ) (line);
(line);
}
{
string line;
i, j, k, lineNum;
((cin, line) && line != ) {
(i = ; i < MAX; ++i) {
arr[i].size = ;
arr[i].mp.();
}
bug = ;
lineNum = ;
(line);
((cin, line) && line != ) {
(!bug) {
++lineNum;
(line);
}
}
(!bug) lineNum = ;
cout << lineNum << endl;
}
;
}
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
#include <set>
#include <algorithm>
using namespace std;
struct DID {
int doci;
int linei;
bool hole;
bool operator==(const DID &rhs) {
if(this->doci != rhs.doci) return false;
if(this->hole && rhs.hole) return true;
return this->linei == rhs.linei;
}
};
vector<vector<string>> docs;
map<string, vector<DID>> mp;
set<string> se = {"a", "the", "to", "and", "or", "not"};
void getDocs(int i) {
vector<string> doc;
string line, word;
int j, k = 0;
while(getline(cin, line) && line != "**********") {
doc.push_back(line);
(j = ; j < line.(); ++j) {
(line[j] >= && line[j] <= ) line[j] = (line[j] - ) + ;
(line[j] < || line[j] > ) line[j] = ;
}
;
(ss >> word) {
(!se.(word)) {
DID d;
d.doci = i;
d.linei = k;
d.hole = ;
(!mp.(word)) mp[word] = <DID>();
mp[word].(d);
}
}
++k;
}
docs.(doc);
}
vector<DID> EMPTYVEC;
{
(!mp.(word)) EMPTYVEC;
mp[word];
}
{
(!mpv.()) {
cout << << endl;
;
}
i, j, k;
(i = ; i < mpv.(); ++i) {
(i != && mpv[i].doci != mpv[i - ].doci) cout << << endl;
(mpv[i].hole) {
k = mpv[i].doci;
(j = ; j < docs[k].(); ++j) cout << docs[k][j] << endl;
;
}
cout << docs[mpv[i].doci][mpv[i].linei] << endl;
}
}
{
set<> vse;
i;
vector<DID>& v = (word);
(i = ; i < v.(); ++i) vse.(v[i].doci);
vector<DID> ve;
(i = ; i < docs.(); ++i) {
(!vse.(i)) {
DID d;
d.doci = i;
d.linei = ;
d.hole = ;
ve.(d);
}
}
(ve);
}
{
set<> vse1, vse2;
i;
(i = ; i < mpv(); ++i) vse(mpv1[i].doci);
(i = ; i < mpv(); ++i) vse(mpv2[i].doci);
(i = ; i < mpv(); ++i) {
(vse(mpv1[i].doci)) mpv.(mpv1[i]);
}
(i = ; i < mpv(); ++i) {
(vse(mpv2[i].doci)) mpv.(mpv2[i]);
}
}
{
(lhs.doci != rhs.doci) lhs.doci < rhs.doci;
lhs.linei < rhs.linei;
}
{
string query, word;
i;
(cin, query);
;
vector<string> v;
(ss >> word) v.(word);
vector<DID> mpv;
(v.() == ) mpv = (v[]);
(v.() == ) {
(v[]);
;
} {
vector<DID>& mpv1 = (v[]), &mpv2 = (v[]);
(v[] == ) {
mpv = mpv1;
(i = ; i < mpv(); ++i) mpv.(mpv2[i]);
} {
(mpv, mpv1, mpv2);
}
}
(mpv.(), mpv.(), cmp);
mpv.((mpv.(), mpv.()), mpv.());
(mpv);
}
{
N, M;
i, j, k;
string s;
cin >> N;
(cin, s);
(i = ; i < N; ++i) (i);
cin >> M;
(cin, s);
(M--) {
();
cout << << endl;
}
;
}
#include <iostream>
#include <string>
#include <map>
#include <sstream>
using namespace std;
void getMap(map<string, string>& mp, string &line) {
int i, j, k;
for(i = 0; i < line.length(); ++i) {
if((line[i] < 'a' || line[i] > 'z') && (line[i] < '0' || line[i] > '9')) line[i] = ' ';
}
stringstream ss(line);
string word1, word2;
while(ss >> word1 >> word2) mp[word1] = word2;
}
int main() {
int n;
bool flag[3];
cin >> n;
string line1, line2;
getline(cin, line1);
while(n--) {
getline(cin, line1);
getline(cin, line2);
map<string, string> mp1, mp2;
getMap(mp1, line1);
getMap(mp2, line2);
flag[0] = false;
flag[1] = false;
flag[2] = false;
for( i = mp(); i != mp(); ++i) {
(!mp(i->first)) {
(flag[] == ) {
flag[] = ;
cout << << i->first;
} cout << << i->first;
}
}
(flag[] == ) cout << endl;
( i = mp(); i != mp(); ++i) {
(!mp(i->first)) {
(flag[] == ) {
flag[] = ;
cout << << i->first;
} cout << << i->first;
}
}
(flag[] == ) cout << endl;
( i = mp(); i != mp(); ++i) {
(mp(i->first) && i->second != mp1[i->first]) {
(flag[] == ) {
flag[] = ;
cout << << i->first;
} cout << << i->first;
}
}
(flag[] == ) cout << endl;
(!flag[] && !flag[] && !flag[]) cout << << endl;
cout << endl;
}
;
}
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct LOC {
double x, y;
};
struct MAP {
double x1, x2, y1, y2;
double cenx, ceny;
double radio;
double area;
string name;
};
vector<MAP> vm;
map<string, vector<int>> mp;
double xt, yt;
double compDis(MAP &m, int x, int y) {
return (m.cenx - x) * (m.cenx - x) + (m.ceny - y) * (m.ceny - y);
}
double compDisRC(MAP &m, int x, int y) {
return (m.x2 - x) * (m.x2 - x) + (m.y2 - y) * (m.y2 - y);
}
bool cmp(int l, int r) {
MAP &m1 = vm[l], &m2 = vm[r];
if(m1.area != m2.area) return m1.area > m2.area;
double dis1 = compDis(m1, xt, yt), dis2 = (m2, xt, yt);
(dis1 != dis2) dis1 < dis2;
(mradio != mradio) mradio < mradio;
dis1 = (m1, xt, yt);
dis2 = (m2, xt, yt);
(dis1 != dis2) dis1 > dis2;
mx1 < mx1;
}
{
string line, word;
x1, x2, y1, y2;
i, j, t;
(cin, line);
(cin >> word && word != ) {
MAP m;
m.name = word;
cin >> x1 >> y1 >> x2 >> y2;
(x1 > x2) { m.x1 = x2; m.x2 = x1; }
{ m.x2 = x2; m.x1 = x1; }
(y1 > y2) { m.y1 = y2; m.y2 = y1; }
{ m.y2 = y2; m.y1 = y1; }
m.cenx = (m.x1 + m.x2) / ;
m.ceny = (m.y1 + m.y2) / ;
m.radio = (m.x2 - m.x1) / (m.y2 - m.y1);
m.radio = m.radio - ;
(m.radio < ) m.radio = -m.radio;
m.area = (m.x2 - m.x1) * (m.y2 - m.y1);
vm.(m);
}
(cin >> word && word != ) {
cin >> xt >> yt;
vector<> v;
(i = ; i < vm.(); ++i) {
(vm[i].x1 <= xt && vm[i].x2 >= xt && vm[i].y1 <= yt && vm[i].y2 >= yt) v.(i);
}
(v.(), v.(), cmp);
vector<> vt;
(i = ; i < v.(); ++i) {
(i == ) { vt.(v[]); ; }
(vm[vt[vt.() - ]].area == vm[v[i]].area) ;
vt.(v[i]);
}
mp[word] = vt;
}
(cin >> word && word != ) {
cin >> t;
cout << word << << t << ;
(!mp.(word)) cout << ;
(mp[word].() == ) cout << ;
(mp[word].() >= t) cout << << vm[mp[word][t - ]].name;
cout << << vm[mp[word][mp[word].() - ]].name;
cout << endl;
}
;
}
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct REQ {
int id, num, elap, serv, btwe;
};
vector<REQ> reqs;
struct PER {
int id;
vector<int> v;
int eart, endt;
};
vector<PER> pers;
int MAXTOPLEN;
struct TIME {
int t;
int flag;
int i;
bool operator==(const TIME & rhs) const {
if(this->t == rhs.t && this->i == rhs.i && this->flag == rhs.flag) return true;
return false;
}
bool operator<(const TIME & rhs) const {
if(this->t != rhs.t) return this->t < rhs.t;
if(this->flag != rhs.flag) return this->flag < rhs.flag;
return this->i < rhs.i;
}
>( TIME & rhs) {
(* == rhs || * < rhs) ;
;
}
};
{
(lhs.t != rhs.t) lhs.t < rhs.t;
lhs.i < rhs.i;
}
{
(pers[i].eart > pers[j].eart) j;
(pers[i].eart < pers[j].eart) i;
(pers[i].id > pers[j].id) j;
(pers[i].id < pers[j].id) i;
}
{
i, j, k;
flag;
choose;
(i = ; i < MAXTOPLEN; ++i) {
flag = ;
(j = ; j < pers.(); ++j) {
(pers[j].v.() <= i || pers[j].endt > t) ;
(pers[j].v[i] == reqs[r].id) {
(flag == ) {
flag = ;
choose = j;
} {
choose = (j, choose);
}
}
}
(flag == ) choose;
}
;
}
{
t = , i, j, k;
TIME ti, tt;
priority_queue<TIME, vector<TIME>, greater<TIME>> pq;
queue<> qth;
(i = ; i < reqs.(); ++i) {
(j = ; j < reqs[i].num; ++j) {
TIME tim;
tim.t = reqs[i].elap + reqs[i].btwe * j;
tim.i = i;
tim.flag = ;
pq.(tim);
}
}
(pq.() || qth.()) {
(pq.()) {
ti = pq.();
pq.();
(ti.flag > ) qth.(ti.i);
t = ti.t;
(pq.()) {
tt = pq.();
(tt.t == t) {
(tt.flag > ) qth.(tt.i);
pq.();
} ;
}
}
k = qth.();
(i = ; i < k; ++i) {
j = qth.();
qth.();
perCho = (j, t);
(perCho < ) { qth.(j); ; }
pers[perCho].eart = t;
pers[perCho].endt = t + reqs[j].serv;
TIME tim;
tim.t = pers[perCho].endt;
tim.flag = ;
pq.(tim);
}
}
t;
}
{
m, n, i, j, k, t = ;
(cin >> m && m != ) {
reqs.();
pers.();
MAXTOPLEN = ;
(m--) {
REQ r;
cin >> r.id >> r.num >> r.elap >> r.serv >> r.btwe;
reqs.(r);
}
cin >> n;
(n--) {
PER p;
cin >> p.id >> i;
(i > MAXTOPLEN) MAXTOPLEN = i;
(i--) {
cin >> j;
p.v.(j);
}
p.eart = ;
p.endt = ;
pers.(p);
}
k = ();
cout << << ++t << << k << << endl;
}
;
}
#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;
struct MES {
int id;
int size;
int price;
int bs;
bool operator<(const MES &rhs) const {
if(this->price != rhs.price) return this->price < rhs.price;
return this->id > rhs.id;
}
bool operator>(const MES &rhs) const {
if(this->price != rhs.price) return this->price > rhs.price;
return this->id > rhs.id;
}
};
vector<MES> vmes;
priority_queue<MES, vector<MES>, less<MES>> buys;
priority_queue<MES, vector<MES>, greater<MES>> sells;
map<int, int> mpBuy, mpSell;
void deleteMes(int id) {
if(id >= vmes.size() || !vmes[id].size) return;
if(vmes[id].bs > 0) mpBuy[vmes[id].price] -= vmes[id].size;
mpSell[vmes[id].price] -= vmes[id].size;
vmes[id].size = ;
}
{
MES m1, m2;
size, price;
(!sells.() && m.size) {
m1 = sells.();
(vmes[mid].size == ) { sells.(); ; }
(vmes[mid].price > m.price) ;
price = vmes[mid].price;
(m.size >= vmes[mid].size) {
size = vmes[mid].size;
sells.();
} size = m.size;
mpSell[price] -= size;
m.size -= size;
vmes[mid].size -= size;
cout << << size << << price << endl;
}
}
{
MES m1, m2;
size, price;
(!buys.() && m.size) {
m1 = buys.();
(vmes[mid].size == ) { buys.(); ; }
(vmes[mid].price < m.price) ;
price = vmes[mid].price;
(m.size >= vmes[mid].size) {
size = vmes[mid].size;
buys.();
} size = m.size;
mpBuy[price] -= size;
m.size -= size;
vmes[mid].size -= size;
cout << << size << << price << endl;
}
}
{
n, i, j, k;
id;
string s;
MES m1, m2;
size, price;
flag = ;
(cin >> n && n != ) {
(flag == ) flag = ;
cout << endl;
priority_queue<MES, vector<MES>, less<MES>> buysT;
priority_queue<MES, vector<MES>, greater<MES>> sellsT;
buys = buysT;
sells = sellsT;
mpBuy.();
mpSell.();
vmes.();
(i = ; i < n; ++i) {
MES m;
m.id = i;
cin >> s;
(s == ) {
cin >> id;
id = id - ;
(id);
m.price = ;
m.size = ;
} {
cin >> m.size >> m.price;
(s == ) {
m.bs = ;
(m);
(m.size > ) {
(!mpBuy.(m.price)) mpBuy[m.price] = ;
mpBuy[m.price] += m.size;
buys.(m);
}
} {
m.bs = ;
(m);
(m.size > ) {
(!mpSell.(m.price)) mpSell[m.price] = ;
mpSell[m.price] += m.size;
sells.(m);
}
}
}
vmes.(m);
cout << ;
size = ;
price = ;
(!buys.()) {
m1 = buys.();
(vmes[mid].size == ) { buys.(); ; }
price = vmes[mid].price;
size = mpBuy[vmes[mid].price];
;
}
cout << size << << price;
cout << ;
size = ;
price = ;
(!sells.()) {
m1 = sells.();
(vmes[mid].size == ) { sells.(); ; }
price = vmes[mid].price;
size = mpSell[vmes[mid].price];
;
}
cout << size << << price;
cout << endl;
}
}
;
}
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct BigInt {
static const long long BASE = 1000000000000000000;
static const int WIDTH = 18;
static const int LEN = 4;
vector<long long> s;
BigInt(long long num = 0) { *this = num; }
BigInt operator=(long long num) {
s.clear();
do {
s.push_back(num % BASE);
num /= BASE;
} while(num > 0);
return *this;
}
BigInt operator+(const BigInt &b) {
BigInt c;
c.s.clear();
long long g = 0, x;
int ssize = s.(), bssize = b.s.();
( i = ;; i++) {
(g == && i >= ssize && i >= bssize) ;
x = g;
(i < ssize) x += s[i];
(i < bssize) x += b.s[i];
c.s.(x % BASE);
g = x / BASE;
}
c;
}
{
string str, st;
i = s.() - , j = LEN;
(i >= && j > ) {
st = (s[i]);
(i != s.() - && st.() < WIDTH) st = + st;
str += st;
--i;
--j;
}
str.(, );
}
};
{
id;
num;
Node* childs[] = {};
};
vector<string> v;
{
i, j, n, num;
Node* tree = Node;
tree->id = ;
tree->num = ;
(i = ; i < v.(); ++i) {
n = v[i].();
Node* p = tree;
(j = ; j < n; ++j) {
num = v[i][j] - ;
(!p->childs[num]) {
p->childs[num] = Node;
p->childs[num]->id = i;
p->childs[num]->num = num;
}
p = p->childs[num];
}
}
tree;
}
{
s[];
i, j, T, n, num, res;
BigInt bv[];
bv[] = ;
bv[] = ;
v.();
v.();
(i = ; i < ; ++i) {
bv[i % ] = bv[(i - ) % ] + bv[(i - ) % ];
v.(bv[i % ].());
}
(, &T);
Node* tree = ();
(i = ; i < T; ++i) {
(, s);
n = (s);
Node* p = tree;
(j = ; j < n; ++j) {
num = s[j] - ;
(!p->childs[num]) ;
p = p->childs[num];
}
(j == n) res = p->id;
res = ;
(, i + , res);
}
;
}
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int oproomNum, reroomNum, startTime, transTime, preop, prere, patiNum;
struct Patient {
char name[105];
int opTime, reTime;
int opNum, opBeg, opEnd, reNum, reBeg, reEnd;
};
vector<Patient> v;
struct Room {
int useTime;
bool use;
int endTime;
};
vector<Room> opV;
vector<Room> reV;
// type 说明
// 1 开始 oproom
// -1 准备 oproom
struct PrioItem {
int time;
int type;
int pai;
int roomi;
bool operator<(const PrioItem &rhs) const {
if(time != rhs.time) return time > rhs.time;
if(type != rhs.type) return type > rhs.type;
return roomi > rhs.roomi;
}
};
int pai, timeNow, endTime;
priority_queue<PrioItem> pq;
// 病人开始手术
void useOpNum(int pai, int roomi) {
PrioItem pi;
opV[roomi].use = true;
opV[roomi].useTime += v[pai].opTime;
opV[roomi].endTime = timeNow + v[pai].opTime + preop;
v[pai].opNum = roomi;
v[pai].opBeg = timeNow;
v[pai].opEnd = timeNow + v[pai].opTime;
pi.time = v[pai].opEnd;
pi.type = ;
pi.pai = pai;
pi.roomi = roomi;
pq.(pi);
}
{
(pai >= patiNum) ;
( i = ; i < opV.(); ++i) {
(opV[i].endTime <= timeNow) (pai++, i);
}
}
{
i, j, k;
(i = ; i < reroomNum; ++i) {
(reV[i].endTime <= timeNow) ;
}
(i == reroomNum) {
;
}
reV[i].useTime += v[pi.pai].reTime;
v[pi.pai].reNum = i;
v[pi.pai].reBeg = timeNow + transTime;
v[pi.pai].reEnd = v[pi.pai].reBeg + v[pi.pai].reTime;
(v[pi.pai].reEnd > endTime) endTime = v[pi.pai].reEnd;
reV[i].endTime = v[pi.pai].reEnd + prere;
PrioItem piNew2;
piNewtype = ;
piNewroomi = pi.roomi;
piNewtime = timeNow + preop;
pq.(piNew2);
}
{
i, j, k;
timeNow = startTime;
PrioItem pi;
(pai = ; pai < oproomNum && pai < patiNum; ++pai) (pai, pai);
(pq.()) {
pi = pq.();
pq.();
timeNow = pi.time;
(pi.type) {
: (pi); ;
: (pi); ;
}
}
(pai != patiNum) {
;
}
}
{
hour = t / ;
min = t % ;
(, hour, min);
}
{
();
();
();
i;
(i = ; i < v.(); ++i) {
(, i + , v[i].name, v[i].opNum + );
(v[i].opBeg);
();
(v[i].opEnd);
(, v[i].reNum + );
(v[i].reBeg);
();
(v[i].reEnd);
();
}
}
{
(endTime == startTime) ;
(type == ) ()opV[i].useTime / (endTime - startTime) * ;
(type == ) ()reV[i].useTime / (endTime - startTime) * ;
}
{
();
();
();
i;
(i = ; i < opV.(); ++i) (, i + , opV[i].useTime, (i, ));
(i = ; i < reV.(); ++i) (, i + , reV[i].useTime, (i, ));
}
{
i, j;
((, &oproomNum, &reroomNum, &startTime, &transTime, &preop, &prere, &patiNum) == ) {
v.();
opV.();
reV.();
pq = <PrioItem>();
startTime = startTime * ;
endTime = startTime;
(i = ; i < oproomNum; ++i) {
Room m;
m.use = ;
m.useTime = ;
opV.(m);
}
(i = ; i < reroomNum; ++i) {
Room m;
m.use = ;
m.useTime = ;
m.endTime = ;
reV.(m);
}
(i = ; i < patiNum; ++i) {
Patient p;
(, p.name);
(, &p.opTime, &p.reTime);
v.(p);
}
();
();
();
();
();
}
;
}

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online