Skip to content

Instantly share code, notes, and snippets.

@speedcell4
Last active February 16, 2016 10:38
Show Gist options
  • Save speedcell4/0e0926e7b46c70481f53 to your computer and use it in GitHub Desktop.
Save speedcell4/0e0926e7b46c70481f53 to your computer and use it in GitHub Desktop.
Codeforces #622
#include <iostream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>
using namespace std;
typedef long long i64;
#define edge(i, u) for (int i = head[u]; i != -1; i = graph[i].next)
#define range(i, a, b, step) for (int i = a, _##i = b, __##i = step; i != _##i && ((i - _##i) ^ __##i) < 0 ; i += __##i)
const int maxv = 14142137;
i64 n, b[maxv];
int main(int argc, char const *argv[]) {
// #ifndef ONLINE_JUDGE
// freopen("a.txt", "r", stdin);
// #endif
i64 cnt = 0;
range(i, 1, 100000000, 1) {
b[i] = b[i - 1] + i;
if (b[i] > 1e14) {
cnt = i;
break;
}
}
while (cin >> n) {
cout << n - *(lower_bound(b, b + cnt, n) - 1) << endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>
using namespace std;
typedef long long i64;
#define edge(i, u) for (int i = head[u]; i != -1; i = graph[i].next)
#define range(i, a, b, step) for (int i = a, _##i = b, __##i = step; i != _##i && ((i - _##i) ^ __##i) < 0 ; i += __##i)
struct time {
int hh, mm;
friend istream & operator >> (istream &cin, time &tm) {
scanf("%2d:%2d", &tm.hh, &tm.mm);
return cin;
}
friend ostream & operator << (ostream &cout, time &tm) {
printf("%02d:%02d", tm.hh, tm.mm);
return cout;
}
void add(int minutes) {
mm += minutes;
hh += mm / 60;
mm %= 60;
hh %= 24;
}
} a;
int offset;
int main(int argc, char const *argv[]) {
// #ifndef ONLINE_JUDGE
// freopen("b.txt", "r", stdin);
// #endif
while (cin >> a >> offset) {
a.add(offset);
cout << a <<endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>
using namespace std;
typedef long long i64;
#define edge(i, u) for (int i = head[u]; i != -1; i = graph[i].next)
#define range(i, a, b, step) for (int i = a, _##i = b, __##i = step; i != _##i && ((i - _##i) ^ __##i) < 0 ; i += __##i)
const int maxv = 200005;
int n, m, l, r, x, a[maxv] = {0}, p[maxv] = {0};
int main(int argc, char const *argv[]) {
// #ifndef ONLINE_JUDGE
// freopen("c.txt", "r", stdin);
// #endif
while (cin >> n >> m) {
range(i, 1, n + 1, 1) {
scanf("%d", &a[i]);
if (a[i] == a[i - 1]) p[i] = p[i - 1];
else p[i] = i - 1;
}
range(i, 1, m + 1, 1) {
scanf("%d %d %d\n", &l, &r, &x);
printf("%d\n", (a[r] != x) ? r : (l <= p[r] ? p[r] : -1));
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>
using namespace std;
typedef long long i64;
#define edge(i, u) for (i64 i = head[u]; i != -1; i = graph[i].next)
#define range(i, a, b, step) for (i64 i = a, _##i = b, __##i = step; i != _##i && ((i - _##i) ^ __##i) < 0 ; i += __##i)
const int maxv = 500005 * 2;
int n, a[maxv];
int main(int argc, char const *argv[]) {
// #ifndef ONLINE_JUDGE
// freopen("d.txt", "r", stdin);
// #endif
while (cin >> n) {
memset(a, -1, sizeof a);
int odd = 1, even = n + 1;
range(i, 1, n + 1, 1) {
if (i % 2 == 1) a[odd] = a[odd + n - i] = i, odd++;
else a[even] = a[even + n - i] = i, even++;
}
range(i, 1, 2 * n + 1, 1) {
cout << (a[i] == -1 ? n : a[i]) << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <vector>
using namespace std;
#define edge(i, u) for (int i = head[u]; i != -1; i = graph[i].next)
#define range(i, a, b, step) for (int i = a, _##i = b, __##i = step; i != _##i && ((i - _##i) ^ __##i) < 0 ; i += __##i)
const int maxv = 500005;
const int maxe = maxv * 2;
struct node {
int v, next;
} graph[maxe];
int graph_cnt, head[maxv];
void graph_clear(void) {
graph_cnt = 0;
memset(head, -1, sizeof head);
}
void graph_add(int u, int v) {
graph[graph_cnt].v = v;
graph[graph_cnt].next = head[u];
head[u] = graph_cnt++;
}
int n, u, v;
bool vis[maxv];
vector<int> leaves;
void dfs(int u, int depth) {
vis[u] = true;
bool is_leave = true;
edge(i, u) if (!vis[graph[i].v]) {
is_leave = false;
dfs(graph[i].v, depth + 1);
}
if (is_leave) {
leaves.push_back(depth);
}
}
int main(int argc, char const *argv[]) {
// #ifndef ONLINE_JUDGE
// freopen("e.txt", "r", stdin);
// #endif
while (scanf("%d", &n) != EOF) {
graph_clear();
memset(vis, false, sizeof vis); vis[1] = true;
range(i, 1, n, 1) {
scanf("%d %d", &u, &v);
graph_add(u, v);
graph_add(v, u);
}
int ans = 0;
edge(i, 1) {
leaves.clear();
dfs(graph[i].v, 0);
sort(leaves.begin(), leaves.end());
range(j, 1, leaves.size(), 1) {
leaves[j] = max(leaves[j], leaves[j - 1] + 1);
}
ans = max(ans, leaves[leaves.size() - 1] + 1);
}
printf("%d\n", ans);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment