Add files via upload

This commit is contained in:
Anthony Wang 2020-05-11 21:01:15 -05:00 committed by GitHub
commit 6760ebf14e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 161 additions and 0 deletions

43
pascal.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
int N;
cin >> N;
cout << "Case #" << t + 1 << ":\n";
if (N <= 500) {
for (int i = 0; i < N; ++i) cout << i + 1 << " " << 1 << '\n';
}
else {
N -= 32;
int sum = 0;
bool side = 0;
for (int i = 0; i < 32; ++i) {
cout << i + 1 << " " << (side ? i + 1 : 1) << '\n';
if (N & (1 << i)) {
if (side) {
for (int j = i; j > 0; --j) cout << i + 1 << " " << j << '\n';
side = 0;
}
else {
for (int j = 2; j <= i + 1; ++j) cout << i + 1 << " " << j << '\n';
side = 1;
}
sum += (1 << i);
}
else ++sum;
}
int x = 32;
N += 32;
while (sum < N) {
cout << x + 1 << ' ' << (side ? x + 1 : 1) << '\n';
++x, ++sum;
}
}
}
}

41
pattern.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
int N;
cin >> N;
string P[55];
for (int i = 0; i < N; ++i) cin >> P[i];
string a, b, c;
bool found = 1;
for (int i = 0; i < N && found; ++i) {
vector<string> v;
v.push_back("");
for (auto& c : P[i]) {
if (c == '*') v.push_back("");
else v.back() += c;
}
for (int j = 0; j < v.front().size() && found; ++j) {
if (j >= a.size()) a.push_back(v.front()[j]);
else if (v.front()[j] != a[j]) found = 0;
}
reverse(v.back().begin(), v.back().end());
for (int j = 0; j < v.back().size() && found; ++j) {
if (j >= c.size()) c.push_back(v.back()[j]);
else if (v.back()[j] != c[j]) found = 0;
}
for (int j = 1; j < v.size() - 1; ++j) b += v[j];
}
reverse(c.begin(), c.end());
cout << "Case #" << t + 1 << ": ";
if (found) cout << a << b << c << '\n';
else cout << "*\n";
}
}

77
square.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
int R, C;
cin >> R >> C;
cout << "Case #" << t + 1 << ": ";
vector<vector<int>> S(R);
set<pair<int, int>> check;
vector<set<int>> rs(R), cs(C);
ll ans = 0, sum = 0;
for (int i = 0; i < R; ++i) {
S[i].resize(C);
for (int j = 0; j < C; ++j) {
cin >> S[i][j];
check.emplace(i, j);
sum += S[i][j];
}
}
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) rs[i].insert(j);
}
for (int j = 0; j < C; ++j) {
for (int i = 0; i < R; ++i) cs[j].insert(i);
}
while (1) {
vector<pair<int, int>> elim;
for (auto& x : check) {
int i = x.first, j = x.second;
if (S[i][j]) {
int cnt = 0, num = 0;
auto r = cs[j].find(i);
if (r != cs[j].end()) {
if (r != cs[j].begin()) cnt += S[*(--r)][j], ++num, ++r;
if (r != --cs[j].end()) cnt += S[*(++r)][j], ++num;
}
auto c = rs[i].find(j);
if (c != rs[i].end()) {
if (c != rs[i].begin()) cnt += S[i][*(--c)], ++num, ++c;
if (c != --rs[i].end()) cnt += S[i][*(++c)], ++num;
}
if (num * S[i][j] < cnt) elim.emplace_back(i, j);
}
}
check.clear();
ans += sum;
for (auto& x : elim) {
int i = x.first, j = x.second, cnt = 0, num = 0;
sum -= S[i][j];
S[i][j] = 0;
auto r = cs[j].find(i);
if (r != cs[j].end()) {
if (r != cs[j].begin()) check.emplace(*(--r), j), ++r;
if (r != --cs[j].end()) check.emplace(*(++r), j);
}
auto c = rs[i].find(j);
if (c != rs[i].end()) {
if (c != rs[i].begin()) check.emplace(i, *(--c)), ++c;
if (c != --rs[i].end()) check.emplace(i, *(++c));
}
rs[i].erase(j);
cs[j].erase(i);
}
if (elim.empty()) break;
}
cout << ans << '\n';
}
}