1、中间数
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; i++) {
int d = 0, u = 0;
for (int j = 0; j < n; j++)
if (a[i] > a[j]) u++;
else if (a[i] < a[j]) d++;
if (u == d) {
cout << a[i] << endl;
return 0;
}
}
puts("-1");
return 0;
}
2、工资计算
枚举税前工资即可。
#include <iostream>
#include <algorithm>
using namespace std;
int a[] = {0, 1500, 4500, 9000, 35000, 55000, 80000, 1000000};
int b[] = {0, 3, 10, 20, 25, 30, 35, 45};
int get(int x) {
if (x <= 3500) return x;
x -= 3500;
int tax = 0;
for (int i = 1; i < 8; i++)
if (x >= a[i - 1])
tax += (min(x, a[i]) - a[i - 1]) / 100 * b[i];
return x + 3500 - tax;
}
int main() {
int T; cin >> T;
for (int i = 0;; i += 100)
if (get(i) == T) {
cout << i << endl; break;
}
return 0;
}
3、权限查询
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_map>
using namespace std;
#define x first
#define y second
unordered_map<string, int> p;
unordered_map<string, vector<pair<string, int>>> r;
unordered_map<string, vector<string>> u;
int main() {
int a; cin >> a;
while (a--) {
string str; cin >> str;
string sub1, sub2;
int x, j = 0;
while (j < str.size() && str[j] != ':') sub1 += str[j++];
if (str[j] == ':') {
j++;
while (j < str.size()) sub2 += str[j++];
x = atoi(sub2.c_str());
p[sub1] = x;
} else p[sub1] = -1;
}
cin >> a;
while (a--) {
string s; int cnt; cin >> s >> cnt;
vector<pair<string, int>> tmp;
while (cnt--) {
string str; cin >> str;
string sub1, sub2;
int x, j = 0;
while (j < str.size() && str[j] != ':') sub1 += str[j++];
if (str[j] == ':') {
j++;
while (j < str.size()) sub2 += str[j++];
x = atoi(sub2.c_str());
tmp.push_back({sub1, x});
} else tmp.push_back({sub1, -1});
}
r[s] = tmp;
}
cin >> a;
while (a--) {
string s; int cnt; cin >> s >> cnt;
vector<string> tmp;
while (cnt--) {
string str; cin >> str;
tmp.push_back(str);
}
u[s] = tmp;
}
cin >> a;
while (a--) {
string s, ss; cin >> s >> ss;
if (!u.count(s)) {
puts("false"); continue;
}
string sub1, sub2;
int x = -1, j = 0;
while (j < ss.size() && ss[j] != ':') sub1 += ss[j++];
if (!p.count(sub1)) {
puts("false"); continue;
}
if (ss[j] == ':') {
j++;
while (j < ss.size()) sub2 += ss[j++];
x = atoi(sub2.c_str());
bool flag = false;
auto f = u[s]; // 用户映射到角色
for (auto c: f) {// 在每一个角色中查找对应权限是否存在
auto ff = r[c];
for (auto cc: ff)
if (cc.x == sub1 && cc.y >= x) flag = true;
}
if (flag) puts("true"); else puts("false");
} else {
if (p[sub1] == -1) {
bool flag = false;
auto f = u[s];
for (auto c: f) {
auto ff = r[c];
for (auto cc: ff)
if (cc.x == sub1) flag = true;
}
if (flag) puts("true"); else puts("false");
} else {
int res = -1;
auto f = u[s];
for (auto c: f) {
auto ff = r[c];
for (auto cc: ff)
if (cc.x == sub1) res = max(res, cc.y);
}
if (res != -1) cout << res << endl;
else puts("false");
}
}
}
return 0;
}
4、压缩编码(区间DP)
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, s[N], f[N][N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) {
scanf("%d", s + i);
s[i] += s[i - 1];
}
for (int len = 2; len <= n; len ++ )
for (int i = 1; i + len - 1 <= n; i ++ ) {
int l = i, r = i + len - 1;
f[l][r] = 1e9;
for (int k = l; k < r; k ++ )
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
}
printf("%d\n", f[1][n]);
return 0;
}