0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 15:58:47 +00:00

Compare commits

...

4 Commits

4 changed files with 283 additions and 0 deletions

56
Luogu/CF1041C/CF1041C.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e5 + 5;
int n, m, k, ans[N];
std::set<std::pair<int, int>> set;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i = 1, x; i <= n; i++) {
cin >> x;
set.emplace(x, i);
}
int x = set.begin()->first,
cnt = 1;
ans[set.begin()->second] = 1;
set.erase(set.begin());
for (int i = 2; i <= n; i++) {
auto it = set.lower_bound(std::make_pair(x + k + 1, 0));
if (it != set.end()) {
ans[it->second] = cnt;
} else {
it = set.begin();
ans[it->second] = ++cnt;
}
x = it->first;
set.erase(it);
}
cout << cnt << endl;
for (int i = 1; i <= n; i++) {
cout << ans[i] << ' ';
}
cout << endl;
return 0;
}

76
Luogu/P1120/P1120.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 70;
int n, m, l, a[N], next[N];
bool vis[N]{false, true};
void dfs(int now, int last, int rest) {
if (!rest) {
if (now == m) {
cout << l << endl;
exit(0);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
vis[i] = true;
dfs(now + 1, i, l - a[i]);
vis[i] = false;
break;
}
}
}
int p = std::lower_bound(a + last + 1, a + n + 1, rest, std::greater<int>()) - a;
for (int i = p; i <= n; i++) {
if (!vis[i]) {
vis[i] = true;
dfs(now, i, rest - a[i]);
vis[i] = false;
if (rest == a[i] || rest == l) return;
i = next[i];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int sum = std::accumulate(a + 1, a + 1 + n, 0);
std::sort(a + 1, a + 1 + n, std::greater<int>());
for (int i = n; i; i--) {
next[i] = a[i] == a[i + 1] ? next[i + 1] : i;
}
for (l = a[1]; l <= sum / 2; l++) {
if (sum % l) continue;
m = sum / l;
dfs(1, 1, l - a[1]);
}
cout << sum << endl;
return 0;
}

62
Luogu/P2602/P2602.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
long long a, b,
f[15], g[15]{1},
cnt_a[10], cnt_b[10];
std::vector<int> gen(long long x) {
std::vector<int> res;
while (x) {
res.push_back(x % 10);
x /= 10;
}
return res;
}
void calc(long long x, long long* r) {
auto num = gen(x);
for (int i = num.size(); i; i--) {
for (int j = 0; j < 10; j++) {
r[j] += f[i - 1] * num.at(i - 1);
}
for (int j = 0; j < num[i - 1]; j++) {
r[j] += g[i - 1];
}
x -= g[i - 1] * num[i - 1];
r[num[i - 1]] += x + 1;
r[0] -= g[i - 1];
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a >> b;
for (int i = 1; i <= 12; i++) {
f[i] = f[i - 1] * 10 + g[i - 1];
g[i] = g[i - 1] * 10;
}
calc(a - 1, cnt_a);
calc(b, cnt_b);
for (int i = 0; i < 10; i++) {
cout << cnt_b[i] - cnt_a[i] << ' ';
}
cout << endl;
return 0;
}

89
Luogu/UVA307/UVA307.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 70;
int n, m, l, a[N], next[N];
bool flag, vis[N];
void dfs(int now, int last, int rest) {
if (!rest) {
if (now == m) {
flag = true;
return;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
vis[i] = true;
dfs(now + 1, i, l - a[i]);
if (flag) return;
vis[i] = false;
break;
}
}
}
int p = std::lower_bound(a + last + 1, a + n + 1, rest, std::greater<int>()) - a;
for (int i = p; i <= n; i++) {
if (!vis[i]) {
vis[i] = true;
dfs(now, i, rest - a[i]);
vis[i] = false;
if (flag || rest == a[i] || rest == l) return;
i = next[i];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
while (cin >> n, n) {
flag = false;
std::fill_n(a, N, 0);
std::fill_n(next, N, 0);
std::fill_n(vis, N, false);
vis[1] = true;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int sum = std::accumulate(a + 1, a + 1 + n, 0);
std::sort(a + 1, a + 1 + n, std::greater<int>());
for (int i = n; i; i--) {
next[i] = a[i] == a[i + 1] ? next[i + 1] : i;
}
for (l = a[1]; l <= sum / 2; l++) {
if (sum % l) continue;
m = sum / l;
dfs(1, 1, l - a[1]);
if (flag) {
cout << l << endl;
break;
}
}
if (!flag) cout << sum << endl;
}
return 0;
}