Skip to content
Snippets Groups Projects
Commit f1339082 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Fix a lot of stuff

parent 4b213008
No related branches found
No related tags found
No related merge requests found
Showing
with 565 additions and 22 deletions
/// Geometry functions /// Geometry Functions
template <typename T> template <typename T>
struct Point { struct Point {
......
/// Tarjan - Articulations and Bridges /// Articulations and Bridges - Tarjan
/// ///
/// Complexity (Time): O(V + E) /// Complexity (Time): O(V + E)
/// Complexity (Space): O(V + E) /// Complexity (Space): O(V + E)
......
/// Floyd Warshall Algorithm /// Floyd Warshall
/// ///
/// Complexity (Time): O(V^3) /// Complexity (Time): O(V^3)
/// Complexity (Space): O(V^2) /// Complexity (Space): O(V^2)
......
/// Lowest Common Ancestor - LCA /// Lowest Common Ancestor (LCA)
/// ///
/// Complexity (Time): /// Complexity (Time):
/// - preprocess: O(V log V) /// - preprocess: O(V log V)
......
/// Big Integer
///
/// Complexity (space): O(n)
#include <bits/stdc++.h>
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
using ll = long long;
using ii = pair<int,int>;
const int base = 1000000000;
const int base_digits = 9;
struct BigInteger {
int sign = 1;
vector<int> num;
BigInteger() {}
BigInteger(const string &x) { read(x); }
BigInteger operator+(const BigInteger &x) const {
if (sign != x.sign)
return *this - (-x);
BigInteger ans = x;
for (int i = 0, carry = 0; i < max(size(), x.size()) || carry; ++i) {
if (i == ans.size())
ans.push_back(0);
if (i < size())
ans[i] += carry + num[i];
else
ans[i] += carry;
carry = ans[i] >= base;
if (carry)
ans[i] -= base;
}
return ans;
}
BigInteger operator-(const BigInteger& x) const {
if (sign != x.sign)
return *this + (-x);
if (abs() < x.abs())
return -(x - *this);
BigInteger ans = *this;
for (int i = 0, carry = 0; i < x.size() || carry; ++i) {
if (i < x.size())
ans[i] -= carry + x[i];
else
ans[i] -= carry;
carry = ans[i] < 0;
if (carry)
ans[i] += base;
}
ans.trim();
return ans;
}
/// Remove leading zeros.
void trim() {
while (!num.empty() && num.back() == 0)
num.pop_back();
if (num.empty())
sign = 1;
}
bool operator<(const BigInteger &x) const {
if (sign != x.sign)
return sign < x.sign;
if (size() != x.size())
return (size() * sign) < (x.size() * x.sign);
for (int i = size() - 1; i >= 0; i--)
if (num[i] != x[i])
return (num[i] * sign) < (x[i] * x.sign);
return false;
}
bool operator>(const BigInteger &x) const { return (x < *this); }
bool operator<=(const BigInteger &x) const { return !(x < *this); }
bool operator>=(const BigInteger &x) const { return !(*this < x); }
bool operator==(const BigInteger &x) const { return !(*this < x) && !(x < *this); }
bool operator!=(const BigInteger &x) const { return !(*this == x); }
/// Handles -x (change of sign).
BigInteger operator-() const {
BigInteger ans = *this;
ans.sign = -sign;
return ans;
}
/// Returs absolute value.
BigInteger abs() const {
BigInteger ans = *this;
ans.sign *= ans.sign;
return ans;
}
/// Transforms string into BigInteger.
void read(const string &s) {
sign = 1;
num.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
num.push_back(x);
}
trim();
}
friend istream& operator>>(istream &stream, BigInteger &v) {
string s; stream >> s;
v.read(s);
return stream;
}
friend ostream& operator<<(ostream &stream, const BigInteger &x) {
if (x.sign == -1)
stream << '-';
stream << (x.empty() ? 0 : x.back());
for (int i = x.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << x.num[i];
return stream;
}
/// Handles vector operations.
int back() const { return num.back(); }
bool empty() const { return num.empty(); }
size_t size() const { return num.size(); }
void push_back(int x) { num.push_back(x); }
int &operator[](int i) { return num[i]; }
int operator[](int i) const { return num[i]; }
};
int main() {
BigInteger x, y;
cin >> x >> y;
cout << x + y << ende;
return 0;
}
/// Euler Totient (phi) /// Euler Totient ($\phi$)
/// ///
/// Complexity (time): O(sqrt(n)) /// Complexity (time): O(sqrt(n))
/// Complexity (space): O(1) /// Complexity (space): O(1)
......
#include <bits/stdc++.h>
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
using ll = long long;
using ii = pair<int,int>;
vector<vector<int>> mat, filled;
vector<int> init = {0, 0, 0, 3, 3, 3, 6, 6, 6};
int check(int r, int c) {
int ans = 0;
for (int i = 0; i < 9; ++i) ans |= (1 << mat[r][i]);
for (int i = 0; i < 9; ++i) ans |= (1 << mat[i][c]);
for (int i = init[r]; i < init[r] + 3; ++i)
for (int j = init[c]; j < init[c] + 3; ++j)
ans |= (1 << mat[i][j]);
return ans;
}
bool solve(int i, int j) {
if (filled[i][j]) {
if (i == 8 && j == 8) return true;
else return solve(i + (j == 8), (j + 1) % 9);
}
int poss = check(i, j);
for (int k = 1; k <= 9; ++k) {
if ((poss & (1 << k)) == 0) {
mat[i][j] = k;
if (i == 8 && j == 8) return true;
if (solve(i + (j == 8), (j + 1) % 9)) return true;
mat[i][j] = filled[i][j];
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
mat.resize(9, vector<int>(9));
int n; cin >> n;
for (int cas = 0; cas < n; ++cas) {
for (auto &i : mat)
for (auto &j : i)
cin >> j;
filled = mat;
if (solve(0, 0))
for (auto &i : mat) {
for (auto j : i) cout << j << " ";
cout << ende;
}
else
cout << "No solution" << ende;
}
return 0;
}
#include <bits/stdc++.h>
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
using ll = long long;
using ii = pair<int,int>;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; n = 3;
vector<ii> v;
v.pb(ii(0, -1));
v.pb(ii(600, 2));
for (int i = 0; i < n; ++i) {
int x; cin >> x;
v.pb(ii(x, 0));
v.pb(ii(x+200, 1));
}
int curr = 0, ans = 0;
bool count = false;
sort(all(v));
for (int i = 0; i < v.size(); ++i) {
if (v[i].se == 0)
curr++;
else if (v[i].se == 1)
curr--;
if (count) {
ans += v[i].fi - v[i-1].fi;
count = false;
}
if (curr == 0)
count = true;
}
cout << ans * 100 << ende;
return 0;
}
#include <bits/stdc++.h>
#define MAX 1000
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
using ll = long long;
using ii = pair<int,int>;
vector<int> graph[MAX];
struct TopologicalSort {
int N;
stack<int> S;
vector<int> cont;
TopologicalSort(int N) :
N(N), cont(N)
{}
void init() {
fill(all(cont), 0);
}
bool dfs(int x) {
cont[x] = 1;
for (auto i : graph[x]) {
if (cont[i] == 1)
return true;
if (!cont[i] && dfs(i))
return true;
}
cont[x] = 2;
S.push(x);
return false;
}
bool run(vector<int> &tsort) {
init();
bool cycle = false;
for (int i = 0; i < N; ++i) {
if (!cont[i])
cycle |= dfs(i);
}
if (cycle)
return true;
while (!S.empty()) {
tsort.pb(S.top());
S.pop();
}
return false;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
for (int cas = 1; cin >> n && n; ++cas) {
for (int i = 0; i < MAX; ++i)
graph[i].clear();
vector<string> v(n);
map<string,int> M;
for (int i = 0; i < n; ++i) {
cin >> v[i];
M[v[i]] = i;
}
for (int i = 0; i < n; ++i) {
string a; cin >> a;
int m; cin >> m;
for (int j = 0; j < m; ++j) {
string b; cin >> b;
graph[M[b]].pb(M[a]);
}
}
TopologicalSort ts(n);
vector<int> ans;
bool cycle = ts.run(ans);
cout << "Teste " << cas << ende;
if (cycle)
cout << "impossivel" << ende;
else {
for (auto i : ans)
cout << v[i] << " ";
cout << ende;
}
cout << ende;
}
return 0;
}
#include <bits/stdc++.h>
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
using ll = long long;
using ii = pair<int,int>;
using dd = pair<double,double>;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << setprecision(2) << fixed;
int n;
double l = 0.0, r;
cin >> n >> r;
vector<dd> v(n);
for (auto &i : v) cin >> i.fi >> i.se;
double grtl, grtr;
for (int i = 0; i < 100; ++i) {
double lt = (r - l) / 3.0 + l;
double rt = ((r - l) * 2.0) / 3.0 + l;
grtl = 0.0; for (auto j : v) grtl = max(grtl, hypot(j.fi - lt, j.se));
grtr = 0.0; for (auto j : v) grtr = max(grtr, hypot(j.fi - rt, j.se));
if (grtl > grtr) l = lt;
else r = rt;
}
cout << l << " " << grtl << ende;
return 0;
}
#include <bits/stdc++.h>
#define EPS 1e-6
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ende '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mset(x, y) memset(&x, (y), sizeof(x))
using namespace std;
using ll = long long;
using ii = pair<int,int>;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int l, c, m, n; cin >> l >> c >> m >> n;
vector<vector<int>> mat(l, vector<int>(c));
for (auto &i : mat) for (auto &j : i) cin >> j;
vector<vector<int>> sum(l+1, vector<int>(c+1, 0));
for (int i = 1; i <= l; ++i)
for (int j = 1; j <= c; ++j)
if (j == 1) sum[i][j] = mat[i-1][j-1];
else sum[i][j] = sum[i][j-1] + mat[i-1][j-1];
for (int i = 2; i <= l; ++i)
for (int j = 1; j <= c; ++j)
sum[i][j] += sum[i-1][j];
int ans = 0;
for (int i = m; i <= l; ++i)
for (int j = n; j <= c; ++j)
ans = max(ans, sum[i][j] - sum[i-m][j] - sum[i][j-n] + sum[i-m][j-n]);
cout << ans << ende;
return 0;
}
2
0 0 0 0 6 9 8 3 0
9 8 0 0 0 0 0 7 6
6 0 0 0 3 8 0 5 1
2 0 5 0 8 1 0 9 0
0 6 0 0 0 0 0 8 0
0 9 0 3 7 0 6 0 2
3 4 0 8 5 0 0 0 9
7 2 0 0 0 0 0 6 8
0 5 6 9 2 0 0 0 0
0 0 0 0 6 9 8 3 0
9 8 0 0 0 0 0 7 6
6 0 0 0 3 8 0 5 1
2 0 5 4 8 1 0 9 0
0 6 0 0 0 0 0 8 0
0 9 0 3 7 0 6 0 2
3 4 0 8 5 0 0 0 9
7 2 0 0 0 0 0 6 8
0 5 6 9 2 0 0 0 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment