buggy_code
stringlengths 11
625k
| fixed_code
stringlengths 17
625k
| bug_type
stringlengths 2
4.45k
| language
int64 0
8
| token_count
int64 5
200k
|
|---|---|---|---|---|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int d = scanner.nextInt();
int x = 0;
int y = 0;
x = ((n + a - 1) / a) * b;
y = ((n + c - 1) / c) * d;
if (x >= y) {
System.out.println(x);
} else {
System.out.println(y);
}
}
}
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int d = scanner.nextInt();
int x = 0;
int y = 0;
x = ((n + a - 1) / a) * b;
y = ((n + c - 1) / c) * d;
if (x >= y) {
System.out.println(y);
} else {
System.out.println(x);
}
}
}
|
[["-", 64, 196, 0, 1, 0, 492, 3, 4, 0, 22], ["+", 64, 196, 0, 1, 0, 492, 3, 4, 0, 22], ["-", 75, 196, 0, 1, 0, 492, 3, 4, 0, 22], ["+", 75, 196, 0, 1, 0, 492, 3, 4, 0, 22]]
| 3
| 151
|
#include <stdio.h>
int main() {
int n, a, b, c, d;
int e, f;
int g, h;
int i, j;
scanf("%d", &n);
scanf("%d%d", &a, &b);
scanf("%d%d", &c, &d);
e = n % a;
f = n % c;
if (e <= 1) {
g = n / a + 1;
h = g * b;
} else {
g = n / a;
h = g * b;
}
if (f <= 1) {
i = n / c + 1;
j = i * d;
} else {
i = n / c;
j = i * d;
}
if (h > j) {
printf("%d\n", j);
} else if (h < j) {
printf("%d\n", h);
} else {
printf("%d\n", j);
}
return 0;
}
|
#include <stdio.h>
int main() {
int n, a, b, c, d;
int e, f;
int g, h;
int i, j;
scanf("%d", &n);
scanf("%d%d", &a, &b);
scanf("%d%d", &c, &d);
e = n % a;
f = n % c;
if (e > 0) {
g = n / a + 1;
h = g * b;
} else {
g = n / a;
h = g * b;
}
if (f > 0) {
i = n / c + 1;
j = i * d;
} else {
i = n / c;
j = i * d;
}
if (h > j) {
printf("%d\n", j);
} else if (h < j) {
printf("%d\n", h);
} else {
printf("%d\n", j);
}
return 0;
}
|
[["-", 8, 9, 0, 57, 15, 23, 0, 16, 17, 19], ["-", 8, 9, 0, 57, 15, 23, 0, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 12, 13]]
| 0
| 209
|
#include <stdio.h>
int main(void) {
int N, A, B, C, D, price_X, price_Y, mod;
scanf("%d %d %d %d %d", &N, &A, &B, &C, &D);
if (N % A != 0) {
mod = 1;
} else {
mod = 0;
}
price_X = (N / A + mod) * B;
if (N % A != 0) {
mod = 1;
} else {
mod = 0;
}
price_Y = (N / C + mod) * D;
if (price_X < price_Y) {
printf("%d\n", price_X);
} else {
printf("%d\n", price_Y);
}
return 0;
}
|
#include <stdio.h>
int main(void) {
int N, A, B, C, D, price_X, price_Y, mod;
scanf("%d %d %d %d %d", &N, &A, &B, &C, &D);
if (N % A != 0) {
mod = 1;
} else {
mod = 0;
}
price_X = (N / A + mod) * B;
if (N % C != 0) {
mod = 1;
} else {
mod = 0;
}
price_Y = (N / C + mod) * D;
if (price_X < price_Y) {
printf("%d\n", price_X);
} else {
printf("%d\n", price_Y);
}
return 0;
}
|
[["-", 0, 57, 15, 23, 0, 16, 31, 16, 12, 22], ["+", 0, 57, 15, 23, 0, 16, 31, 16, 12, 22]]
| 0
| 148
|
#include <stdio.h>
#include <string.h>
int main(void) {
int n, x, y, a, b, c, d;
scanf("%d", &n);
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
scanf("%d", &d);
x = n / a * b;
y = n / c * d;
if (n / a > 0) {
x = x + b;
}
if (n / c > 0) {
y = y + d;
}
if (x < y) {
printf("%d\n", x);
} else {
printf("%d\n", y);
}
return 0;
}
|
#include <stdio.h>
#include <string.h>
int main(void) {
int n, x, y, a, b, c, d;
scanf("%d", &n);
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
scanf("%d", &d);
x = n / a * b;
y = n / c * d;
if (n % a > 0) {
x = x + b;
}
if (n % c > 0) {
y = y + d;
}
if (x < y) {
printf("%d\n", x);
} else {
printf("%d\n", y);
}
return 0;
}
|
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 85], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 109]]
| 1
| 158
|
#include <iostream>
using namespace std;
int nedan(int a, int i, int price) {
int E;
if (a % i != 0) {
E = ((int)((a / i) + 1)) * price;
} else {
E = (int)(a / i) * price;
}
return E;
}
int main() {
int N, A, B, C, D;
cin >> N >> A >> B >> C >> D;
if (nedan(N, A, B) > nedan(N, C, D)) {
cout << nedan(N, A, B) << endl;
} else {
cout << nedan(N, C, D) << endl;
}
}
|
#include <iostream>
using namespace std;
int nedan(int a, int i, int price) {
int E;
if (a % i != 0) {
E = ((int)((a / i) + 1)) * price;
} else {
E = (int)(a / i) * price;
}
return E;
}
int main() {
int N, A, B, C, D;
cin >> N >> A >> B >> C >> D;
if (nedan(N, A, B) < nedan(N, C, D)) {
cout << nedan(N, A, B) << endl;
} else {
cout << nedan(N, C, D) << endl;
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
| 1
| 151
|
#include <iostream>
using namespace std;
int main() {
int n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
int x = (n / a) * b + (n % a > 0 ? 1 : 0) * b;
int y = (n / c) * d + (n % c > 0 ? 1 : 0) * d;
if (x > y)
cout << x << endl;
else
cout << y << endl;
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
int x = (n / a) * b + (n % a > 0 ? 1 : 0) * b;
int y = (n / c) * d + (n % c > 0 ? 1 : 0) * d;
if (x > y)
cout << y << endl;
else
cout << x << endl;
return 0;
}
|
[["-", 0, 57, 64, 1, 0, 16, 31, 16, 12, 22], ["+", 0, 57, 64, 1, 0, 16, 31, 16, 12, 22], ["-", 75, 76, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 75, 76, 0, 1, 0, 16, 31, 16, 12, 22]]
| 1
| 107
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, A, B, C, D;
cin >> N >> A >> B >> C >> D;
if ((N / A) * B < (N / C) * D && N % A == 0 && N % C == 0)
cout << (N / A) * B << endl;
if ((N / A + 1) * B < (N / C) * D && N % A > 0 && N % C == 0)
cout << (N / A + 1) * B << endl;
if ((N / A) * B > (N / C + 1) * D && N % C > 0 && N % A == 0)
cout << (N / C + 1) * D << endl;
if ((N / A + 1) * B < (N / C + 1) * D && N % A > 0 && N % C > 0)
cout << (N / A + 1) * B << endl;
if ((N / A) * B > (N / C) * D && N % A == 0 && N % C == 0)
cout << (N / C) * D << endl;
if ((N / A + 1) * B > (N / C) * D && N % A > 0 && N % C == 0)
cout << (N / C) * D << endl;
if ((N / A) * B < (N / C + 1) * D && N % C > 0 && N % A == 0)
cout << (N / A) * B << endl;
if ((N / A + 1) * B > (N / C + 1) * D && N % A > 0 && N % C > 0)
cout << (N / C + 1) * D << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, A, B, C, D;
cin >> N >> A >> B >> C >> D;
if ((N / A) * B <= (N / C) * D && N % A == 0 && N % C == 0)
cout << (N / A) * B << endl;
if ((N / A + 1) * B <= (N / C) * D && N % A > 0 && N % C == 0)
cout << (N / A + 1) * B << endl;
if ((N / A) * B >= (N / C + 1) * D && N % C > 0 && N % A == 0)
cout << (N / C + 1) * D << endl;
if ((N / A + 1) * B <= (N / C + 1) * D && N % A > 0 && N % C > 0)
cout << (N / A + 1) * B << endl;
if ((N / A) * B > (N / C) * D && N % A == 0 && N % C == 0)
cout << (N / C) * D << endl;
if ((N / A + 1) * B > (N / C) * D && N % A > 0 && N % C == 0)
cout << (N / C) * D << endl;
if ((N / A) * B < (N / C + 1) * D && N % C > 0 && N % A == 0)
cout << (N / A) * B << endl;
if ((N / A + 1) * B > (N / C + 1) * D && N % A > 0 && N % C > 0)
cout << (N / C + 1) * D << endl;
return 0;
}
|
[["-", 15, 339, 51, 16, 31, 16, 31, 16, 17, 18], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 17, 19], ["-", 15, 339, 51, 16, 31, 16, 31, 16, 17, 47], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 17, 20]]
| 1
| 398
|
#include <iostream>
using namespace std;
int main() {
int n, a, b, c, d, e, g;
cin >> n >> a >> b >> c >> d;
if (n % a == 0)
e = n / a * b;
if (n % a != 0)
e = (n / a + 1) * b;
if (n % c == 0)
g = n / c * d;
if (n % c != 0)
g = (n / c + 1) * d;
if (e > g)
cout << g << endl;
if (e < g)
cout << e << endl;
}
|
#include <iostream>
using namespace std;
int main() {
int n, a, b, c, d, e, g;
cin >> n >> a >> b >> c >> d;
if (n % a == 0)
e = n / a * b;
if (n % a != 0)
e = (n / a + 1) * b;
if (n % c == 0)
g = n / c * d;
if (n % c != 0)
g = (n / c + 1) * d;
if (e >= g)
cout << g << endl;
if (e < g)
cout << e << endl;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
| 1
| 135
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
string s;
cin >> s;
ll ans = 0; // ??????
ll pm = 0; // ?????????????????????????????????
// J???I?????????????????´???
ll sumI[N + 1], sumJ[N + 1];
sumJ[0] = sumI[N] = 0;
ll J = 0;
FOR(i, 0, N) {
if (s[i] == 'J')
J++;
sumJ[i + 1] = sumJ[i] + (s[i] == 'O' ? J : 0);
}
ll I = 0;
for (int i = N - 1; i >= 0; i--) {
if (s[i] == 'I')
I++;
sumI[i] = sumI[i + 1] + (s[i] == 'O' ? I : 0);
// cout << sumI[i] << endl;
}
ll JJ = 0, II = I;
FOR(i, 0, N) {
if (s[i] == 'O') {
// J < i
pm = max(pm, sumI[i + 1]);
// i < I
pm = max(pm, sumJ[i + 1]);
}
}
// O?????????????????´???
JJ = 0;
II = I;
pm = max(pm, JJ * II);
FOR(i, 0, N) {
if (s[i] == 'O')
ans += JJ * II;
JJ += s[i] == 'J';
II -= s[i] == 'I';
pm = max(pm, JJ * II);
}
ans += pm;
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
string s;
cin >> s;
ll ans = 0; // ??????
ll pm = 0; // ?????????????????????????????????
// J???I?????????????????´???
ll sumI[N + 1], sumJ[N + 1];
sumJ[0] = sumI[N] = 0;
ll J = 0;
FOR(i, 0, N) {
if (s[i] == 'J')
J++;
sumJ[i + 1] = sumJ[i] + (s[i] == 'O' ? J : 0);
}
ll I = 0;
for (int i = N - 1; i >= 0; i--) {
if (s[i] == 'I')
I++;
sumI[i] = sumI[i + 1] + (s[i] == 'O' ? I : 0);
// cout << sumI[i] << endl;
}
ll JJ = 0, II = I;
FOR(i, 0, N) {
if (s[i] == 'O') {
// J < i
pm = max(pm, sumI[i]);
// i < I
pm = max(pm, sumJ[i + 1]);
}
}
// O?????????????????´???
JJ = 0;
II = I;
pm = max(pm, JJ * II);
FOR(i, 0, N) {
if (s[i] == 'O')
ans += JJ * II;
JJ += s[i] == 'J';
II -= s[i] == 'I';
pm = max(pm, JJ * II);
}
ans += pm;
cout << ans << endl;
return 0;
}
|
[["-", 3, 4, 0, 69, 341, 342, 0, 16, 17, 72], ["-", 3, 4, 0, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 410
|
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
int U[200000], V[200000];
vector<pair<int, int>> graph[100000];
int v[100000], in[100000];
bool live[200000];
void BFS() {
memset(v, -1, sizeof(v));
queue<int> que;
que.push(0);
v[0] = 0;
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto to : graph[p]) {
if (v[to.first] == v[p] + 1) {
in[to.first]++;
} else if (v[to.first] < 0) {
v[to.first] = v[p] + 1;
in[to.first]++;
que.push(to.first);
}
}
}
}
int main() {
scanf("%d %d %d", &N, &M, &Q);
for (int i = 0; i < M; i++) {
scanf("%d %d", &U[i], &V[i]);
--U[i], --V[i];
graph[U[i]].emplace_back(V[i], i);
graph[V[i]].emplace_back(U[i], i);
}
BFS();
int halt = 0;
for (int i = 0; i < Q; i++) {
int R;
scanf("%d", &R);
live[--R] = true;
if (v[U[R]] != v[V[R]]) {
if (v[U[R]] > v[V[R]])
swap(U[R], V[R]);
if (--in[V[R]] == 0) {
queue<int> que;
que.push(V[R]);
while (!que.empty()) {
int p = que.front();
que.pop();
++halt;
for (auto to : graph[p]) {
if (live[to.second])
continue;
if (v[to.first] == v[p] + 1 && --in[to.first] == 0) {
que.push(to.first);
}
}
v[p] = -114;
}
}
}
printf("%d\n", halt);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
int U[200000], V[200000];
vector<pair<int, int>> graph[100000];
int v[100000], in[100000];
bool live[200000];
void BFS() {
memset(v, -1, sizeof(v));
queue<int> que;
que.push(0);
v[0] = 0;
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto to : graph[p]) {
if (v[to.first] == v[p] + 1) {
in[to.first]++;
} else if (v[to.first] < 0) {
v[to.first] = v[p] + 1;
in[to.first]++;
que.push(to.first);
}
}
}
}
int main() {
scanf("%d %d %d", &N, &M, &Q);
for (int i = 0; i < M; i++) {
scanf("%d %d", &U[i], &V[i]);
--U[i], --V[i];
graph[U[i]].emplace_back(V[i], i);
graph[V[i]].emplace_back(U[i], i);
}
BFS();
int halt = 0;
for (int i = 0; i < Q; i++) {
int R;
scanf("%d", &R);
live[--R] = true;
if (v[U[R]] != v[V[R]]) {
if (v[U[R]] > v[V[R]])
swap(U[R], V[R]);
if (--in[V[R]] == 0) {
queue<int> que;
que.push(V[R]);
while (!que.empty()) {
int p = que.front();
que.pop();
++halt;
for (auto to : graph[p]) {
if (live[to.second])
continue;
if (v[to.first] == v[p] + 1 && --in[to.first] == 0) {
que.push(to.first);
}
}
v[p] = 114514;
}
}
}
printf("%d\n", halt);
}
}
|
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
| 1
| 541
|
//-------------------------------------------
//- Train Fare -
//- -
//- autumn_eel niha katemasen! -
//-------------------------------------------
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
inline void reader(int *x) {
int k = getchar_unlocked();
*x = k - '0';
while (true) {
k = getchar_unlocked();
if (k < '0')
break;
*x = ((*x) << 1) + ((*x) << 3) + k - '0';
}
}
inline void writeln(int x) {
int s = 0, p;
char f[7];
while (x) {
p = x;
x /= 10;
f[s++] = p - (x << 1) - (x << 3);
}
if (!s)
f[s++] = 0;
while (s--)
putchar_unlocked(f[s] + '0');
putchar_unlocked('\n');
}
#pragma warning(disable : 4996)
int n, m, q, a[200003], b[200003], c[200003], d[200003], e[100003], f[100003],
g[100003];
int h1[100003], h2[100003], h3[100003], h4[100003], dist[100003], E[200003];
vector<pair<int, int>> z[100003];
vector<int> x[100003];
vector<int> D[100003];
int Q1[110000], Q1L, Q1R;
int main() {
reader(&n);
reader(&m);
reader(&q);
fill(d, d + m + 1, 1 << 30);
for (int i = 1; i <= m; i++) {
reader(&a[i]), reader(&b[i]);
f[a[i]]++;
f[b[i]]++;
}
for (int i = 1; i <= q; i++)
reader(&c[i]), d[c[i]] = i;
for (int i = 1; i <= n; i++) {
x[i].resize(f[i]);
}
for (int i = 1; i <= m; i++) {
x[a[i]][g[a[i]]] = b[i];
g[a[i]]++;
x[b[i]][g[b[i]]] = a[i];
g[b[i]]++;
}
fill(dist, dist + n + 1, -1);
dist[1] = 0;
Q1[0] = 1;
Q1R = 1;
while (Q1R > Q1L) {
int a1 = Q1[Q1L];
Q1L++;
for (int i : x[a1]) {
if (dist[i] == -1) {
dist[i] = dist[a1] + 1;
Q1[Q1R] = i;
Q1R++;
}
}
}
int rn = 0;
for (int i = 1; i <= m; i++) {
if (dist[a[i]] > dist[b[i]])
h1[a[i]]++;
if (dist[b[i]] > dist[a[i]])
h1[b[i]]++;
}
for (int i = 1; i <= n; i++)
z[i].resize(h1[i]);
for (int i = 1; i <= m; i++) {
if (dist[a[i]] > dist[b[i]]) {
z[a[i]][h2[a[i]]] = make_pair(b[i], d[i]);
h2[a[i]]++;
}
if (dist[b[i]] > dist[a[i]]) {
z[b[i]][h2[b[i]]] = make_pair(a[i], d[i]);
h2[b[i]]++;
}
}
for (int i = 1; i <= n; i++) {
h3[dist[i]]++;
rn = max(rn, dist[i]);
}
for (int i = 0; i <= rn; i++)
D[i].resize(h3[i]);
for (int i = 1; i <= rn; i++)
D[dist[i]][h4[dist[i]]] = i, h4[dist[i]]++;
e[1] = (1 << 30);
for (int i = 1; i <= rn; i++) {
for (int j : D[i]) {
for (pair<int, int> k : z[j]) {
e[j] = max(e[j], min(e[k.first], k.second));
}
}
}
for (int i = 2; i <= n; i++) {
if (e[i] < 300000)
E[e[i]]++;
}
for (int i = 1; i <= q; i++) {
E[i] += E[i - 1];
writeln(E[i]);
}
return 0;
}
|
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
inline void reader(int *x) {
int k = getchar_unlocked();
*x = k - '0';
while (true) {
k = getchar_unlocked();
if (k < '0')
break;
*x = ((*x) << 1) + ((*x) << 3) + k - '0';
}
}
inline void writeln(int x) {
int s = 0, p;
char f[7];
while (x) {
p = x;
x /= 10;
f[s++] = p - (x << 1) - (x << 3);
}
if (!s)
f[s++] = 0;
while (s--)
putchar_unlocked(f[s] + '0');
putchar_unlocked('\n');
}
#pragma warning(disable : 4996)
int n, m, q, a[200003], b[200003], c[200003], d[200003], e[100003], f[100003],
g[100003];
int h1[100003], h2[100003], h3[100003], h4[100003], dist[100003], E[200003];
vector<pair<int, int>> z[100003];
vector<int> x[100003];
vector<int> D[100003];
int Q1[110000], Q1L, Q1R;
int main() {
reader(&n);
reader(&m);
reader(&q);
fill(d, d + m + 1, 1 << 30);
for (int i = 1; i <= m; i++) {
reader(&a[i]), reader(&b[i]);
f[a[i]]++;
f[b[i]]++;
}
for (int i = 1; i <= q; i++)
reader(&c[i]), d[c[i]] = i;
for (int i = 1; i <= n; i++) {
x[i].resize(f[i]);
}
for (int i = 1; i <= m; i++) {
x[a[i]][g[a[i]]] = b[i];
g[a[i]]++;
x[b[i]][g[b[i]]] = a[i];
g[b[i]]++;
}
fill(dist, dist + n + 1, -1);
dist[1] = 0;
Q1[0] = 1;
Q1R = 1;
while (Q1R > Q1L) {
int a1 = Q1[Q1L];
Q1L++;
for (int i : x[a1]) {
if (dist[i] == -1) {
dist[i] = dist[a1] + 1;
Q1[Q1R] = i;
Q1R++;
}
}
}
int rn = 0;
for (int i = 1; i <= m; i++) {
if (dist[a[i]] > dist[b[i]])
h1[a[i]]++;
if (dist[b[i]] > dist[a[i]])
h1[b[i]]++;
}
for (int i = 1; i <= n; i++)
z[i].resize(h1[i]);
for (int i = 1; i <= m; i++) {
if (dist[a[i]] > dist[b[i]]) {
z[a[i]][h2[a[i]]] = make_pair(b[i], d[i]);
h2[a[i]]++;
}
if (dist[b[i]] > dist[a[i]]) {
z[b[i]][h2[b[i]]] = make_pair(a[i], d[i]);
h2[b[i]]++;
}
}
for (int i = 1; i <= n; i++) {
h3[dist[i]]++;
rn = max(rn, dist[i]);
}
for (int i = 0; i <= rn; i++)
D[i].resize(h3[i]);
for (int i = 1; i <= n; i++)
D[dist[i]][h4[dist[i]]] = i, h4[dist[i]]++;
e[1] = (1 << 30);
for (int i = 1; i <= rn; i++) {
for (int j : D[i]) {
for (pair<int, int> k : z[j]) {
e[j] = max(e[j], min(e[k.first], k.second));
}
}
}
for (int i = 2; i <= n; i++) {
if (e[i] < 300000)
E[e[i]]++;
}
for (int i = 1; i <= q; i++) {
E[i] += E[i - 1];
writeln(E[i]);
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
| 1
| 1,130
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
inline void reader(int &x) {
int k, m = 0;
x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
x = x * 10 + k - '0';
}
if (m)
x = -x;
}
inline void writer(int x, char c) {
int s = 0, m = 0;
char f[10];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
using namespace std;
vector<int> E[100000], G[100000];
int l[100000], d[100000], b[100000], cnt[200001], u[200000], v[200000],
r[200000], que[100000];
int main() {
int n, m, q;
reader(n);
reader(m);
reader(q);
rep(i, m) {
reader(u[i]);
reader(v[i]);
u[i]--;
v[i]--;
r[i] = q;
l[u[i]]++;
l[v[i]]++;
}
rep(i, n) E[i].reserve(l[i]), G[i].reserve(l[i]), l[i] = 0;
rep(i, q) {
int a;
reader(a);
r[a - 1] = i;
}
rep(i, m) {
E[u[i]][l[u[i]]] = v[i];
G[u[i]][l[u[i]]++] = r[i];
E[v[i]][l[v[i]]] = u[i];
G[v[i]][l[u[i]]++] = r[i];
}
d[0] = 1;
b[0] = q;
int s = 0, g = 1;
while (s != g) {
int p = que[s++];
rep(i, l[p]) {
int a = E[p][i];
if (d[a] == 0) {
d[a] = d[p] + 1;
que[g++] = a;
}
if (d[a] == d[p] + 1)
b[a] = max(b[a], min(b[p], G[p][i]));
}
}
rep(i, n) cnt[b[i]]++;
int ans = 0;
rep(i, q) {
ans += cnt[i];
writer(ans, '\n');
}
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
inline void reader(int &x) {
int k, m = 0;
x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
x = x * 10 + k - '0';
}
if (m)
x = -x;
}
inline void writer(int x, char c) {
int s = 0, m = 0;
char f[10];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
using namespace std;
vector<int> E[100000], G[100000];
int l[100000], d[100000], b[100000], cnt[200001], u[200000], v[200000],
r[200000], que[100000];
int main() {
int n, m, q;
reader(n);
reader(m);
reader(q);
rep(i, m) {
reader(u[i]);
reader(v[i]);
u[i]--;
v[i]--;
r[i] = q;
l[u[i]]++;
l[v[i]]++;
}
rep(i, n) E[i].reserve(l[i]), G[i].reserve(l[i]), l[i] = 0;
rep(i, q) {
int a;
reader(a);
r[a - 1] = i;
}
rep(i, m) {
E[u[i]][l[u[i]]] = v[i];
G[u[i]][l[u[i]]++] = r[i];
E[v[i]][l[v[i]]] = u[i];
G[v[i]][l[v[i]]++] = r[i];
}
d[0] = 1;
b[0] = q;
int s = 0, g = 1;
while (s != g) {
int p = que[s++];
rep(i, l[p]) {
int a = E[p][i];
if (d[a] == 0) {
d[a] = d[p] + 1;
que[g++] = a;
}
if (d[a] == d[p] + 1)
b[a] = max(b[a], min(b[p], G[p][i]));
}
}
rep(i, n) cnt[b[i]]++;
int ans = 0;
rep(i, q) {
ans += cnt[i];
writer(ans, '\n');
}
}
|
[["-", 0, 27, 28, 69, 341, 342, 0, 69, 28, 22], ["+", 0, 27, 28, 69, 341, 342, 0, 69, 28, 22]]
| 1
| 743
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
struct BinaryIndexedTree {
vector<int64> data;
int curr;
BinaryIndexedTree(int sz) {
data.assign(++sz, 0);
curr = 1;
while (curr < data.size())
curr <<= 1;
}
inline int64 sum(int k) {
int64 ret = 0;
for (++k; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
inline int upper_bound(int64 w) {
int i = 0;
for (int k = curr; k > 0; k >>= 1) {
if (i + k < data.size() && data[i + k] <= w) {
w -= data[i + k];
i += k;
}
}
return (i);
}
inline void add(int k, int64 x) {
for (++k; k < data.size(); k += k & -k)
data[k] += x;
}
};
int N, Q;
int X[200000], D[200000], L[200000];
char buff[32];
int main() {
fgets(buff, sizeof(buff), stdin);
scanf(buff, "%d %d", &N, &Q);
for (int i = 0; i < Q; i++) {
fgets(buff, sizeof(buff), stdin);
sscanf(buff, "%d %d %d", &X[i], &D[i], &L[i]);
}
BinaryIndexedTree xpy(N), xmy(N);
for (int i = 0; i < N; i++) {
xpy.add(i, 1);
xmy.add(i, 1);
}
for (int i = Q - 1; i >= 0; i--) {
if (D[i] == 1) {
xmy.add(0, -L[i] * 2);
xmy.add(xpy.upper_bound(X[i]), L[i] * 2);
} else {
xpy.add(xmy.upper_bound(X[i]), L[i] * 2);
}
}
for (int i = 0; i < N; i++) {
printf("%lld\n", (xpy.sum(i) - xmy.sum(i)) / 2);
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
struct BinaryIndexedTree {
vector<int64> data;
int curr;
BinaryIndexedTree(int sz) {
data.assign(++sz, 0);
curr = 1;
while (curr < data.size())
curr <<= 1;
}
inline int64 sum(int k) {
int64 ret = 0;
for (++k; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
inline int upper_bound(int64 w) {
int i = 0;
for (int k = curr; k > 0; k >>= 1) {
if (i + k < data.size() && data[i + k] <= w) {
w -= data[i + k];
i += k;
}
}
return (i);
}
inline void add(int k, int64 x) {
for (++k; k < data.size(); k += k & -k)
data[k] += x;
}
};
int N, Q;
int X[200000], D[200000], L[200000];
char buff[32];
int main() {
fgets(buff, sizeof(buff), stdin);
sscanf(buff, "%d %d", &N, &Q);
for (int i = 0; i < Q; i++) {
fgets(buff, sizeof(buff), stdin);
sscanf(buff, "%d %d %d", &X[i], &D[i], &L[i]);
}
BinaryIndexedTree xpy(N), xmy(N);
for (int i = 0; i < N; i++) {
xpy.add(i, 1);
xmy.add(i, 1);
}
for (int i = Q - 1; i >= 0; i--) {
if (D[i] == 1) {
xmy.add(0, -L[i] * 2);
xmy.add(xpy.upper_bound(X[i]), L[i] * 2);
} else {
xpy.add(xmy.upper_bound(X[i]), L[i] * 2);
}
}
for (int i = 0; i < N; i++) {
printf("%lld\n", (xpy.sum(i) - xmy.sum(i)) / 2);
}
}
|
[["-", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22]]
| 1
| 506
|
import java.util.*;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws Exception {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int ret = 0;
if (a < 0) {
ret += a * (-1) * c;
ret += d;
ret += b * e;
} else {
ret += (b - a) * d;
}
System.out.println(ret);
}
}
|
import java.util.*;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws Exception {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int ret = 0;
if (a < 0) {
ret += a * (-1) * c;
ret += d;
ret += b * e;
} else {
ret += (b - a) * e;
}
System.out.println(ret);
}
}
|
[["-", 75, 196, 0, 1, 0, 11, 12, 16, 12, 22], ["+", 75, 196, 0, 1, 0, 11, 12, 16, 12, 22]]
| 3
| 140
|
#include <stdio.h>
int main(void) {
int a, b, c, d, e, m, x, y, z;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (a < 0) {
m = a * -1;
x = c * m;
y = e * b;
z = x + y + d;
printf("%d\n", z);
} else if (0 < a) {
x = b - a;
y = x * e;
printf("%d\n", z);
} else {
x = b * e;
y = x + d;
printf("%d\n", z);
}
return 0;
}
|
#include <stdio.h>
int main(void) {
int a, b, c, d, e, m, x, y, z;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (a < 0) {
m = a * -1;
x = c * m;
y = e * b;
z = x + y + d;
printf("%d\n", z);
} else if (0 < a) {
x = b - a;
y = x * e;
printf("%d\n", y);
} else {
x = b * e;
y = x + d;
printf("%d\n", y);
}
return 0;
}
|
[["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
| 1
| 153
|
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
if (a > 0) {
cout << (b - a) * e << endl;
} else {
cout << b * e + d + a * c << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
if (a > 0) {
cout << (b - a) * e << endl;
} else {
cout << b * e + d - a * c << endl;
}
return 0;
}
|
[["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33]]
| 1
| 75
|
#include <stdio.h>
int main() {
int a, b, c, d, e;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
if (a < 0 && b <= 0) {
printf("%d\n", c * (b - a));
} else if (a < 0 && b > 0) {
printf("%d\n", (-a) * c + d + b * e);
} else {
printf("%d\n", b * e);
}
return 0;
}
|
#include <stdio.h>
int main() {
int a, b, c, d, e;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
if (a < 0 && b <= 0) {
printf("%d\n", c * (b - a));
} else if (a < 0 && b > 0) {
printf("%d\n", (-a) * c + d + b * e);
} else {
printf("%d\n", (b - a) * e);
}
return 0;
}
|
[["+", 0, 2, 3, 4, 0, 16, 31, 23, 0, 24], ["+", 3, 4, 0, 16, 31, 23, 0, 16, 17, 33], ["+", 3, 4, 0, 16, 31, 23, 0, 16, 12, 22], ["+", 0, 2, 3, 4, 0, 16, 31, 23, 0, 25]]
| 1
| 121
|
//============================================================================
// Name : AOL.cpp
// Author : Akari Fujii
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e;
if (a < 0)
f = d + e * b + (-a) * c;
if (a = 0)
f = d + e * b;
if (a > 0)
f = (b - a) * e;
cout << f << endl;
}
|
//============================================================================
// Name : AOL.cpp
// Author : Akari Fujii
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e;
if (a < 0)
f = d + e * b + (-a) * c;
if (a == 0)
f = d + e * b;
if (a > 0)
f = (b - a) * e;
cout << f << endl;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
| 1
| 101
|
#include <iostream>
using namespace std;
int main(void) {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
int f = 0;
if (a < 0) {
for (;;) {
a = a + 1;
f = f + c;
if (a == b) {
cout << f << endl;
return 0;
}
if (a == 0) {
f = f + d;
break;
}
}
} else if (a >= 0) {
for (;;) {
if (a == b) {
cout << f << endl;
return 0;
}
a = a + 1;
f = f + e;
}
}
return 0;
}
|
#include <iostream>
using namespace std;
int main(void) {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
int f = 0;
if (a < 0) {
for (;;) {
a = a + 1;
f = f + c;
if (a == b) {
cout << f << endl;
return 0;
}
if (a == 0) {
f = f + d;
break;
}
}
}
if (a >= 0) {
for (;;) {
if (a == b) {
cout << f << endl;
return 0;
}
a = a + 1;
f = f + e;
}
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95]]
| 1
| 149
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, e;
int s = 0;
cin >> a >> b >> c >> d >> e;
if (a < 0) {
s += (a * c * -1) + d;
}
s += (b - a) * e;
cout << s << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, e;
int s = 0;
cin >> a >> b >> c >> d >> e;
if (a < 0) {
s += a * c * (-1) + d;
a = 0;
}
s += (b - a) * e;
cout << s << endl;
return 0;
}
|
[["-", 0, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["+", 0, 11, 12, 16, 31, 16, 12, 23, 0, 24], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 13]]
| 1
| 79
|
#include <stdio.h>
int A, B, C, D, E, T;
int main() {
scanf("%d %d %d %d %d", &A, &B, &C, &D, &E);
if (A > 0) {
T = (B - A) * E;
} else if (A < 0) {
T = (0 - A) * C + D + (B - A) * E;
}
printf("%d\n", T);
return 0;
}
|
#include <stdio.h>
int A, B, C, D, E, T;
int main() {
scanf("%d %d %d %d %d", &A, &B, &C, &D, &E);
if (A > 0) {
T = (B - A) * E;
} else if (A < 0) {
T = (0 - A) * C + D + B * E;
}
printf("%d\n", T);
return 0;
}
|
[["-", 0, 11, 12, 16, 12, 16, 31, 23, 0, 24], ["-", 12, 16, 12, 16, 31, 23, 0, 16, 17, 33], ["-", 12, 16, 12, 16, 31, 23, 0, 16, 12, 22], ["-", 0, 11, 12, 16, 12, 16, 31, 23, 0, 25]]
| 1
| 103
|
#include <stdio.h>
int main(void) {
int a, b, c, d, e, x;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (a < 0) {
x = (a * -1) + c + (b * e);
}
if (a > 0) {
x = (b - a) * e;
}
printf("%d\n", x);
return 0;
}
|
#include <stdio.h>
int main(void) {
int a, b, c, d, e, x;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (a < 0) {
x = (a * -1 * c) + d + (b * e);
}
if (a > 0) {
x = (b - a) * e;
}
printf("%d\n", x);
return 0;
}
|
[["+", 12, 16, 31, 16, 31, 23, 0, 16, 17, 48], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 12, 22], ["-", 0, 1, 0, 11, 12, 16, 31, 16, 12, 22], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 12, 22]]
| 1
| 99
|
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
int main(void) {
long n, a, b, x, y, z, p, q, r;
cin >> n >> a >> x >> y >> z;
if (n < 0) {
p = 0;
p = x * (0 - n);
q = 0;
q = y;
r = 0;
r = z * a;
cout << p + q + r << endl;
} else if (n == 0) {
p = 0;
q = 0;
q = y;
r = 0;
r = z * a;
cout << p + q + r << endl;
} else if (n > 0) {
p = 0;
q = 0;
r = z * a;
cout << p + q + r << endl;
}
}
|
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
int main(void) {
long n, a, b, x, y, z, p, q, r;
cin >> n >> a >> x >> y >> z;
if (n < 0) {
p = 0;
p = x * (0 - n);
q = 0;
q = y;
r = 0;
r = z * a;
cout << p + q + r << endl;
} else if (n == 0) {
p = 0;
q = 0;
q = y;
r = 0;
r = z * a;
cout << p + q + r << endl;
} else if (n > 0) {
p = 0;
q = 0;
r = z * (a - n);
cout << p + q + r << endl;
}
}
|
[["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 17, 33], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 12, 22], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
| 1
| 174
|
#include <stdio.h>
int main(void) {
int a, b, c, d, e;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
if (a < 0)
printf("%d\n", -a * c + d + b * e);
else
printf("%d\n", d + (b - a) * e);
return 0;
}
|
#include <stdio.h>
int main(void) {
int a, b, c, d, e;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
if (a < 0)
printf("%d\n", -a * c + d + b * e);
else
printf("%d\n", (b - a) * e);
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72]]
| 1
| 89
|
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e;
int ans;
cin >> a >> b >> c >> d >> e;
if (a <= 0) {
ans = (-a) * c + d + b * e;
} else {
ans = (b - a) + e;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e;
int ans;
cin >> a >> b >> c >> d >> e;
if (a <= 0) {
ans = (-a) * c + d + b * e;
} else {
ans = (b - a) * e;
}
cout << ans << endl;
return 0;
}
|
[["-", 0, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 0, 9, 0, 1, 0, 11, 12, 16, 17, 48]]
| 1
| 83
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, e, sum;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
if (a < 0) {
sum = -a * c + d + b * e;
} else if (a = 0) {
sum = d + b * e;
} else if (a > 0) {
sum = (b - a) * e;
}
cout << sum << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, e, sum;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
if (a < 0) {
sum = -a * c + d + b * e;
}
if (a == 0) {
sum = d + b * e;
}
if (a > 0) {
sum = (b - a) * e;
}
cout << sum << endl;
}
|
[["-", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95], ["-", 75, 76, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["-", 0, 57, 75, 76, 0, 57, 75, 76, 0, 95]]
| 1
| 108
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D, E;
cin >> A >> B >> C >> D >> E;
if (A < 0)
cout << -A + D + E * B << endl;
if (A > 0)
cout << E * (A - B) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D, E;
cin >> A >> B >> C >> D >> E;
if (A < 0)
cout << -A * C + D + E * B << endl;
if (A > 0)
cout << E * (B - A) << endl;
return 0;
}
|
[["+", 31, 16, 12, 16, 31, 16, 31, 16, 17, 48], ["+", 31, 16, 12, 16, 31, 16, 31, 16, 12, 22], ["-", 31, 16, 12, 16, 12, 23, 0, 16, 31, 22], ["-", 31, 16, 12, 16, 12, 23, 0, 16, 17, 33], ["+", 31, 16, 12, 16, 12, 23, 0, 16, 17, 33], ["+", 31, 16, 12, 16, 12, 23, 0, 16, 12, 22]]
| 1
| 75
|
#include <iostream>
using namespace std;
int main() {
int A, B, C, D, E;
cin >> A >> B >> C >> D >> E;
if (B < 0) {
cout << (B - A) * C << endl;
} else if (B < 0) {
cout << (-A) * C + D + B * E << endl;
} else {
cout << (B - A) * E << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int A, B, C, D, E;
cin >> A >> B >> C >> D >> E;
if (B < 0) {
cout << (B - A) * C << endl;
} else if (A < 0) {
cout << (0 - A) * C + D + B * E << endl;
} else {
cout << (B - A) * E << endl;
}
return 0;
}
|
[["-", 75, 76, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 31, 16, 31, 16, 31, 23, 0, 16, 31, 13]]
| 1
| 99
|
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
const static int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const static int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
int H, W;
char S[1000][1001];
cin >> H >> W;
for (int i = 0; i < H; i++) {
cin >> S[i];
}
queue<Pii> Que;
int ret = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (S[i][j] == '.')
Que.push(Pii(0, Pi(j, i)));
else
S[i][j] -= '0';
}
}
while (!Que.empty()) {
Pii &p = Que.front();
ret = max(ret, p.first);
for (int i = 0; i < 8; i++) {
int ny = p.second.second + dy[i];
int nx = p.second.first + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && S[nx][ny] != '.') {
--S[ny][nx];
if (S[ny][nx] == 0) {
Que.push(Pii(p.first + 1, Pi(nx, ny)));
}
}
Que.pop();
}
}
cout << ret << endl;
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
const static int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const static int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
int H, W;
char S[1000][1000];
cin >> H >> W;
for (int i = 0; i < H; i++) {
cin >> S[i];
}
queue<Pii> Que;
int ret = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (S[i][j] == '.')
Que.push(Pii(0, Pi(j, i)));
else
S[i][j] -= '0';
}
}
while (!Que.empty()) {
Pii &p = Que.front();
ret = max(ret, p.first);
for (int i = 0; i < 8; i++) {
int ny = p.second.second + dy[i];
int nx = p.second.first + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && S[ny][nx] != '.') {
--S[ny][nx];
if (S[ny][nx] == 0) {
Que.push(Pii(p.first + 1, Pi(nx, ny)));
}
}
}
Que.pop();
}
cout << ret << endl;
return (0);
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 12, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 51, 16, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46]]
| 1
| 379
|
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int h, w, map[1001][1001];
struct pos {
int x, y;
};
std::queue<pair<pos, int>> que;
int check(int _X, int _Y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (map[_X + i][_Y + j] == -1)
count++;
}
}
return count;
}
bool Cl(int _X, int _Y) { return check(_X, _Y) >= map[_X][_Y]; };
void Break(int _x, int _y, int _wave) {
pos p;
p.x = _x, p.y = _y;
que.push(make_pair(p, _wave));
}
int main() {
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
char c;
cin >> c;
if (c == '.')
map[i][j] = -1;
else
map[i][j] = c - '0';
}
}
for (int i = 2; i <= h - 1; i++) {
for (int j = 2; j <= w - 1; j++) {
if (Cl(i, j) && map[i][j] != -1)
map[i][j] = 0, Break(i, j, 1);
else if (map[i][j] != -1) {
map[i][j] -= check(i, j);
}
}
}
int big = 0;
while (que.size()) {
pair<pos, int> P = que.front();
que.pop();
pos po;
po = P.first;
if (map[po.x][po.y] == -1)
continue;
big = std::max(big, P.second);
map[po.x][po.y] = -1;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
pos tep;
tep.x = po.x + i, tep.y = po.y + j;
if (map[tep.x][tep.y] > 0)
map[tep.x][tep.y]--;
if (map[tep.x][tep.y] == 0)
que.push(make_pair(tep, ++P.second));
}
}
}
cout << big << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int h, w, map[1001][1001];
struct pos {
int x, y;
};
std::queue<pair<pos, int>> que;
int check(int _X, int _Y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (map[_X + i][_Y + j] == -1)
count++;
}
}
return count;
}
bool Cl(int _X, int _Y) { return check(_X, _Y) >= map[_X][_Y]; };
void Break(int _x, int _y, int _wave) {
pos p;
p.x = _x, p.y = _y;
que.push(make_pair(p, _wave));
}
int main() {
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
char c;
cin >> c;
if (c == '.')
map[i][j] = -1;
else
map[i][j] = c - '0';
}
}
for (int i = 2; i <= h - 1; i++) {
for (int j = 2; j <= w - 1; j++) {
if (Cl(i, j) && map[i][j] != -1)
map[i][j] = 0, Break(i, j, 1);
else if (map[i][j] != -1) {
map[i][j] -= check(i, j);
}
}
}
int big = 0;
while (que.size()) {
pair<pos, int> P = que.front();
que.pop();
pos po;
po = P.first;
if (map[po.x][po.y] == -1)
continue;
big = std::max(big, P.second);
map[po.x][po.y] = -1;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
pos tep;
tep.x = po.x + i, tep.y = po.y + j;
if (map[tep.x][tep.y] > 0)
map[tep.x][tep.y]--;
if (map[tep.x][tep.y] == 0)
que.push(make_pair(tep, P.second + 1));
}
}
}
cout << big << endl;
return 0;
}
|
[["-", 3, 4, 0, 2, 3, 4, 0, 27, 17, 29], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 586
|
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
string S[2000];
int x[2000][2000];
int z[2000][2000];
queue<int> Q;
queue<int> R;
int main() {
int n, m;
char T[10] = {'.', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> S[i];
for (int j = 0; j < m; j++) {
for (int k = 0; k < 10; k++) {
if (S[i][j] == T[k]) {
x[i][j] = k;
}
}
}
}
int t = 1;
int p = 0;
int sum = 0;
memset(z, 0, sizeof(z));
t = 0;
sum++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (x[i][j] >= 1) {
p = 0;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (x[i + k][j + l] == 0) {
p++;
}
}
}
if (p >= x[i][j]) {
z[i][j] = 1;
Q.push(i * 1000 + j);
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (z[i][j] == 1) {
x[i][j] = 0;
}
}
}
int c, d;
while (sum == 0 || !Q.empty()) {
sum++;
while (!Q.empty()) {
c = Q.front() / 1000;
d = Q.front() % 1000;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (x[c + i][d + j] >= 1) {
p = 0;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (x[c + i + k][d + j + l]) {
p++;
}
}
}
if (p >= x[c + i][d + j] && x[c + i][d + j] != 10) {
x[c + i][d + j] = 10;
R.push((c + i) * 1000 + (d + j));
}
}
}
}
Q.pop();
}
while (!R.empty()) {
c = R.front() / 1000;
d = R.front() % 1000;
x[c][d] = 0;
Q.push(R.front());
R.pop();
}
}
cout << sum << endl;
return 0;
}
|
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
string S[2000];
int x[2000][2000];
int z[2000][2000];
queue<int> Q;
queue<int> R;
int main() {
int n, m;
char T[10] = {'.', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> S[i];
for (int j = 0; j < m; j++) {
for (int k = 0; k < 10; k++) {
if (S[i][j] == T[k]) {
x[i][j] = k;
}
}
}
}
int t = 1;
int p = 0;
int sum = 0;
memset(z, 0, sizeof(z));
t = 0;
sum++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (x[i][j] >= 1) {
p = 0;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (x[i + k][j + l] == 0) {
p++;
}
}
}
if (p >= x[i][j]) {
z[i][j] = 1;
Q.push(i * 1000 + j);
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (z[i][j] == 1) {
x[i][j] = 0;
}
}
}
int c, d;
while (sum == 0 || !Q.empty()) {
sum++;
while (!Q.empty()) {
c = Q.front() / 1000;
d = Q.front() % 1000;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (x[c + i][d + j] >= 1) {
p = 0;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (x[c + i + k][d + j + l] == 0) {
p++;
}
}
}
if (p >= x[c + i][d + j] && x[c + i][d + j] != 10) {
x[c + i][d + j] = 10;
R.push((c + i) * 1000 + (d + j));
}
}
}
}
Q.pop();
}
while (!R.empty()) {
c = R.front() / 1000;
d = R.front() % 1000;
x[c][d] = 0;
Q.push(R.front());
R.pop();
}
}
cout << sum - 1 << endl;
return 0;
}
|
[["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
| 1
| 724
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
#define INF 1000000000
using namespace std;
const vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
const vector<int> dy = {-1, -1, 0, 1, 1, 1, 0, -1};
int H, W;
vector<string> M;
vector<vector<int>> state;
int check(int x, int y, int s) {
int ret = 0;
for (int dir = 0; dir < 8; dir++) {
if (state[y + dy[dir]][x + dx[dir]] < s ||
M[y + dy[dir]][x + dx[dir]] == '.') {
ret++;
}
}
return ret;
}
int main() {
scanf("%d", &H);
scanf("%d", &W);
M = vector<string>(H);
for (int i = 0; i < H; i++)
cin >> M[i];
state = vector<vector<int>>(H, vector<int>(W, INF));
queue<pair<int, int>> que;
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (check(j, i, 1) >= M[i][j] - 48 && M[i][j] != '.') {
que.push(make_pair(j, i));
state[i][j] = 1;
}
}
}
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int dir = 0; dir < 8; dir++) {
int x2 = x + dx[dir];
int y2 = y + dy[dir];
if (state[y2][x2] == INF && M[y2][x2] != '.' &&
check(x2, y2, state[y][x] + 1) >= M[y2][x2]) {
state[y2][x2] = state[y][x] + 1;
que.push(make_pair(x2, y2));
}
}
}
int ret = 0;
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (state[i][j] != INF) {
ret = max(ret, state[i][j]);
}
}
}
printf("%d\n", ret);
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
#define INF 1000000000
using namespace std;
const vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
const vector<int> dy = {-1, -1, 0, 1, 1, 1, 0, -1};
int H, W;
vector<string> M;
vector<vector<int>> state;
int check(int x, int y, int s) {
int ret = 0;
for (int dir = 0; dir < 8; dir++) {
if (state[y + dy[dir]][x + dx[dir]] < s ||
M[y + dy[dir]][x + dx[dir]] == '.') {
ret++;
}
}
return ret;
}
int main() {
scanf("%d", &H);
scanf("%d", &W);
M = vector<string>(H);
for (int i = 0; i < H; i++)
cin >> M[i];
state = vector<vector<int>>(H, vector<int>(W, INF));
queue<pair<int, int>> que;
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (check(j, i, 1) >= M[i][j] - 48 && M[i][j] != '.') {
que.push(make_pair(j, i));
state[i][j] = 1;
}
}
}
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int dir = 0; dir < 8; dir++) {
int x2 = x + dx[dir];
int y2 = y + dy[dir];
if (state[y2][x2] == INF && M[y2][x2] != '.' &&
check(x2, y2, state[y][x] + 1) >= M[y2][x2] - 48) {
state[y2][x2] = state[y][x] + 1;
que.push(make_pair(x2, y2));
}
}
}
int ret = 0;
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (state[i][j] != INF) {
ret = max(ret, state[i][j]);
}
}
}
printf("%d\n", ret);
return 0;
}
|
[["+", 15, 339, 51, 16, 12, 16, 12, 16, 17, 33], ["+", 15, 339, 51, 16, 12, 16, 12, 16, 12, 13]]
| 1
| 601
|
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#define FLAG -1
using namespace std;
typedef pair<int, int> P;
int main() {
int h, w, dh[] = {1, 1, 1, 0, -1, -1, -1, 0},
dw[] = {1, 0, -1, -1, -1, 0, 1, 1}, ctn, i, j;
int solve = -2;
cin >> h >> w;
queue<P> que, tmp;
char d[h][w];
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
cin >> d[i][j];
if ('1' <= d[i][j] && '9' >= d[i][j])
que.push(make_pair(i, j));
}
}
que.push(make_pair(FLAG, FLAG));
for (;;) {
solve++;
map<P, bool> m;
for (;;) {
// cout << que.size()<<endl;
P p = que.front();
que.pop();
if (p.first == FLAG) { // cout <<" break "<<endl<<endl;
break;
}
if (d[p.first][p.second] == '.')
continue;
ctn = 0;
for (i = 0; i < 8; i++) {
if (d[p.first + dh[i]][p.second + dw[i]] == '.')
ctn++;
}
if (ctn >= d[p.first][p.second] - '0') {
tmp.push(p);
for (j = 0; j < 8; j++)
if (m.count(make_pair(p.first + dh[j], p.second + dw[j])) == 0) {
que.push(make_pair(p.first + dh[j], p.second + dw[j]));
m[make_pair(p.first + dh[j], p.second + dw[j])] = true;
}
}
}
if (tmp.empty())
break;
while (!tmp.empty()) {
d[tmp.front().first][tmp.front().second] = '.';
tmp.pop();
}
/*for(i =0 ;i<h;i++)
{
for(j=0;j<w;j++)cout << d[i][j];
cout <<endl;
}
*/
que.push(make_pair(FLAG, FLAG));
}
cout << solve << endl;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#define FLAG -1
using namespace std;
typedef pair<int, int> P;
int main() {
int h, w, dh[] = {1, 1, 1, 0, -1, -1, -1, 0},
dw[] = {1, 0, -1, -1, -1, 0, 1, 1}, ctn, i, j;
int solve = -1;
cin >> h >> w;
queue<P> que, tmp;
char d[h][w];
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
cin >> d[i][j];
if ('1' <= d[i][j] && '9' >= d[i][j])
que.push(make_pair(i, j));
}
}
que.push(make_pair(FLAG, FLAG));
for (;;) {
solve++;
map<P, bool> m;
for (;;) {
// cout << que.size()<<endl;
P p = que.front();
que.pop();
if (p.first == FLAG) { // cout <<" break "<<endl<<endl;
break;
}
if (d[p.first][p.second] == '.')
continue;
ctn = 0;
for (i = 0; i < 8; i++) {
if (d[p.first + dh[i]][p.second + dw[i]] == '.')
ctn++;
}
if (ctn >= d[p.first][p.second] - '0') {
tmp.push(p);
for (j = 0; j < 8; j++)
if (m.count(make_pair(p.first + dh[j], p.second + dw[j])) == 0) {
que.push(make_pair(p.first + dh[j], p.second + dw[j]));
m[make_pair(p.first + dh[j], p.second + dw[j])] = true;
}
}
}
if (tmp.empty())
break;
while (!tmp.empty()) {
d[tmp.front().first][tmp.front().second] = '.';
tmp.pop();
}
/*for(i =0 ;i<h;i++)
{
for(j=0;j<w;j++)cout << d[i][j];
cout <<endl;
}
*/
que.push(make_pair(FLAG, FLAG));
}
cout << solve << endl;
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 523
|
/*************************
PCK 2012 t1
************************/
/*#include <stdio.h>
int main(){
int s;
int sum = 0;
int i;
for ( i = 0; i < 10; i++){
scanf("%d", &s);
sum += s;
}
printf("%d\n", sum);
}*/
/************************
PCK 2012 t2
************************/
/*
#include <stdio.h>
int main(){
int b[3];
int i;
for ( i = 0; i < 3; i++){
scanf("%d", &b[i]);
}
if (b[2] == 1)
printf("Open\n");
else if (b[0] == 1){
if (b[1] == 1)
printf("Open\n");
else
printf("Close\n");
}
else
printf("Close\n");
}*/
/****************************
PCK 2012 t3
****************************/
/*
#include <stdio.h>
int main(){
int n;
int h[101] = { 0 };
int i;
int temp;
scanf("%d", &n);
while (n != 0){
for (i = 0; i < n + 1; i++){
scanf("%d", &h[i]);
}
if ((h[1] - h[0]) + h[1] == h[2]){
temp = h[1] - h[0];
for (i = 1; i < n; i++){
if (h[i] + temp != h[i + 1]){
printf("%d\n", h[i + 1]);
break;
}
}
}
else{
if ((h[2] - h[1]) + h[2] == h[3])
printf("%d\n", h[0]);
else if ((h[1] - h[0]) + h[2] != h[3])
printf("%d\n", h[2]);
else
printf("%d\n", h[1]);
}
scanf("%d", &n);
}
}
*/
/*************************************
PCK 2012 t4
(2??´????±?????????????)
*************************************/
/*
#include <stdio.h>
int main(void)
{
int n;
int num[4], l, s, count, i, j, temp;
scanf("%d", &n);
while (n != 0000){
count = 0;
while (n != 6174){
num[0] = n / 1000;
num[1] = (n - num[0] * 1000) / 100;
num[2] = (n - num[0] * 1000 - num[1] * 100) / 10;
num[3] = n - num[0] * 1000 - num[1] * 100 - num[2] * 10;
if (num[0] == num[1] && num[1] == num[2] && num[2] ==
num[3]) //??¨???????????°???(1111??????7777??????)?????? break; for (i = 0; i<3;
i++){ for (j = i + 1; j<4; j++){ if (num[i]>num[j]){ temp = num[i]; num[i] =
num[j]; num[j] = temp;
}
}
}
s = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
l = num[3] * 1000 + num[2] * 100 + num[1] * 10 + num[0];
n = l - s;
count++;
}
if (n == 6174 || count>0)
printf("%d\n", count);
else
printf("NA\n");
scanf("%d", &n);
}
return 0;
}*/
/*************************************
PCK 2012 t5
*************************************/
/*
#include <algorithm>
#include <stdio.h>
int *p, *j;
int bfs(int n,int ){
return max(n * )
}
int main(){
int n;
scanf("%d", &n);
while (n != 0){
p = new int[n];
j = new int[n - 1];
for (int i = 0; i < n; i++)
scanf("%d", &p[i]);
for (int i = 0; i < n - 1; i++)
scanf("%d", &j[i]);
scanf("%d", &n);
}
}*/
// PCK 2013 t4 ????????????????????????
/*/
#include <iostream>
using namespace std;
#include <algorithm>
#include <cstring>
int main(){
int n;
int hand[100];
while (cin >> n && n != 0){
memset(hand, 0, sizeof(hand));
int field = 0;
int num = 0;
for (int i = 0; i < 100; i++){
char c;
cin >> c;
if (c == 'M') hand[num]++;
else if (c == 'S'){ field += hand[num] + 1; hand[num] =
0; } else { hand[num] += field + 1; field = 0; } if (num + 1 < n) num++; else
num = 0;
}
sort(hand, hand + n);
for (int i = 0; i < n; i++)
cout << hand[i] << ' ';
cout << field << endl;
}
}
*/
/*********************************************************************
pck 2009 t1 ?????\?????§???????°??????????????
*********************************************************************/
/*
#include <iostream>
using namespace std;
int main(){
int s1, s2;
while (cin >> s1 >> s2 && s1 != 0 && s2 != 0){
int sum[5];
int max;
int num;
sum[0] = s1 + s2;
max = sum[0];
num = 0;
for (int i = 1; i < 5; i++){
cin >> s1 >> s2;
sum[i] = s1 + s2;
if (max < sum[i]){
max = sum[i];
num = i;
}
}
cout << (char)('A' + num) << ' ' << max << endl;
}
}*/
/******************************************************************************
pck 2009 t3
?????§??¬?´???°-??????????????????????????????
*******************************************************************************/
/*
#include <iostream>
using namespace std;
#include <algorithm>
int main(){
int x, y;
while (cin >> x >> y && x != 0 && y != 0){
if (y > x) swap(x, y);
int count = 0;
while (y != 0){
count++;
x = x % y;
swap(x, y);
}
cout << x << ' ' << count << endl;
}
}
*/
/************************************************************
PCK 2013 t3 ?????????????????????
************************************************************/
/*
#include <iostream>
using namespace std;
int main(){
int n;
while (cin >> n && n != 0){
int m = n;
bool flg = false;
int k;
for (int i = 0; i < n; i++){
cin >> k;
if (k >= 2) flg = true;
if (k == 0) m--;
}
if (flg)
cout << m + 1 << endl;
else cout << "NA" << endl;
}
}
*/
/*******************************************
PCK 2013 t5 ??£??¢
********************************************/
/*
#include <iostream>
using namespace std;
int main(){
int q;
cin >> q;
for (int i = 0; i < q; i++){
int c, a, n;
int count = 0;
cin >> c >> a >> n;
for (; c > 0 && a > 0 && n > 0; count++){
c--; a--; n--;
}
for (; c >= 2 && a > 0; count++){
c -= 2; a--;
}
for (; c >= 3; count++)
c -= 3;
cout << count << endl;
}
}
*/
/*****************************************************************
PCK 2012 t1
10????§£???????????????????????????
*****************************************************************/
/*
#include <iostream>
using namespace std;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int sum = 0;
int s;
for (int i = 0; i < 10; i++){
cin >> s;
sum += s;
}
cout << sum << "\n";
}
*/
/*
#include <bitset>
#include <stack>
#include <stdio.h>
using namespace std;
class Game
{
public:
int k; // ????????????????????????????
int m; // ????????§????¶?????????????????????°
bitset <16> x; // ??????????????¶???
Game(int k1, int m1, bitset<16> x1)
{
k = k1;
m = m1;
x = x1;
}
};
int main()
{
int i1, i2, m, m1, n=1, n1, n2, c=1, k, k1, max;
bitset <16> x, y;
scanf("%d %d\n", &n, &c);
while (n > 0) {
// ??\???
bitset<16> a[n];
for (i1 = 0; i1 < n; i1++) {
for (i2 = 15; i2 >= 0; i2--) {
scanf("%d", &k);
a[i1].set(i2, k);
}
}
bitset<16> b[c];
for (i1 = 0; i1 < c; i1++) {
for (i2 = 15; i2 >= 0; i2--) {
scanf("%d", &k);
b[i1].set(i2, k);
}
}
// ??????
max = 0;
stack<Game> st;
st.push(Game(0, 0, a[0]));
while (!st.empty()) {
k = st.top().k;
m = st.top().m;
x = st.top().x;
st.pop();
for (i1 = 0; i1 < c; i1++) {
y = x;
n1 = y.count();
y ^= b[i1];
y &= x;
n2 = y.count();
m1 = m + n1 - n2;
if (k == n - 1) {
if (m1 > max)
max = m1;
}
else {
k1 = k + 1;
y |= a[k1];
st.push(Game(k1, m1, y));
}
}
}
printf("%d\n", max);
// ?¬?????????????
scanf("%d %d\n", &n, &c);
}
return 0;
}
*/
/*
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
int main(){
int n, m, a[101], b[101];
int point[101];
memset(point, 0, sizeof(point));
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> a[i];
for (int i = 1; i <= m; i++){
for (int j = 1; j <= n; j++){
cin >> b[j];
if (b[j] == a[i])point[j]++;
else point[a[i]]++;
}
}
for (int i = 1; i <= n; i++)
cout << point[i] << endl;
}*/
/* JOI ?°????
#include<iostream>
using namespace std;
#include<stdio.h>
int map[1002][1002]; //???????????????????
int dat[1002][1002];
//??????????????£?????¨?°´????????????????????????????????± int x[1000001],
y[1000001]; //[n]??????????????????????????????????????¨???????????§?¨? int
dx[4] = { 1,0,-1,0 }; //?§?????????? int dy[4] = { 0,1,0,-1 };
//?§?????????? int main()
{
int mx, my;
scanf("%d%d", &mx, &my);
for (int i = 0; i < mx + 2; i++) {
for (int j = 0; j < my + 2; j++) {
map[i][j] = 1000000000; dat[i][j] = 0; //?????????
}
}
for (int i = 0; i < mx; i++) {
for (int j = 0; j < my; j++) {
scanf("%d", &map[i + 1][j + 1]); //??\???
}
}
for (int i = 1; i <= mx; i++) {
for (int j = 1; j <= my; j++) {
x[map[i][j]] = i, y[map[i][j]] = j;
//???????????????????????´???????£?
}
}
int c = 0; //?°?????????°
for (int i = 1; i <= mx*my; i++){ //??????¨?
int r = 0;
for (int j = 0; j < 4; j++){ //4??????????§????
int a = x[i] + dx[j], b = y[i] + dy[j]; //?§???????
if (map[a][b] < i) //??????????????±?????? i
??\?????????
{
if (r == 0)r = dat[a][b];
//?§?????????§????????§??????????????? else if (r != dat[a][b])r = -1;
//??´??????????°????????????£????????????
}
}
if (r == 0)dat[x[i]][y[i]] = i;
//??¨?????????????????????????????°?°´????????? else dat[x[i]][y[i]] = r;
//????????¨???????????´????????°????????? if (r == -1)c++;
//?°?????????????
}
printf("%d\n", c);
}
*/
/* JOI 2006 ??¬??? t2 ??????????????? */
/*#include <iostream>
using namespace std;
int main() {
int n, k;
while (cin >> n >> k && n != 0 && k != 0) {
}
return 0;
}*/
/* ?????§?????? */
/*
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
int main() {
long long int n;
long long int k;
while (cin >> n >> k && n != 0 && k != 0) {
vector<long long int> v;
v.push_back(0);
for (long long int i = 1; i <= n; ++i) {
long long int m;
cin >> m;
v.push_back(m);
v[i] += v[i - 1];
}
long long int sum = 0;
for (long long int i = 1; i + k <= n; ++i) {
sum = max(sum, v[i + k] - v[i]);
}
cout << sum << endl;
}
return 0;
}*/
/* JOI 2016 t3 ?????????????????? */
/*
#include<iostream>
using namespace std;
#include<cstring>
int map[102][102] = { 1 };
int main(){
memset(map, 1, sizeof(map));
int n, m, d;
cin >> n >> m >> d;
for (int i = 0; i < n; ++i) {
for (int i2 = 0; i2 < m; ++i2) {
char str;
cin >> str;
if (str == '.') { map[i][i2] = 0; }
}
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
for (int i2 = 0; i2 < m; ++i2) {
if (map[i][i2] == 0) {
bool flg = true;
for (int j = 1; j < d; ++j) {
if (map[i + j][i2] != 0) {
flg = false;
break;
}
}
if (flg) { ++cnt; }
flg = true;
for (int j = 1; j < d; ++j) {
if (map[i][i2 + j] != 0) {
flg = false;
break;
}
}
if (flg) { ++cnt; }
}
}
}
cout << cnt << endl;
return 0;
}
*/
/*
#include<iostream>
using namespace std;
#include<string>
int dp[101][101] = { 0 };
int main(){
string s, t;
cin >> s >> t;
for (int i = 0; i <= t.size(); ++i) { dp[i][0] = 0; }
for (int i = 0; i <= s.size(); ++i) { dp[0][i] = 1; }
for (int i = 1; i <= t.size(); ++i) {
for (int i2 = 1; i2 <= s.size(); ++i2) {
if (t[i - 1] == s[i2 - 1]) {
dp[i][i2] = dp[i - 1][i2 - 1] + dp[i][i2 - 1];
}
else dp[i][i2] = dp[i][i2 - 1];
}
}
cout << dp[t.size()][s.size()] << endl;
return 0;
}
*/
/*
#include<iostream>
using namespace std;
#include<algorithm>
int a[100001];
int main() {
int n, k;
while (cin >> n >> k && n != 0 && k != 0) {
a[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
int maxi = 0;
for (int i = k; i <= n; ++i) {
maxi = max(maxi, a[i] - a[i - k]);
}
cout << maxi << endl;
}
return 0;
}
*/
/* JOI 2010 ?????´??? */
/*
#include<iostream>
using namespace std;
#include<algorithm>
unsigned long long dp[21][101] = { 0 };
int main() {
unsigned long long n, num;
cin >> n;
for (unsigned long long i = 0; i < n - 1; ++i) {
cin >> num;
if (i == 0) {
++dp[num][i];
}
else {
for (unsigned long long j = 0; j <= 20; ++j) {
if (dp[j][i - 1] != 0) {
if (j + num >= 0 && j + num <= 20) {
dp[j + num][i] += dp[j][i - 1];
}
if (j - num >= 0 && j - num <= 20) {
dp[j - num][i] += dp[j][i - 1];
}
}
}
}
}
cin >> num;
cout << dp[num][n - 2] << endl;
return 0;
}
*/
/* JOI 2010 t5 ????????? */
/*
#include<iostream>
using namespace std;
#include<cstring>
#include<queue>
typedef unsigned long long int ullint;
struct Q {
ullint x;
ullint y;
};
Q In(ullint x, ullint y) { Q a; a.x = x; a.y = y; return a; }
char map[1002][1002];
ullint num[1003][1003] = { 0 };
ullint xm[] = { 0,0,-1,1 };
ullint ym[] = { -1,1,0,0 };
int main() {
ullint h, w, n;
Q st;
cin >> h >> w >> n;
for (ullint i = 0; i <= h + 1; ++i) {
for (ullint i2 = 0; i2 <= w + 1; ++i2) {
map[i][i2] = 'X';
}
}
for (ullint i = 1; i <= h; ++i) {
for (ullint i2 = 1; i2 <= w; ++i2) {
char str;
cin >> str;
map[i][i2] = str;
if (str == 'S') { st.y = i; st.x = i2; }
}
}
queue<Q> que; que.push(st);
ullint number = 1;
ullint ans = 0;
for (number = 1; number <= n; ++number) {
while (!que.empty()) {
Q now;
now = que.front();
que.pop();
ullint memo = num[now.y][now.x];
if (map[now.y][now.x] == '0' + number) {
ans += memo;
for (ullint i = 0; i <= h + 1; ++i) {
for (ullint i2 = 0; i2 <= w + 1; ++i2) {
num[i][i2] = 0;
}
}
while (!que.empty()) { que.pop(); }
que.push(In(now.x, now.y));
break;
}
for (ullint i = 0; i < 4; ++i) {
if (map[now.y + ym[i]][now.x + xm[i]] != 'X') {
if (num[now.y + ym[i]][now.x + xm[i]] ==
0) { num[now.y + ym[i]][now.x + xm[i]] = memo + 1; que.push(In(now.x + xm[i],
now.y + ym[i]));
}
}
}
}
}
cout << ans << endl;
return 0;
}
*/
/* JOI 2014 t3 ?°??±??????±?£? */
/*
#include<iostream>
using namespace std;
#include<cstring>
int main() {
int h, w;
int map[102][102];
char data[102][102];
memset(map, -1, sizeof(map));
cin >> h >> w;
for (int i = 0; i < h; ++i) {
for (int i2 = 0; i2 < w; ++i2) {
char str;
cin >> str;
data[i][i2] = str;
if (data[i][i2] == 'c') {
map[i][i2] = 0;
}
if (data[i][i2] == '.') {
if (i2 != 0) {
if (map[i][i2 - 1] != -1) {
map[i][i2] = map[i][i2 - 1] + 1;
}
}
}
}
}
for (int i = 0; i < h; ++i) {
for (int i2 = 0; i2 < w; ++i2) {
cout << map[i][i2];
if (i2 < w - 1) {
cout << " ";
}
else {
cout << endl;
}
}
}
return 0;
}
*/
/* JOI 2014 t4 ?????????????????? */
/*
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
#define INF 1000000001
typedef long long int llint;
llint dp[1002][1002];
int main() {
llint n, m;
llint d[1002];
llint c[1002];
memset(dp, 0, sizeof(dp));
cin >> n >> m;
for (llint i = 1; i <= n; ++i) {
cin >> d[i];
}
for (llint i = 1; i <= m; ++i) {
cin >> c[i];
}
for (llint day = 0; day <= m; ++day) {
for (llint num = 1; num <= n; ++num) {
dp[day][num] = INF;
}
}
for (llint day = 1; day <= m; ++day) {
for (llint num = 1; num <= n; ++num) {
dp[day][num] = min(dp[day - 1][num], dp[day - 1][num -
1] + c[day] * d[num]);
}
}
cout << dp[m][n] << endl;
return 0;
}
*/
/* JOI 2014 t5 ????????? */
#include <iostream>
using namespace std;
#include <cstring>
#include <queue>
struct XY {
int x;
int y;
};
int map[1002][1002] = {};
bool map_flg[1002][1002] = {};
int xm[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int ym[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int main() {
memset(map, -1, sizeof(map));
memset(map_flg, true, sizeof(map_flg));
int h, w;
XY st;
queue<XY> que;
cin >> h >> w;
for (int i = 1; i <= h; ++i) {
for (int i2 = 1; i2 <= w; ++i2) {
char str;
cin >> str;
if (str <= '9' || str > '0') {
map[i][i2] = str - '0';
}
if (str == '.') {
map[i][i2] = 0;
st.y = i;
st.x = i2;
que.push(st);
}
}
}
queue<XY> temp;
int ans = 0;
do {
while (!que.empty()) {
XY now = que.front();
que.pop();
map_flg[now.y][now.x] = false;
for (int i = 0; i < 8; ++i) {
if (map_flg[now.y + ym[i]][now.x + xm[i]]) {
if (map[now.y + ym[i]][now.x + xm[i]] > 0) {
--map[now.y + ym[i]][now.x + xm[i]];
if (map[now.y + ym[i]][now.x + xm[i]] == 0) {
XY next;
next.y = now.y + ym[i];
next.x = now.x + xm[i];
temp.push(next);
}
}
}
}
}
if (temp.empty()) {
break;
} else {
++ans;
while (!temp.empty()) {
que.push(temp.front());
temp.pop();
}
}
} while (1);
cout << ans << endl;
return 0;
}
|
/*************************
PCK 2012 t1
************************/
/*#include <stdio.h>
int main(){
int s;
int sum = 0;
int i;
for ( i = 0; i < 10; i++){
scanf("%d", &s);
sum += s;
}
printf("%d\n", sum);
}*/
/************************
PCK 2012 t2
************************/
/*
#include <stdio.h>
int main(){
int b[3];
int i;
for ( i = 0; i < 3; i++){
scanf("%d", &b[i]);
}
if (b[2] == 1)
printf("Open\n");
else if (b[0] == 1){
if (b[1] == 1)
printf("Open\n");
else
printf("Close\n");
}
else
printf("Close\n");
}*/
/****************************
PCK 2012 t3
****************************/
/*
#include <stdio.h>
int main(){
int n;
int h[101] = { 0 };
int i;
int temp;
scanf("%d", &n);
while (n != 0){
for (i = 0; i < n + 1; i++){
scanf("%d", &h[i]);
}
if ((h[1] - h[0]) + h[1] == h[2]){
temp = h[1] - h[0];
for (i = 1; i < n; i++){
if (h[i] + temp != h[i + 1]){
printf("%d\n", h[i + 1]);
break;
}
}
}
else{
if ((h[2] - h[1]) + h[2] == h[3])
printf("%d\n", h[0]);
else if ((h[1] - h[0]) + h[2] != h[3])
printf("%d\n", h[2]);
else
printf("%d\n", h[1]);
}
scanf("%d", &n);
}
}
*/
/*************************************
PCK 2012 t4
(2??´????±?????????????)
*************************************/
/*
#include <stdio.h>
int main(void)
{
int n;
int num[4], l, s, count, i, j, temp;
scanf("%d", &n);
while (n != 0000){
count = 0;
while (n != 6174){
num[0] = n / 1000;
num[1] = (n - num[0] * 1000) / 100;
num[2] = (n - num[0] * 1000 - num[1] * 100) / 10;
num[3] = n - num[0] * 1000 - num[1] * 100 - num[2] * 10;
if (num[0] == num[1] && num[1] == num[2] && num[2] ==
num[3]) //??¨???????????°???(1111??????7777??????)?????? break; for (i = 0; i<3;
i++){ for (j = i + 1; j<4; j++){ if (num[i]>num[j]){ temp = num[i]; num[i] =
num[j]; num[j] = temp;
}
}
}
s = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
l = num[3] * 1000 + num[2] * 100 + num[1] * 10 + num[0];
n = l - s;
count++;
}
if (n == 6174 || count>0)
printf("%d\n", count);
else
printf("NA\n");
scanf("%d", &n);
}
return 0;
}*/
/*************************************
PCK 2012 t5
*************************************/
/*
#include <algorithm>
#include <stdio.h>
int *p, *j;
int bfs(int n,int ){
return max(n * )
}
int main(){
int n;
scanf("%d", &n);
while (n != 0){
p = new int[n];
j = new int[n - 1];
for (int i = 0; i < n; i++)
scanf("%d", &p[i]);
for (int i = 0; i < n - 1; i++)
scanf("%d", &j[i]);
scanf("%d", &n);
}
}*/
// PCK 2013 t4 ????????????????????????
/*/
#include <iostream>
using namespace std;
#include <algorithm>
#include <cstring>
int main(){
int n;
int hand[100];
while (cin >> n && n != 0){
memset(hand, 0, sizeof(hand));
int field = 0;
int num = 0;
for (int i = 0; i < 100; i++){
char c;
cin >> c;
if (c == 'M') hand[num]++;
else if (c == 'S'){ field += hand[num] + 1; hand[num] =
0; } else { hand[num] += field + 1; field = 0; } if (num + 1 < n) num++; else
num = 0;
}
sort(hand, hand + n);
for (int i = 0; i < n; i++)
cout << hand[i] << ' ';
cout << field << endl;
}
}
*/
/*********************************************************************
pck 2009 t1 ?????\?????§???????°??????????????
*********************************************************************/
/*
#include <iostream>
using namespace std;
int main(){
int s1, s2;
while (cin >> s1 >> s2 && s1 != 0 && s2 != 0){
int sum[5];
int max;
int num;
sum[0] = s1 + s2;
max = sum[0];
num = 0;
for (int i = 1; i < 5; i++){
cin >> s1 >> s2;
sum[i] = s1 + s2;
if (max < sum[i]){
max = sum[i];
num = i;
}
}
cout << (char)('A' + num) << ' ' << max << endl;
}
}*/
/******************************************************************************
pck 2009 t3
?????§??¬?´???°-??????????????????????????????
*******************************************************************************/
/*
#include <iostream>
using namespace std;
#include <algorithm>
int main(){
int x, y;
while (cin >> x >> y && x != 0 && y != 0){
if (y > x) swap(x, y);
int count = 0;
while (y != 0){
count++;
x = x % y;
swap(x, y);
}
cout << x << ' ' << count << endl;
}
}
*/
/************************************************************
PCK 2013 t3 ?????????????????????
************************************************************/
/*
#include <iostream>
using namespace std;
int main(){
int n;
while (cin >> n && n != 0){
int m = n;
bool flg = false;
int k;
for (int i = 0; i < n; i++){
cin >> k;
if (k >= 2) flg = true;
if (k == 0) m--;
}
if (flg)
cout << m + 1 << endl;
else cout << "NA" << endl;
}
}
*/
/*******************************************
PCK 2013 t5 ??£??¢
********************************************/
/*
#include <iostream>
using namespace std;
int main(){
int q;
cin >> q;
for (int i = 0; i < q; i++){
int c, a, n;
int count = 0;
cin >> c >> a >> n;
for (; c > 0 && a > 0 && n > 0; count++){
c--; a--; n--;
}
for (; c >= 2 && a > 0; count++){
c -= 2; a--;
}
for (; c >= 3; count++)
c -= 3;
cout << count << endl;
}
}
*/
/*****************************************************************
PCK 2012 t1
10????§£???????????????????????????
*****************************************************************/
/*
#include <iostream>
using namespace std;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int sum = 0;
int s;
for (int i = 0; i < 10; i++){
cin >> s;
sum += s;
}
cout << sum << "\n";
}
*/
/*
#include <bitset>
#include <stack>
#include <stdio.h>
using namespace std;
class Game
{
public:
int k; // ????????????????????????????
int m; // ????????§????¶?????????????????????°
bitset <16> x; // ??????????????¶???
Game(int k1, int m1, bitset<16> x1)
{
k = k1;
m = m1;
x = x1;
}
};
int main()
{
int i1, i2, m, m1, n=1, n1, n2, c=1, k, k1, max;
bitset <16> x, y;
scanf("%d %d\n", &n, &c);
while (n > 0) {
// ??\???
bitset<16> a[n];
for (i1 = 0; i1 < n; i1++) {
for (i2 = 15; i2 >= 0; i2--) {
scanf("%d", &k);
a[i1].set(i2, k);
}
}
bitset<16> b[c];
for (i1 = 0; i1 < c; i1++) {
for (i2 = 15; i2 >= 0; i2--) {
scanf("%d", &k);
b[i1].set(i2, k);
}
}
// ??????
max = 0;
stack<Game> st;
st.push(Game(0, 0, a[0]));
while (!st.empty()) {
k = st.top().k;
m = st.top().m;
x = st.top().x;
st.pop();
for (i1 = 0; i1 < c; i1++) {
y = x;
n1 = y.count();
y ^= b[i1];
y &= x;
n2 = y.count();
m1 = m + n1 - n2;
if (k == n - 1) {
if (m1 > max)
max = m1;
}
else {
k1 = k + 1;
y |= a[k1];
st.push(Game(k1, m1, y));
}
}
}
printf("%d\n", max);
// ?¬?????????????
scanf("%d %d\n", &n, &c);
}
return 0;
}
*/
/*
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
int main(){
int n, m, a[101], b[101];
int point[101];
memset(point, 0, sizeof(point));
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> a[i];
for (int i = 1; i <= m; i++){
for (int j = 1; j <= n; j++){
cin >> b[j];
if (b[j] == a[i])point[j]++;
else point[a[i]]++;
}
}
for (int i = 1; i <= n; i++)
cout << point[i] << endl;
}*/
/* JOI ?°????
#include<iostream>
using namespace std;
#include<stdio.h>
int map[1002][1002]; //???????????????????
int dat[1002][1002];
//??????????????£?????¨?°´????????????????????????????????± int x[1000001],
y[1000001]; //[n]??????????????????????????????????????¨???????????§?¨? int
dx[4] = { 1,0,-1,0 }; //?§?????????? int dy[4] = { 0,1,0,-1 };
//?§?????????? int main()
{
int mx, my;
scanf("%d%d", &mx, &my);
for (int i = 0; i < mx + 2; i++) {
for (int j = 0; j < my + 2; j++) {
map[i][j] = 1000000000; dat[i][j] = 0; //?????????
}
}
for (int i = 0; i < mx; i++) {
for (int j = 0; j < my; j++) {
scanf("%d", &map[i + 1][j + 1]); //??\???
}
}
for (int i = 1; i <= mx; i++) {
for (int j = 1; j <= my; j++) {
x[map[i][j]] = i, y[map[i][j]] = j;
//???????????????????????´???????£?
}
}
int c = 0; //?°?????????°
for (int i = 1; i <= mx*my; i++){ //??????¨?
int r = 0;
for (int j = 0; j < 4; j++){ //4??????????§????
int a = x[i] + dx[j], b = y[i] + dy[j]; //?§???????
if (map[a][b] < i) //??????????????±?????? i
??\?????????
{
if (r == 0)r = dat[a][b];
//?§?????????§????????§??????????????? else if (r != dat[a][b])r = -1;
//??´??????????°????????????£????????????
}
}
if (r == 0)dat[x[i]][y[i]] = i;
//??¨?????????????????????????????°?°´????????? else dat[x[i]][y[i]] = r;
//????????¨???????????´????????°????????? if (r == -1)c++;
//?°?????????????
}
printf("%d\n", c);
}
*/
/* JOI 2006 ??¬??? t2 ??????????????? */
/*#include <iostream>
using namespace std;
int main() {
int n, k;
while (cin >> n >> k && n != 0 && k != 0) {
}
return 0;
}*/
/* ?????§?????? */
/*
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
int main() {
long long int n;
long long int k;
while (cin >> n >> k && n != 0 && k != 0) {
vector<long long int> v;
v.push_back(0);
for (long long int i = 1; i <= n; ++i) {
long long int m;
cin >> m;
v.push_back(m);
v[i] += v[i - 1];
}
long long int sum = 0;
for (long long int i = 1; i + k <= n; ++i) {
sum = max(sum, v[i + k] - v[i]);
}
cout << sum << endl;
}
return 0;
}*/
/* JOI 2016 t3 ?????????????????? */
/*
#include<iostream>
using namespace std;
#include<cstring>
int map[102][102] = { 1 };
int main(){
memset(map, 1, sizeof(map));
int n, m, d;
cin >> n >> m >> d;
for (int i = 0; i < n; ++i) {
for (int i2 = 0; i2 < m; ++i2) {
char str;
cin >> str;
if (str == '.') { map[i][i2] = 0; }
}
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
for (int i2 = 0; i2 < m; ++i2) {
if (map[i][i2] == 0) {
bool flg = true;
for (int j = 1; j < d; ++j) {
if (map[i + j][i2] != 0) {
flg = false;
break;
}
}
if (flg) { ++cnt; }
flg = true;
for (int j = 1; j < d; ++j) {
if (map[i][i2 + j] != 0) {
flg = false;
break;
}
}
if (flg) { ++cnt; }
}
}
}
cout << cnt << endl;
return 0;
}
*/
/*
#include<iostream>
using namespace std;
#include<string>
int dp[101][101] = { 0 };
int main(){
string s, t;
cin >> s >> t;
for (int i = 0; i <= t.size(); ++i) { dp[i][0] = 0; }
for (int i = 0; i <= s.size(); ++i) { dp[0][i] = 1; }
for (int i = 1; i <= t.size(); ++i) {
for (int i2 = 1; i2 <= s.size(); ++i2) {
if (t[i - 1] == s[i2 - 1]) {
dp[i][i2] = dp[i - 1][i2 - 1] + dp[i][i2 - 1];
}
else dp[i][i2] = dp[i][i2 - 1];
}
}
cout << dp[t.size()][s.size()] << endl;
return 0;
}
*/
/*
#include<iostream>
using namespace std;
#include<algorithm>
int a[100001];
int main() {
int n, k;
while (cin >> n >> k && n != 0 && k != 0) {
a[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
int maxi = 0;
for (int i = k; i <= n; ++i) {
maxi = max(maxi, a[i] - a[i - k]);
}
cout << maxi << endl;
}
return 0;
}
*/
/* JOI 2010 ?????´??? */
/*
#include<iostream>
using namespace std;
#include<algorithm>
unsigned long long dp[21][101] = { 0 };
int main() {
unsigned long long n, num;
cin >> n;
for (unsigned long long i = 0; i < n - 1; ++i) {
cin >> num;
if (i == 0) {
++dp[num][i];
}
else {
for (unsigned long long j = 0; j <= 20; ++j) {
if (dp[j][i - 1] != 0) {
if (j + num >= 0 && j + num <= 20) {
dp[j + num][i] += dp[j][i - 1];
}
if (j - num >= 0 && j - num <= 20) {
dp[j - num][i] += dp[j][i - 1];
}
}
}
}
}
cin >> num;
cout << dp[num][n - 2] << endl;
return 0;
}
*/
/* JOI 2010 t5 ????????? */
/*
#include<iostream>
using namespace std;
#include<cstring>
#include<queue>
typedef unsigned long long int ullint;
struct Q {
ullint x;
ullint y;
};
Q In(ullint x, ullint y) { Q a; a.x = x; a.y = y; return a; }
char map[1002][1002];
ullint num[1003][1003] = { 0 };
ullint xm[] = { 0,0,-1,1 };
ullint ym[] = { -1,1,0,0 };
int main() {
ullint h, w, n;
Q st;
cin >> h >> w >> n;
for (ullint i = 0; i <= h + 1; ++i) {
for (ullint i2 = 0; i2 <= w + 1; ++i2) {
map[i][i2] = 'X';
}
}
for (ullint i = 1; i <= h; ++i) {
for (ullint i2 = 1; i2 <= w; ++i2) {
char str;
cin >> str;
map[i][i2] = str;
if (str == 'S') { st.y = i; st.x = i2; }
}
}
queue<Q> que; que.push(st);
ullint number = 1;
ullint ans = 0;
for (number = 1; number <= n; ++number) {
while (!que.empty()) {
Q now;
now = que.front();
que.pop();
ullint memo = num[now.y][now.x];
if (map[now.y][now.x] == '0' + number) {
ans += memo;
for (ullint i = 0; i <= h + 1; ++i) {
for (ullint i2 = 0; i2 <= w + 1; ++i2) {
num[i][i2] = 0;
}
}
while (!que.empty()) { que.pop(); }
que.push(In(now.x, now.y));
break;
}
for (ullint i = 0; i < 4; ++i) {
if (map[now.y + ym[i]][now.x + xm[i]] != 'X') {
if (num[now.y + ym[i]][now.x + xm[i]] ==
0) { num[now.y + ym[i]][now.x + xm[i]] = memo + 1; que.push(In(now.x + xm[i],
now.y + ym[i]));
}
}
}
}
}
cout << ans << endl;
return 0;
}
*/
/* JOI 2014 t3 ?°??±??????±?£? */
/*
#include<iostream>
using namespace std;
#include<cstring>
int main() {
int h, w;
int map[102][102];
char data[102][102];
memset(map, -1, sizeof(map));
cin >> h >> w;
for (int i = 0; i < h; ++i) {
for (int i2 = 0; i2 < w; ++i2) {
char str;
cin >> str;
data[i][i2] = str;
if (data[i][i2] == 'c') {
map[i][i2] = 0;
}
if (data[i][i2] == '.') {
if (i2 != 0) {
if (map[i][i2 - 1] != -1) {
map[i][i2] = map[i][i2 - 1] + 1;
}
}
}
}
}
for (int i = 0; i < h; ++i) {
for (int i2 = 0; i2 < w; ++i2) {
cout << map[i][i2];
if (i2 < w - 1) {
cout << " ";
}
else {
cout << endl;
}
}
}
return 0;
}
*/
/* JOI 2014 t4 ?????????????????? */
/*
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
#define INF 1000000001
typedef long long int llint;
llint dp[1002][1002];
int main() {
llint n, m;
llint d[1002];
llint c[1002];
memset(dp, 0, sizeof(dp));
cin >> n >> m;
for (llint i = 1; i <= n; ++i) {
cin >> d[i];
}
for (llint i = 1; i <= m; ++i) {
cin >> c[i];
}
for (llint day = 0; day <= m; ++day) {
for (llint num = 1; num <= n; ++num) {
dp[day][num] = INF;
}
}
for (llint day = 1; day <= m; ++day) {
for (llint num = 1; num <= n; ++num) {
dp[day][num] = min(dp[day - 1][num], dp[day - 1][num -
1] + c[day] * d[num]);
}
}
cout << dp[m][n] << endl;
return 0;
}
*/
/* JOI 2014 t5 ????????? */
#include <iostream>
using namespace std;
#include <cstring>
#include <queue>
struct XY {
int x;
int y;
};
int map[1002][1002] = {};
bool map_flg[1002][1002] = {};
int xm[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ym[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
memset(map, -1, sizeof(map));
memset(map_flg, true, sizeof(map_flg));
int h, w;
XY st;
queue<XY> que;
cin >> h >> w;
for (int i = 1; i <= h; ++i) {
for (int i2 = 1; i2 <= w; ++i2) {
char str;
cin >> str;
if (str <= '9' || str > '0') {
map[i][i2] = str - '0';
}
if (str == '.') {
map[i][i2] = 0;
st.y = i;
st.x = i2;
que.push(st);
}
}
}
queue<XY> temp;
int ans = 0;
do {
while (!que.empty()) {
XY now = que.front();
que.pop();
map_flg[now.y][now.x] = false;
for (int i = 0; i < 8; ++i) {
if (map_flg[now.y + ym[i]][now.x + xm[i]]) {
if (map[now.y + ym[i]][now.x + xm[i]] > 0) {
--map[now.y + ym[i]][now.x + xm[i]];
if (map[now.y + ym[i]][now.x + xm[i]] == 0) {
XY next;
next.y = now.y + ym[i];
next.x = now.x + xm[i];
temp.push(next);
}
}
}
}
}
if (temp.empty()) {
break;
} else {
++ans;
while (!temp.empty()) {
que.push(temp.front());
temp.pop();
}
}
} while (1);
cout << ans << endl;
return 0;
}
|
[["-", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13], ["-", 0, 30, 0, 43, 49, 50, 51, 83, 0, 21]]
| 1
| 566
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n, m, x, y, a, b, c;
scanf("%d%d", &n, &m);
scanf("%d", &x);
vector<int> e(n + 1);
for (int i = 1; i < m; i++) {
scanf("%d", &y);
if (x < y)
e[x]++, e[y]--;
else
e[x]--;
e[y]++;
x = y;
}
ll ans = 0, num = 0;
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
num += e[i];
ans += min(num * a, num * b + c);
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n, m, x, y, a, b, c;
scanf("%d%d", &n, &m);
scanf("%d", &x);
vector<int> e(n + 1);
for (int i = 1; i < m; i++) {
scanf("%d", &y);
if (x < y)
e[x]++, e[y]--;
else
e[x]--, e[y]++;
x = y;
}
ll ans = 0, num = 0;
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
num += e[i];
ans += min(num * a, num * b + c);
}
printf("%lld\n", ans);
}
|
[["-", 8, 9, 0, 57, 75, 76, 0, 1, 0, 35], ["+", 0, 57, 75, 76, 0, 1, 0, 34, 0, 21]]
| 1
| 200
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define N_MAX 100000
#define M_MAX 100000
int main() {
int N, A[N_MAX], B[N_MAX], C[N_MAX], M;
int P[M_MAX];
int thcou[N_MAX + 1] = {0};
scanf("%d%d", &N, &M);
FOR(i, 0, M) { scanf("%d", &P[i]); }
FOR(i, 0, N - 1) { scanf("%d%d%d", &A[i], &B[i], &C[i]); }
FOR(i, 0, M - 1) {
int x = P[i], y = P[i + 1];
if (x > y)
swap(x, y);
x--;
y--;
thcou[x]++;
thcou[y]--;
}
FOR(i, 1, N) { thcou[i] += thcou[i - 1]; }
long long int ans = 0;
FOR(i, 0, M) {
ans += min((long long)A[i] * thcou[i], (long long)B[i] * thcou[i] + C[i]);
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define N_MAX 100000
#define M_MAX 100000
int main() {
int N, A[N_MAX], B[N_MAX], C[N_MAX], M;
int P[M_MAX];
int thcou[N_MAX + 1] = {0};
scanf("%d%d", &N, &M);
FOR(i, 0, M) { scanf("%d", &P[i]); }
FOR(i, 0, N - 1) { scanf("%d%d%d", &A[i], &B[i], &C[i]); }
FOR(i, 0, M - 1) {
int x = P[i], y = P[i + 1];
if (x > y)
swap(x, y);
x--;
y--;
thcou[x]++;
thcou[y]--;
}
FOR(i, 1, N) { thcou[i] += thcou[i - 1]; }
long long int ans = 0;
FOR(i, 0, N) {
ans += min((long long)A[i] * thcou[i], (long long)B[i] * thcou[i] + C[i]);
}
printf("%lld\n", ans);
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
| 1
| 294
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100005
int n, m, a, b, c, cnt = 0, from[N], to[N], f, t, s = 1, e = 1;
int ans = 0;
signed main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
cin >> t;
for (int i = 1; i < m; ++i) {
f = t;
cin >> t;
from[i] = min(f, t);
to[i] = max(f, t);
}
sort(from + 1, from + m - 1);
sort(to + 1, to + m - 1);
for (int i = 1; i < n; ++i) {
cin >> a >> b >> c;
while (i == from[s]) {
cnt++;
s++;
}
while (i == to[e]) {
cnt--;
e++;
}
ans += min(a * cnt, c + b * cnt);
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100005
int n, m, a, b, c, cnt = 0, from[N], to[N], f, t, s = 1, e = 1;
int ans = 0;
signed main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
cin >> t;
for (int i = 1; i < m; ++i) {
f = t;
cin >> t;
from[i] = min(f, t);
to[i] = max(f, t);
}
sort(from + 1, from + m);
sort(to + 1, to + m);
for (int i = 1; i < n; ++i) {
cin >> a >> b >> c;
while (i == from[s]) {
cnt++;
s++;
}
while (i == to[e]) {
cnt--;
e++;
}
ans += min(a * cnt, c + b * cnt);
}
cout << ans << "\n";
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 232
|
#include <bits/stdc++.h>
#define int long
using namespace std;
int i, j, k;
int x, y;
int p[100005];
int l, r;
int M, N;
int a[100010];
int b[100010];
int c[100010];
int cnt[100010];
signed main() {
ios_base::sync_with_stdio(0);
cin >> M >> N;
for (i = 1; i <= N; i++)
cin >> p[i];
for (i = 1; i < M; i++) {
cin >> a[i] >> b[i] >> c[i];
}
for (i = 1; i < M; i++) {
if (p[i] < p[i + 1])
for (j = p[i]; j < p[i + 1]; j++)
cnt[j]++;
else
for (j = p[i] - 1; j >= p[i + 1]; j--)
cnt[j]++;
}
int kq = 0;
for (i = 1; i < M; i++) {
kq += min(a[i] * cnt[i], c[i] + b[i] * cnt[i]);
}
cout << kq << endl;
}
|
#include <bits/stdc++.h>
#define int long
using namespace std;
int i, j, k;
int x, y;
int p[100005];
int l, r;
int M, N;
int a[100010];
int b[100010];
int c[100010];
int cnt[100010];
signed main() {
ios_base::sync_with_stdio(0);
cin >> M >> N;
for (i = 1; i <= N; i++)
cin >> p[i];
for (i = 1; i < M; i++) {
cin >> a[i] >> b[i] >> c[i];
}
for (i = 1; i < N; i++) {
if (p[i] < p[i + 1])
for (j = p[i]; j < p[i + 1]; j++)
cnt[j]++;
else
for (j = p[i] - 1; j >= p[i + 1]; j--)
cnt[j]++;
}
int kq = 0;
for (i = 1; i < M; i++) {
kq += min(a[i] * cnt[i], c[i] + b[i] * cnt[i]);
}
cout << kq << endl;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
| 1
| 274
|
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
int main(void) {
int n, m;
cin >> n >> m;
int p[100010];
for (int i = 0; i < m; i++) {
cin >> p[i];
}
int array[100010] = {};
//?????´
/*for(int i = 0; i < m-1; i++){
if(p[i] < p[i+1]){
for(int j = p[i]; j < p[i+1]; j++){
array[j]++;
}
}else{
for(int j = p[i+1]; j < p[i]; j++){
array[j]++;
}
}
}*/
//?????¢??????
for (int i = 0; i < m - 1; i++) {
if (p[i] < p[i + 1]) {
array[p[i]]++;
array[p[i + 1]]--;
} else {
array[p[i + 1]]++;
array[p[i]]--;
}
}
for (int i = 1; i < m; i++) {
array[i + 1] += array[i];
}
long long ans = 0;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
ans += min(a * array[i], c + b * array[i]);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
int main(void) {
int n, m;
cin >> n >> m;
int p[100010];
for (int i = 0; i < m; i++) {
cin >> p[i];
}
int array[100010] = {};
//テヲツ?堙ァツ崢エ
/*for(int i = 0; i < m-1; i++){
if(p[i] < p[i+1]){
for(int j = p[i]; j < p[i+1]; j++){
array[j]++;
}
}else{
for(int j = p[i+1]; j < p[i]; j++){
array[j]++;
}
}
}*/
//テ」ツつ、テ」ツδ「テ」ツつケテヲツウツ?
for (int i = 0; i < m - 1; i++) {
if (p[i] < p[i + 1]) {
array[p[i]]++;
array[p[i + 1]]--;
} else {
array[p[i + 1]]++;
array[p[i]]--;
}
}
for (int i = 1; i < n; i++) {
array[i + 1] += array[i];
}
long long ans = 0;
for (int i = 1; i < n; i++) {
long long a, b, c;
cin >> a >> b >> c;
ans += min(a * array[i], c + b * array[i]);
}
cout << ans << endl;
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 8, 9, 0, 7, 8, 9, 0, 43, 39, 40], ["+", 0, 7, 8, 9, 0, 43, 39, 86, 0, 96]]
| 1
| 240
|
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
const lli MAXN = 2002;
const lli INF = 1LL << 60;
lli N, A[MAXN];
lli dp[MAXN][MAXN];
int main() {
while (cin >> N) {
for (lli i = 0; i < N; ++i)
cin >> A[i];
memset(dp, -1, sizeof(dp));
for (lli i = 0; i < N; ++i) {
dp[N - 1][i + 1] = A[i];
}
for (lli l = N - 1; l > 0; --l) {
for (lli i = 0; i < N; ++i) {
if (dp[l][i] == -1)
continue;
lli j = (i + l - 1) % N;
if ((N - l) % 2) {
if (A[i] < A[j]) {
dp[l - 1][i] = max(dp[l - 1][i], dp[l][i]);
} else {
dp[l - 1][(i + 1) % N] = max(dp[l - 1][(i + 1) % N], dp[l][i]);
}
} else {
dp[l - 1][i] = max(dp[l - 1][i], dp[l][i] + A[j]);
dp[l - 1][(i + 1) % N] = max(dp[l - 1][(i + 1) % N], dp[l][i] + A[i]);
}
}
}
lli res = 0;
for (lli i = 0; i < N; ++i) {
res = max(res, dp[0][i]);
}
cout << res << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
const lli MAXN = 2002;
const lli INF = 1LL << 60;
lli N, A[MAXN];
lli dp[MAXN][MAXN];
int main() {
while (cin >> N) {
for (lli i = 0; i < N; ++i)
cin >> A[i];
memset(dp, -1, sizeof(dp));
for (lli i = 0; i < N; ++i) {
dp[N - 1][(i + 1) % N] = A[i];
}
for (lli l = N - 1; l > 0; --l) {
for (lli i = 0; i < N; ++i) {
if (dp[l][i] == -1)
continue;
lli j = (i + l - 1) % N;
if ((N - l) % 2) {
if (A[i] < A[j]) {
dp[l - 1][i] = max(dp[l - 1][i], dp[l][i]);
} else {
dp[l - 1][(i + 1) % N] = max(dp[l - 1][(i + 1) % N], dp[l][i]);
}
} else {
dp[l - 1][i] = max(dp[l - 1][i], dp[l][i] + A[j]);
dp[l - 1][(i + 1) % N] = max(dp[l - 1][(i + 1) % N], dp[l][i] + A[i]);
}
}
}
lli res = 0;
for (lli i = 0; i < N; ++i) {
res = max(res, dp[0][i]);
}
cout << res << endl;
}
return 0;
}
|
[["+", 31, 69, 341, 342, 0, 16, 31, 23, 0, 24], ["+", 31, 69, 341, 342, 0, 16, 31, 23, 0, 25], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 109], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 22]]
| 1
| 418
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 2010
#define prev(x) ((x - 1 + N) % N)
#define next(x) ((x + 1) % N)
typedef long long ll;
ll N, A[MAX_N];
ll dp[MAX_N][MAX_N];
int solve(int L, int R, int P) {
if (L == R) {
return (P == 0 ? A[L] : 0);
}
if (dp[L][R] >= 0) {
return dp[L][R];
}
ll res = 0;
if (P == 0) {
res = max(res, solve(prev(L), R, 1) + A[L]);
res = max(res, solve(L, next(R), 1) + A[R]);
} else {
if (A[L] < A[R]) {
res = solve(L, next(R), 0);
} else {
res = solve(prev(L), R, 0);
}
}
return dp[L][R] = res;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
memset(dp, -1, sizeof(dp));
ll res = 0;
for (int i = 0; i < N; i++) {
res = max(res, solve(prev(i), next(i), 1) + A[i]);
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 2010
#define prev(x) ((x - 1 + N) % N)
#define next(x) ((x + 1) % N)
typedef long long ll;
ll N, A[MAX_N];
ll dp[MAX_N][MAX_N];
ll solve(int L, int R, int P) {
if (L == R) {
return (P == 0 ? A[L] : 0);
}
if (dp[L][R] >= 0) {
return dp[L][R];
}
ll res = 0;
if (P == 0) {
res = max(res, solve(prev(L), R, 1) + A[L]);
res = max(res, solve(L, next(R), 1) + A[R]);
} else {
if (A[L] < A[R]) {
res = solve(L, next(R), 0);
} else {
res = solve(prev(L), R, 0);
}
}
return dp[L][R] = res;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
memset(dp, -1, sizeof(dp));
ll res = 0;
for (int i = 0; i < N; i++) {
res = max(res, solve(prev(i), next(i), 1) + A[i]);
}
cout << res << endl;
return 0;
}
|
[["-", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 14, 39, 78]]
| 1
| 324
|
#include <iostream>
using namespace std;
int n, a[2000];
long long int dp[2000][2000];
long long int ans = 0;
int main() {
// part1
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
// part2
for (int i = 0; i < n; i++)
dp[0][i] = a[i];
// part3
for (int i = 1; i < n; i++)
for (int j = 0; j < n; j++)
if (i & 1) {
if (a[(j + i) % n] >= a[(j + n - 1) % n])
dp[i][j] = dp[i - 1][j];
if (a[j] >= a[(j + i + 1) % n])
dp[i][j] = max(dp[i][j], dp[i - 1][(j + 1) % n]);
} else
dp[i][j] = max(dp[i - 1][j + 1] + a[j], dp[i - 1][j] + a[(j + i) % n]);
// part4
for (int i = 0; i < n; i++)
ans = max(ans, dp[n - 1][i]);
cout << ans << endl;
return 0;
}
|
#include <iostream>
using namespace std;
int n, a[2000];
long long int dp[2000][2000];
long long int ans = 0;
int main() {
// part1
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
// part2
for (int i = 0; i < n; i++)
dp[0][i] = a[i];
// part3
for (int i = 1; i < n; i++)
for (int j = 0; j < n; j++)
if (i & 1) {
if (a[(j + i) % n] >= a[(j + n - 1) % n])
dp[i][j] = dp[i - 1][j];
if (a[j] >= a[(j + i + 1) % n])
dp[i][j] = max(dp[i][j], dp[i - 1][(j + 1) % n]);
} else
dp[i][j] =
max(dp[i - 1][(j + 1) % n] + a[j], dp[i - 1][j] + a[(j + i) % n]);
// part4
for (int i = 0; i < n; i++)
ans = max(ans, dp[n - 1][i]);
cout << ans << endl;
return 0;
}
|
[["+", 31, 69, 341, 342, 0, 16, 31, 23, 0, 24], ["+", 31, 69, 341, 342, 0, 16, 31, 23, 0, 25], ["+", 0, 16, 31, 69, 341, 342, 0, 16, 17, 109], ["+", 0, 16, 31, 69, 341, 342, 0, 16, 12, 22]]
| 1
| 319
|
#include <stdio.h>
#define INF 2001001001001001001
long long vec[100010][2];
long long P[100010];
int main(void) {
int n, q;
int i, j, k;
int in1, in2, x;
long long t, pos;
vec[0][0] = (-1) * INF;
vec[0][1] = 1;
scanf("%d%lld%d", &n, &t, &q);
for (i = 1; i <= n; i++) {
scanf("%d%d", &in1, &in2);
vec[i][0] = in1;
vec[i][1] = in2;
}
vec[n + 1][0] = INF;
vec[n + 1][1] = 2;
for (i = 1; i <= n; i++) {
if (vec[i][1] == 1 && vec[i + 1][1] == 2) {
pos = (vec[i][0] + vec[i + 1][0]) / 2;
for (j = i; j >= 0; j--) {
if (vec[j][1] == 2)
break;
P[j] = pos;
}
for (j = i + 1; j <= n + 1; j++) {
if (vec[j][1] == 1)
break;
P[j] = pos;
}
}
}
for (i = 0; i < q; i++) {
scanf("%d", &x);
if (vec[x][1] == 1) {
if (vec[x][0] + t < P[x])
printf("%lld\n", vec[x][0] + t);
else
printf("%lld\n", P[x]);
} else {
if (vec[x][0] - t > P[x])
printf("%lld\n", vec[x][0] - t);
else
printf("%lld\n", P[x]);
}
}
return 0;
}
|
#include <stdio.h>
#define INF 2001001001001001001
long long vec[100010][2];
long long P[100010];
int main(void) {
int n, q;
int i, j, k;
long long in1;
int in2, x;
long long t, pos;
vec[0][0] = (-1) * INF;
vec[0][1] = 1;
scanf("%d%lld%d", &n, &t, &q);
for (i = 1; i <= n; i++) {
scanf("%lld%d", &in1, &in2);
vec[i][0] = in1;
vec[i][1] = in2;
}
vec[n + 1][0] = INF;
vec[n + 1][1] = 2;
for (i = 0; i <= n; i++) {
if (vec[i][1] == 1 && vec[i + 1][1] == 2) {
pos = (vec[i][0] + vec[i + 1][0]) / 2;
for (j = i; j >= 0; j--) {
if (vec[j][1] == 2)
break;
P[j] = pos;
}
for (j = i + 1; j <= n + 1; j++) {
if (vec[j][1] == 1)
break;
P[j] = pos;
}
}
}
for (i = 0; i < q; i++) {
scanf("%d", &x);
if (vec[x][1] == 1) {
if (vec[x][0] + t < P[x])
printf("%lld\n", vec[x][0] + t);
else
printf("%lld\n", P[x]);
} else {
if (vec[x][0] - t > P[x])
printf("%lld\n", vec[x][0] - t);
else
printf("%lld\n", P[x]);
}
}
return 0;
}
|
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 21], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13]]
| 0
| 450
|
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
signed main() {
int n, t, q;
cin >> n >> t >> q;
vector<int> a(n + 1), d(n + 1), x(q);
rep(i, 0, n) cin >> a[i] >> d[i];
rep(i, 0, q) cin >> x[i];
vector<int> ans(n);
int l = 0, r = n;
rep(i, 0, n) {
if (d[i] == 1) {
l = i;
break;
}
ans[i] = a[i] - t;
}
repb(i, n - 1, 0) {
if (d[i] == 2) {
r = i + 1;
break;
}
ans[i] = a[i] + t;
}
d[r] = 1;
int st = l, now = 1, tmp = 0;
rep(i, l, r + 1) {
if (d[i] == now)
continue;
if (d[i] == 2)
tmp = (a[i - 1] + a[i]) / 2;
if (d[i] == 1) {
rep(j, st, i - 1) {
if (abs(a[j] - tmp) <= t)
ans[j] = tmp;
else if (d[j] == 1)
ans[j] = a[j] + t;
else
ans[j] = a[j] - t;
}
st = i;
}
now = d[i];
}
rep(i, 0, q) { cout << ans[x[i] - 1] << endl; }
}
|
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
signed main() {
int n, t, q;
cin >> n >> t >> q;
vector<int> a(n + 1), d(n + 1), x(q);
rep(i, 0, n) cin >> a[i] >> d[i];
rep(i, 0, q) cin >> x[i];
vector<int> ans(n);
int l = 0, r = n;
rep(i, 0, n) {
if (d[i] == 1) {
l = i;
break;
}
ans[i] = a[i] - t;
}
repb(i, n - 1, 0) {
if (d[i] == 2) {
r = i + 1;
break;
}
ans[i] = a[i] + t;
}
d[r] = 1;
int st = l, now = 1, tmp = 0;
rep(i, l, r + 1) {
if (d[i] == now)
continue;
if (d[i] == 2)
tmp = (a[i - 1] + a[i]) / 2;
if (d[i] == 1) {
rep(j, st, i) {
if (abs(a[j] - tmp) <= t)
ans[j] = tmp;
else if (d[j] == 1)
ans[j] = a[j] + t;
else
ans[j] = a[j] - t;
}
st = i;
}
now = d[i];
}
rep(i, 0, q) { cout << ans[x[i] - 1] << endl; }
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
| 1
| 413
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second
#define PI acos(-1.0)
#define INF 10e9 + 7
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
ll n, t;
ll a[100100], d[100100];
ll q, x;
int main() {
cin >> n >> t >> q;
FOR(i, 1, n + 1) { cin >> a[i] >> d[i]; }
REP(i, q) {
ll goal;
ll ga = -1, pa = -1;
cin >> x;
if (d[x] == 1) {
for (ll j = x + 1; j < n; j++) {
if (d[x] != d[j]) {
ga = j;
break;
} else
pa = j;
}
} else {
for (ll j = x - 1; j >= 0; j--) {
if (d[x] != d[j]) {
ga = j;
break;
} else
pa = j;
}
}
if (ga == -1) {
if (d[x] == 1)
cout << a[x] + t << endl;
else
cout << a[x] - t << endl;
} else {
ll goal;
if (pa == -1) {
goal = (a[ga] + a[x]) / 2;
} else {
goal = (a[ga] + a[pa]) / 2;
}
if (d[x] == 1) {
if (goal - a[x] <= t)
cout << goal << endl;
else
cout << a[x] + t << endl;
} else {
if (a[x] - goal <= t)
cout << goal << endl;
else
cout << a[x] - t << endl;
}
}
}
}
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second
#define PI acos(-1.0)
#define INF 10e9 + 7
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
ll n, t;
ll a[100100], d[100100];
ll q, x;
int main() {
cin >> n >> t >> q;
FOR(i, 1, n + 1) { cin >> a[i] >> d[i]; }
REP(i, q) {
ll goal;
ll ga = -1, pa = -1;
cin >> x;
if (d[x] == 1) {
for (ll j = x + 1; j < n; j++) {
if (d[x] != d[j]) {
ga = j;
break;
} else
pa = j;
}
} else {
for (ll j = x - 1; j >= 1; j--) {
if (d[x] != d[j]) {
ga = j;
break;
} else
pa = j;
}
}
if (ga == -1) {
if (d[x] == 1)
cout << a[x] + t << endl;
else
cout << a[x] - t << endl;
} else {
ll goal;
if (pa == -1) {
goal = (a[ga] + a[x]) / 2;
} else {
goal = (a[ga] + a[pa]) / 2;
}
if (d[x] == 1) {
if (goal - a[x] <= t)
cout << goal << endl;
else
cout << a[x] + t << endl;
} else {
if (a[x] - goal <= t)
cout << goal << endl;
else
cout << a[x] - t << endl;
}
}
}
}
|
[["-", 75, 76, 0, 9, 0, 7, 15, 16, 12, 13], ["+", 75, 76, 0, 9, 0, 7, 15, 16, 12, 13]]
| 1
| 474
|
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
#define INF 1000000000
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef pair<ll, ll> P;
ll n, m, k, s;
vector<ll> G[200000];
vector<P> g[200000];
ll c[200000];
ll d[200000];
bool used[200000];
bool is_d[200000];
bool is_z[200000];
void get_d(ll cen) {
queue<P> que;
fill(used, used + n, false);
REP(i, G[cen].size()) {
que.push(P(G[cen][i], 1));
used[G[cen][i]] = is_d[G[cen][i]] = true;
}
while (!que.empty()) {
P p = que.front();
que.pop();
if (p.second >= k)
break;
REP(i, G[p.first].size()) {
if (used[G[p.first][i]])
continue;
que.push(P(G[p.first][i], p.second + 1));
used[G[p.first][i]] = is_d[G[p.first][i]] = true;
}
}
}
int main() {
scanf("%lld%lld%lld%lld", &n, &m, &k, &s);
ll p, q;
scanf("%lld%lld", &p, &q);
REP(i, k) {
scanf("%lld", c + i);
c[i]--;
}
REP(i, m) {
ll a, b;
scanf("%lld%lld", &a, &b);
a--;
b--;
G[a].pb(b);
G[b].pb(a);
}
fill(is_d, is_d + n, false);
REP(i, k)
get_d(c[i]);
fill(is_z, is_z + n, false);
REP(i, k)
is_z[c[i]] = true;
REP(i, n) {
if (is_z[i])
continue;
REP(j, G[i].size()) {
if (is_z[G[i][j]])
continue;
g[i].pb(P(G[i][j], is_d[G[i][j]] ? q : p));
}
}
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + n, INF);
d[0] = 0;
que.push(P(0, 0));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
REP(i, g[v].size()) {
P e = g[v][i];
if (d[e.first] > d[v] + e.second) {
d[e.first] = d[v] + e.second;
que.push(P(d[e.first], e.first));
}
}
}
printf("%lld\n", d[n - 1] - (is_d[n - 1] ? q : p));
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
#define INF 1000000000000000
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef pair<ll, ll> P;
ll n, m, k, s;
vector<ll> G[200000];
vector<P> g[200000];
ll c[200000];
ll d[200000];
bool used[200000];
bool is_d[200000];
bool is_z[200000];
void get_d(ll cen) {
queue<P> que;
fill(used, used + n, false);
REP(i, G[cen].size()) {
que.push(P(G[cen][i], 1));
used[G[cen][i]] = is_d[G[cen][i]] = true;
}
while (!que.empty()) {
P p = que.front();
que.pop();
if (p.second >= s)
break;
REP(i, G[p.first].size()) {
if (used[G[p.first][i]])
continue;
que.push(P(G[p.first][i], p.second + 1));
used[G[p.first][i]] = is_d[G[p.first][i]] = true;
}
}
}
int main() {
scanf("%lld%lld%lld%lld", &n, &m, &k, &s);
ll p, q;
scanf("%lld%lld", &p, &q);
REP(i, k) {
scanf("%lld", c + i);
c[i]--;
}
REP(i, m) {
ll a, b;
scanf("%lld%lld", &a, &b);
a--;
b--;
G[a].pb(b);
G[b].pb(a);
}
fill(is_d, is_d + n, false);
REP(i, k)
get_d(c[i]);
fill(is_z, is_z + n, false);
REP(i, k)
is_z[c[i]] = true;
REP(i, n) {
if (is_z[i])
continue;
REP(j, G[i].size()) {
if (is_z[G[i][j]])
continue;
g[i].pb(P(G[i][j], is_d[G[i][j]] ? q : p));
}
}
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + n, INF);
d[0] = 0;
que.push(P(0, 0));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
REP(i, g[v].size()) {
P e = g[v][i];
if (d[e.first] > d[v] + e.second) {
d[e.first] = d[v] + e.second;
que.push(P(d[e.first], e.first));
}
}
}
printf("%lld\n", d[n - 1] - (is_d[n - 1] ? q : p));
return 0;
}
|
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
| 1
| 825
|
class Heap
require 'forwardable'
extend Forwardable
def initialize
@array = []
@index = {}
end
def add(obj, key)
@array << [obj, key]
@index[obj] = @array.size - 1
lift_up(@array.size - 1)
end
def swap(i, j)
u = @array[i]
v = @array[j]
@index[u[0]], @index[v[0]] = j, i
@array[i], @array[j] = v, u
end
def lift_up(i)
return if i == 0
j = (i - 1) / 2
if (@array[i][1] <=> @array[j][1]) < 0
swap(i, j)
lift_up(j)
end
end
def del_min
raise "heap is empty!" if empty?
min = @array[0]
if size > 1
swap(0, @array.size-1)
@array.pop
lift_down(0)
else
@array.pop
end
@index.delete(min[0])
min
end
def inc_key(obj, diff)
i = @index[obj]
@array[i][1] += diff
lift_down(i)
end
def dec_key(obj, diff)
i = @index[obj]
@array[i][1] -= diff
lift_up(i)
end
def update_key(obj, new_key)
i = @index[obj]
return unless i
return if (new_key <=> @array[i][1]) >= 0
@array[i][1] = new_key
lift_up(i)
end
def min_obj
@array[0][0]
end
def min_key
@array[0][1]
end
def lift_down(i)
j = 2 * i + 1
k = j + 1
if k >= size
return if j >= size
if (@array[i][1] <=> @array[j][1]) > 0
swap(i, j)
lift_down(j)
end
else
l = ((@array[j][1] <=> @array[k][1]) < 0 ? j : k)
if (@array[i][1] <=> @array[l][1]) > 0
swap(i, l)
lift_down(l)
end
end
end
def_delegators :@array, :empty?, :size
end
module Graphical
require 'set'
def reachable_from(s, max_depth = nil)
queue = s.to_a
reached = Set[*queue]
depth = 0
loop {
break if max_depth && depth >= max_depth
next_queue = []
until queue.empty?
u = queue.shift
each_out_connection_of(u){|v, _|
next if reached.include?(v)
reached << v
next_queue << v
}
end
break if next_queue.empty?
queue = next_queue
depth += 1
}
return reached
end
def dijkstra(start)
raise "start must be Enumerable" if !start.kind_of?(Enumerable)
h = Heap.new
reached = Set[*start.to_a]
start.each{|s| h.add(s, 0)}
dist = {}
until h.empty?
u, key = h.del_min
return key if block_given? && yield(u, key)
dist[u] = key
each_out_connection_of(u){|v, l|
d = dist[u] + l
if reached.include?(v)
h.update_key(v, d)
else
h.add(v, d)
reached << v
end
}
end
block_given? ? nil : dist
end
end
class Graph
include Graphical
def initialize(*options)
@options = Set[*options]
end
def add_node(property = {})
Node.new(property)
end
def add_edge(u, v, property = {})
e = Edge.new(u, v, property)
u.add_edge(e)
v.add_edge(e)
e
end
def del_edge(e)
u, v = e.opposite.keys
u.del_edge(e)
v.del_edge(e)
e
end
class Node
def initialize(property)
@property = property
@edges = Set.new
end
def add_edge(e)
@edges << e
end
def del_edge(e)
@edges.delete(e)
end
def to_s
@property[:name] || super
end
def each_edge
@edges.each{|e|
yield e
}
end
attr_reader :property
end
class Edge
def initialize(u, v, property)
@opposite = {u => v, v => u}
@property = property
end
attr_reader :opposite, :property
def to_s
@property[:name] || super
end
def length
@property[:length] || 1
end
def actual_capacity(from)
v = @opposite[from]
cap = @property[:capacity]
flow = (@property[:flow] ||= 0)
to = (@property[:flow_to] ||= v)
to == v ? cap - flow : cap + flow
end
end
def each_out_connection_of(u)
u.each_edge{|e|
yield(e.opposite[u], e.length)
}
end
alias :each_in_connection_of :each_out_connection_of
end
class Digraph < Graph
def add_node(property = {})
Node.new(property)
end
def add_edge(u, v, property = {})
e = Edge.new(u, v, property)
u.add_out_edge(e)
v.add_in_edge(e)
e
end
def del_edge(e)
u, v = e.from, e.to
u.out_edges.delete(e)
v.in_edges.delete(e)
e
end
class Node < Graph::Node
def initialize(property)
@property = property
@out_edges = Set.new
@in_edges = Set.new
end
def add_out_edge(e)
@out_edges << e
end
def del_out_edge(e)
@out_edges.delete(e)
end
def add_in_edge(e)
@in_edges << e
end
def del_in_edge(e)
@in_edges.delete(e)
end
attr_reader :in_edges, :out_edges
end
class Edge < Graph::Edge
def initialize(u, v, property)
@from = u
@to = v
@property = property
end
attr_reader :from, :to
end
def each_out_connection_of(u)
u.out_edges.each{|e|
yield [e.to, e.length]
}
end
def each_in_connection_of(v)
v.in_edges.each{|e|
yield [e.from, e.length]
}
end
end
n, m, k, s = gets.split.map(&:to_i)
safe_fee, danger_fee = gets.split.map(&:to_i)
g = Digraph.new
nodes = (0..n).map { g.add_node }
p z = Set[*(1..k).map { gets.to_i }]
edges = []
m.times do
i, j = gets.split.map(&:to_i)
edges << g.add_edge(nodes[i], nodes[j])
edges << g.add_edge(nodes[j], nodes[i])
end
zombie_nodes = Set[*z.map {|i| nodes[i]}]
danger_nodes = g.reachable_from(zombie_nodes, s)
edges.each do |e|
if zombie_nodes.include?(e.to)
e.property[:length] = Float::INFINITY
elsif e.to == nodes[n]
e.property[:length] = 0
elsif danger_nodes.include?(e.to)
e.property[:length] = danger_fee
else
e.property[:length] = safe_fee
end
end
p g.dijkstra([nodes[1]]){|v| v == nodes[n]}
|
class Heap
require 'forwardable'
extend Forwardable
def initialize
@array = []
@index = {}
end
def add(obj, key)
@array << [obj, key]
@index[obj] = @array.size - 1
lift_up(@array.size - 1)
end
def swap(i, j)
u = @array[i]
v = @array[j]
@index[u[0]], @index[v[0]] = j, i
@array[i], @array[j] = v, u
end
def lift_up(i)
return if i == 0
j = (i - 1) / 2
if (@array[i][1] <=> @array[j][1]) < 0
swap(i, j)
lift_up(j)
end
end
def del_min
raise "heap is empty!" if empty?
min = @array[0]
if size > 1
swap(0, @array.size-1)
@array.pop
lift_down(0)
else
@array.pop
end
@index.delete(min[0])
min
end
def inc_key(obj, diff)
i = @index[obj]
@array[i][1] += diff
lift_down(i)
end
def dec_key(obj, diff)
i = @index[obj]
@array[i][1] -= diff
lift_up(i)
end
def update_key(obj, new_key)
i = @index[obj]
return unless i
return if (new_key <=> @array[i][1]) >= 0
@array[i][1] = new_key
lift_up(i)
end
def min_obj
@array[0][0]
end
def min_key
@array[0][1]
end
def lift_down(i)
j = 2 * i + 1
k = j + 1
if k >= size
return if j >= size
if (@array[i][1] <=> @array[j][1]) > 0
swap(i, j)
lift_down(j)
end
else
l = ((@array[j][1] <=> @array[k][1]) < 0 ? j : k)
if (@array[i][1] <=> @array[l][1]) > 0
swap(i, l)
lift_down(l)
end
end
end
def_delegators :@array, :empty?, :size
end
module Graphical
require 'set'
def reachable_from(s, max_depth = nil)
queue = s.to_a
reached = Set[*queue]
depth = 0
loop {
break if max_depth && depth >= max_depth
next_queue = []
until queue.empty?
u = queue.shift
each_out_connection_of(u){|v, _|
next if reached.include?(v)
reached << v
next_queue << v
}
end
break if next_queue.empty?
queue = next_queue
depth += 1
}
return reached
end
def dijkstra(start)
raise "start must be Enumerable" if !start.kind_of?(Enumerable)
h = Heap.new
reached = Set[*start.to_a]
start.each{|s| h.add(s, 0)}
dist = {}
until h.empty?
u, key = h.del_min
return key if block_given? && yield(u, key)
dist[u] = key
each_out_connection_of(u){|v, l|
d = dist[u] + l
if reached.include?(v)
h.update_key(v, d)
else
h.add(v, d)
reached << v
end
}
end
block_given? ? nil : dist
end
end
class Graph
include Graphical
def initialize(*options)
@options = Set[*options]
end
def add_node(property = {})
Node.new(property)
end
def add_edge(u, v, property = {})
e = Edge.new(u, v, property)
u.add_edge(e)
v.add_edge(e)
e
end
def del_edge(e)
u, v = e.opposite.keys
u.del_edge(e)
v.del_edge(e)
e
end
class Node
def initialize(property)
@property = property
@edges = Set.new
end
def add_edge(e)
@edges << e
end
def del_edge(e)
@edges.delete(e)
end
def to_s
@property[:name] || super
end
def each_edge
@edges.each{|e|
yield e
}
end
attr_reader :property
end
class Edge
def initialize(u, v, property)
@opposite = {u => v, v => u}
@property = property
end
attr_reader :opposite, :property
def to_s
@property[:name] || super
end
def length
@property[:length] || 1
end
def actual_capacity(from)
v = @opposite[from]
cap = @property[:capacity]
flow = (@property[:flow] ||= 0)
to = (@property[:flow_to] ||= v)
to == v ? cap - flow : cap + flow
end
end
def each_out_connection_of(u)
u.each_edge{|e|
yield(e.opposite[u], e.length)
}
end
alias :each_in_connection_of :each_out_connection_of
end
class Digraph < Graph
def add_node(property = {})
Node.new(property)
end
def add_edge(u, v, property = {})
e = Edge.new(u, v, property)
u.add_out_edge(e)
v.add_in_edge(e)
e
end
def del_edge(e)
u, v = e.from, e.to
u.out_edges.delete(e)
v.in_edges.delete(e)
e
end
class Node < Graph::Node
def initialize(property)
@property = property
@out_edges = Set.new
@in_edges = Set.new
end
def add_out_edge(e)
@out_edges << e
end
def del_out_edge(e)
@out_edges.delete(e)
end
def add_in_edge(e)
@in_edges << e
end
def del_in_edge(e)
@in_edges.delete(e)
end
attr_reader :in_edges, :out_edges
end
class Edge < Graph::Edge
def initialize(u, v, property)
@from = u
@to = v
@property = property
end
attr_reader :from, :to
end
def each_out_connection_of(u)
u.out_edges.each{|e|
yield [e.to, e.length]
}
end
def each_in_connection_of(v)
v.in_edges.each{|e|
yield [e.from, e.length]
}
end
end
n, m, k, s = gets.split.map(&:to_i)
safe_fee, danger_fee = gets.split.map(&:to_i)
g = Digraph.new
nodes = (0..n).map { g.add_node }
z = Set[*(1..k).map { gets.to_i }]
edges = []
m.times do
i, j = gets.split.map(&:to_i)
edges << g.add_edge(nodes[i], nodes[j])
edges << g.add_edge(nodes[j], nodes[i])
end
zombie_nodes = Set[*z.map {|i| nodes[i]}]
danger_nodes = g.reachable_from(zombie_nodes, s)
edges.each do |e|
if zombie_nodes.include?(e.to)
e.property[:length] = Float::INFINITY
elsif e.to == nodes[n]
e.property[:length] = 0
elsif danger_nodes.include?(e.to)
e.property[:length] = danger_fee
else
e.property[:length] = safe_fee
end
end
p g.dijkstra([nodes[1]]){|v| v == nodes[n]}
|
[["-", 36, 36, 36, 36, 0, 493, 0, 652, 735, 22]]
| 4
| 1,457
|
#include "bits/stdc++.h"
using namespace std;
int dp[1002][1002][16];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int H, W;
cin >> H >> W;
for (int i = 0; i < 1002; ++i)
for (int j = 0; j < 1002; ++j)
for (int k = 0; k < 16; ++k) {
dp[i][j][k] = 1e9;
}
dp[1][1][0] = 0;
vector<vector<int>> field(H + 2, vector<int>(W + 2));
for (int i = 0; i < H; ++i) {
string st;
cin >> st;
for (int j = 0; j < W; ++j) {
if (st[j] != '.') {
field[i + 1][j + 1] = st[j] - '0';
}
}
}
for (int nowy = 1; nowy < H + 1; ++nowy) {
for (int nowx = 1; nowx < W + 1; ++nowx) {
for (int n = 0; n < 16; ++n) {
const int eat_ld = n & 1;
const int eat_d = (n & 2) >> 1;
const int eat_r = (n & 4) >> 2;
const int eat_ru = (n & 8) >> 3;
if (1e9 <= dp[nowy][nowx][n])
continue;
vector<vector<char>> eats(4, vector<char>(4));
eats[2][0] = eat_ld;
eats[2][1] = eat_d;
eats[1][2] = eat_r;
eats[0][2] = eat_ru;
eats[1][1] = true;
for (int way = 0; way < 2; ++way) {
const int nextx = nowx + dx[way];
const int nexty = nowy + dy[way];
const int ax = 1 + dx[way];
const int ay = 1 + dy[way];
for (int w = 0; w < 4; ++w) {
int nexteat_ld = eats[2 + dy[way]][0 + dx[way]];
int nexteat_d = eats[2 + dy[way]][1 + dx[way]];
int nexteat_r = eats[1 + dy[way]][2 + dx[way]];
int nexteat_ru = eats[0 + dy[way]][2 + dx[way]];
int eat_sum = 0;
if (!eats[ay][ax]) {
if (ay == 2 + dy[way] && ax == 0 + dx[way])
nexteat_ld = true;
if (ay == 2 + dy[way] && ax == 1 + dx[way])
nexteat_d = true;
if (ay == 1 + dy[way] && ax == 2 + dx[way])
nexteat_r = true;
if (ay == 0 + dy[way] && ax == 2 + dx[way])
nexteat_ru = true;
eat_sum += field[nexty][nextx];
}
for (int v = 0; v < 4; ++v) {
if (v == w)
continue;
else {
if (!eats[ay + dy[v]][ax + dx[v]]) {
if (ay + dy[v] == 2 + dy[way] && ax + dx[v] == 0 + dx[way])
nexteat_ld = true;
if (ay + dy[v] == 2 + dy[way] && ax + dx[v] == 1 + dx[way])
nexteat_d = true;
if (ay + dy[v] == 1 + dy[way] && ax + dx[v] == 2 + dx[way])
nexteat_r = true;
if (ay + dy[v] == 0 + dy[way] && ax + dx[v] == 2 + dx[way])
nexteat_ru = true;
eat_sum += field[nexty + dy[v]][nextx + dx[v]];
}
}
}
int n =
dp[nowy][nowx][eat_ld + 2 * eat_d + 4 * eat_r + 8 * eat_ru] +
eat_sum;
dp[nexty][nextx]
[nexteat_ld + 2 * nexteat_d + 4 * nexteat_r + 8 * nexteat_ru] =
min(dp[nexty][nextx][nexteat_ld + 2 * nexteat_d +
4 * nexteat_r + 8 * nexteat_ru],
n);
}
}
}
}
}
int ans = 1e9;
for (int l = 0; l < 16; ++l) {
ans = min(ans, dp[H][W][l]);
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
int dp[1002][1002][16];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int H, W;
cin >> H >> W;
for (int i = 0; i < 1002; ++i)
for (int j = 0; j < 1002; ++j)
for (int k = 0; k < 16; ++k) {
dp[i][j][k] = 1e9;
}
dp[1][1][0] = 0;
vector<vector<int>> field(H + 3, vector<int>(W + 3));
for (int i = 0; i < H; ++i) {
string st;
cin >> st;
for (int j = 0; j < W; ++j) {
if (st[j] != '.') {
field[i + 1][j + 1] = st[j] - '0';
}
}
}
for (int nowy = 1; nowy < H + 1; ++nowy) {
for (int nowx = 1; nowx < W + 1; ++nowx) {
for (int n = 0; n < 16; ++n) {
const int eat_ld = n & 1;
const int eat_d = (n & 2) >> 1;
const int eat_r = (n & 4) >> 2;
const int eat_ru = (n & 8) >> 3;
if (1e9 <= dp[nowy][nowx][n])
continue;
vector<vector<char>> eats(4, vector<char>(4));
eats[2][0] = eat_ld;
eats[2][1] = eat_d;
eats[1][2] = eat_r;
eats[0][2] = eat_ru;
eats[1][1] = true;
for (int way = 0; way < 2; ++way) {
const int nextx = nowx + dx[way];
const int nexty = nowy + dy[way];
const int ax = 1 + dx[way];
const int ay = 1 + dy[way];
for (int w = 0; w < 4; ++w) {
int nexteat_ld = eats[2 + dy[way]][0 + dx[way]];
int nexteat_d = eats[2 + dy[way]][1 + dx[way]];
int nexteat_r = eats[1 + dy[way]][2 + dx[way]];
int nexteat_ru = eats[0 + dy[way]][2 + dx[way]];
int eat_sum = 0;
if (!eats[ay][ax]) {
if (ay == 2 + dy[way] && ax == 0 + dx[way])
nexteat_ld = true;
if (ay == 2 + dy[way] && ax == 1 + dx[way])
nexteat_d = true;
if (ay == 1 + dy[way] && ax == 2 + dx[way])
nexteat_r = true;
if (ay == 0 + dy[way] && ax == 2 + dx[way])
nexteat_ru = true;
eat_sum += field[nexty][nextx];
}
for (int v = 0; v < 4; ++v) {
if (v == w)
continue;
else {
if (!eats[ay + dy[v]][ax + dx[v]]) {
if (ay + dy[v] == 2 + dy[way] && ax + dx[v] == 0 + dx[way])
nexteat_ld = true;
if (ay + dy[v] == 2 + dy[way] && ax + dx[v] == 1 + dx[way])
nexteat_d = true;
if (ay + dy[v] == 1 + dy[way] && ax + dx[v] == 2 + dx[way])
nexteat_r = true;
if (ay + dy[v] == 0 + dy[way] && ax + dx[v] == 2 + dx[way])
nexteat_ru = true;
eat_sum += field[nexty + dy[v]][nextx + dx[v]];
}
}
}
int n =
dp[nowy][nowx][eat_ld + 2 * eat_d + 4 * eat_r + 8 * eat_ru] +
eat_sum;
dp[nexty][nextx]
[nexteat_ld + 2 * nexteat_d + 4 * nexteat_r + 8 * nexteat_ru] =
min(dp[nexty][nextx][nexteat_ld + 2 * nexteat_d +
4 * nexteat_r + 8 * nexteat_ru],
n);
}
}
}
}
}
int ans = 1e9;
for (int l = 0; l < 16; ++l) {
ans = min(ans, dp[H][W][l]);
}
cout << ans << endl;
return 0;
}
|
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["-", 51, 4, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 51, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 1,036
|
#include <bits/stdc++.h>
using namespace std;
int N, M, K;
long long L, R, A[20001], dp[20001];
int main() {
scanf("%d %d %d", &N, &M, &K);
for (int i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
dp[i] = 1e9;
L = R = A[i];
for (int j = 1; j <= M && i - j >= 0; j++) {
dp[i] = min(dp[i], dp[i - j] + (R - L) * j + K);
L = min(L, A[i - j]);
R = max(R, A[i - j]);
}
}
printf("%lld\n", dp[N]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M, K;
long long L, R, A[20001], dp[20001];
int main() {
scanf("%d %d %d", &N, &M, &K);
for (int i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
dp[i] = (1LL << 60);
L = R = A[i];
for (int j = 1; j <= M && i - j >= 0; j++) {
dp[i] = min(dp[i], dp[i - j] + (R - L) * j + K);
L = min(L, A[i - j]);
R = max(R, A[i - j]);
}
}
printf("%lld\n", dp[N]);
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 1, 0, 11, 12, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 23, 0, 16, 31, 13], ["+", 0, 1, 0, 11, 12, 23, 0, 16, 17, 151], ["+", 0, 1, 0, 11, 12, 23, 0, 16, 12, 13], ["+", 8, 9, 0, 1, 0, 11, 12, 23, 0, 25]]
| 1
| 192
|
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
long long int dp[20001];
int main() {
for (int i = 0; i < 20001; ++i) {
dp[i] = 0;
}
dp[0] = 0;
int N, M, K;
cin >> N >> M >> K;
vector<long long int> ws(N);
for (int i = 0; i < N; ++i) {
cin >> ws[i];
}
for (int i = 0; i < N; ++i) {
long long int amin = 1e18;
long long int amax = 0;
long long int cost = K;
for (int j = 0; j < min(N - i, M); ++j) {
amin = min(amin, ws[i + j]);
amax = max(amax, ws[i + j]);
dp[i + j + 1] =
min(dp[i + j + 1], dp[i] + cost + (j + 1) * (amax - amin));
}
}
cout << dp[N] << endl;
return 0;
}
|
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
long long int dp[20001];
int main() {
for (int i = 0; i < 20001; ++i) {
dp[i] = 1e18;
}
dp[0] = 0;
int N, M, K;
cin >> N >> M >> K;
vector<long long int> ws(N);
for (int i = 0; i < N; ++i) {
cin >> ws[i];
}
for (int i = 0; i < N; ++i) {
long long int amin = 1e18;
long long int amax = 0;
long long int cost = K;
for (int j = 0; j < min(N - i, M); ++j) {
amin = min(amin, ws[i + j]);
amax = max(amax, ws[i + j]);
dp[i + j + 1] =
min(dp[i + j + 1], dp[i] + cost + (j + 1) * (amax - amin));
}
}
cout << dp[N] << endl;
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13]]
| 1
| 278
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int64 INF = 1LL << 58;
int main() {
int N, M, K, A[20000];
int64 dp[20001];
scanf("%d %d %d", &N, &M, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
fill_n(dp, 20001, INF);
dp[0] = 0;
for (int i = 0; i < N; i++) {
int small = A[i], large = A[i];
for (int64 j = 0; j < min(M, N - i); j++) {
small = min(small, A[i + j]);
large = max(large, A[i + j]);
dp[i + j + 1] = min(dp[i + j + 1], dp[i] + K + (j + 1) * (large - small));
}
}
printf("%d\n", dp[N]);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int64 INF = 1LL << 58;
int main() {
int N, M, K, A[20000];
int64 dp[20001];
scanf("%d %d %d", &N, &M, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
fill_n(dp, 20001, INF);
dp[0] = 0;
for (int i = 0; i < N; i++) {
int small = A[i], large = A[i];
for (int64 j = 0; j < min(M, N - i); j++) {
small = min(small, A[i + j]);
large = max(large, A[i + j]);
dp[i + j + 1] = min(dp[i + j + 1], dp[i] + K + (j + 1) * (large - small));
}
}
printf("%lld\n", dp[N]);
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
| 1
| 239
|
#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;
signed main() {
int n, m, k, a[20000];
int dp[20001][1001];
cin >> n >> m >> k;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 1000; j++)
dp[i][j] = INF;
}
dp[0][0] = 0;
dp[0][1000] = 0;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
int mi = a[i - 1], ma = a[i - 1];
for (int j = 0; j < min(m, i); j++) {
dp[i][j] = dp[i - j - 1][1000] + k + (j + 1) * (ma - mi);
dp[i][1000] = min(dp[i][1000], dp[i][j]);
if (i - j - 2 >= 0) {
mi = min(mi, a[i - j - 2]);
ma = max(ma, a[i - j - 2]);
}
}
}
cout << dp[n][1000] << endl;
return 0;
}
|
#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;
signed main() {
int n, m, k, a[20000];
static int dp[20001][1001];
cin >> n >> m >> k;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 1000; j++)
dp[i][j] = INF;
}
dp[0][0] = 0;
dp[0][1000] = 0;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
int mi = a[i - 1], ma = a[i - 1];
for (int j = 0; j < min(m, i); j++) {
dp[i][j] = dp[i - j - 1][1000] + k + (j + 1) * (ma - mi);
dp[i][1000] = min(dp[i][1000], dp[i][j]);
if (i - j - 2 >= 0) {
mi = min(mi, a[i - j - 2]);
ma = max(ma, a[i - j - 2]);
}
}
}
cout << dp[n][1000] << endl;
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 43, 0, 114, 0, 115]]
| 1
| 304
|
//============================================================================
// Name : DPTest.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int N, M, K;
int A[20001] = {};
const long long INF = 1000000000;
long long dp[20001] = {};
int maxdp[20001][1001] = {};
int mindp[20001][1001] = {};
int maxD(int n2, int n3) {
int ans2 = A[n2 + n3];
if (maxdp[n2][n3] > 0)
return maxdp[n2][n3];
if (n3 > 1)
ans2 = max(maxD(n2, n3 - 1), A[n2 + n3]);
maxdp[n2][n3] = ans2;
return ans2;
}
int minD(int n2, int n3) {
int ans2 = A[n2 + n3];
if (mindp[n2][n3] > 0)
return mindp[n2][n3];
if (n3 > 1)
ans2 = min(minD(n2, n3 - 1), A[n2 + n3]);
mindp[n2][n3] = ans2;
return ans2;
}
long long calc(int n1) {
long long ans = INF;
if (dp[n1] > -1)
return dp[n1];
for (int i = 1; i < M + 1 && i < n1 + 1; i++) {
ans = min(ans, calc(n1 - i) + (long long)K +
(long long)i *
(long long)(maxD(n1 - i, i) - minD(n1 - i, i)));
}
dp[n1] = ans;
return ans;
}
int main() {
cin >> N >> M >> K;
for (int i = 1; i < N + 1; i++)
cin >> A[i];
fill(dp, dp + 20001, -1);
dp[0] = 0;
cout << calc(N) << endl;
return 0;
}
|
//============================================================================
// Name : DPTest.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int N, M, K;
int A[20001] = {};
const long long INF = 100000000000000;
long long dp[20001] = {};
int maxdp[20001][1001] = {};
int mindp[20001][1001] = {};
int maxD(int n2, int n3) {
int ans2 = A[n2 + n3];
if (maxdp[n2][n3] > 0)
return maxdp[n2][n3];
if (n3 > 1)
ans2 = max(maxD(n2, n3 - 1), A[n2 + n3]);
maxdp[n2][n3] = ans2;
return ans2;
}
int minD(int n2, int n3) {
int ans2 = A[n2 + n3];
if (mindp[n2][n3] > 0)
return mindp[n2][n3];
if (n3 > 1)
ans2 = min(minD(n2, n3 - 1), A[n2 + n3]);
mindp[n2][n3] = ans2;
return ans2;
}
long long calc(int n1) {
long long ans = INF;
if (dp[n1] > -1)
return dp[n1];
for (int i = 1; i < M + 1 && i < n1 + 1; i++) {
ans = min(ans, calc(n1 - i) + (long long)K +
(long long)i *
(long long)(maxD(n1 - i, i) - minD(n1 - i, i)));
}
dp[n1] = ans;
return ans;
}
int main() {
cin >> N >> M >> K;
for (int i = 1; i < N + 1; i++)
cin >> A[i];
fill(dp, dp + 20001, -1);
dp[0] = 0;
cout << calc(N) << endl;
return 0;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
| 1
| 415
|
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long ll;
#define MAX 20010
ll dp[MAX];
int N, M;
ll K, A[MAX];
void computer() {
rep(i, N + 1) dp[i] = LLONG_MAX;
rep(i, N) {
dp[i] = ((i - 1 >= 0) ? dp[i - 1] : 0) + K;
ll maxi = A[i];
ll mini = A[i];
for (int j = i - 1; j >= 0; --j) {
if (i - j + 1 > M)
break;
maxi = max(maxi, A[i]);
mini = min(mini, A[i]);
dp[i] = min(dp[i], ((j - 1 >= 0) ? dp[j - 1] : 0) + K +
(ll)(i - j + 1) * (maxi - mini));
}
}
cout << dp[N - 1] << endl;
}
int main() {
cin >> N >> M >> K;
rep(i, N) cin >> A[i];
computer();
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long ll;
#define MAX 20010
ll dp[MAX];
int N, M;
ll K, A[MAX];
void computer() {
rep(i, N + 1) dp[i] = LLONG_MAX;
rep(i, N) {
dp[i] = ((i - 1 >= 0) ? dp[i - 1] : 0) + K;
ll maxi = A[i];
ll mini = A[i];
for (int j = i - 1; j >= 0; --j) {
if (i - j + 1 > M)
break;
maxi = max(maxi, A[j]);
mini = min(mini, A[j]);
dp[i] = min(dp[i], ((j - 1 >= 0) ? dp[j - 1] : 0) + K +
(ll)(i - j + 1) * (maxi - mini));
}
}
cout << dp[N - 1] << endl;
}
int main() {
cin >> N >> M >> K;
rep(i, N) cin >> A[i];
computer();
return 0;
}
|
[["-", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22]]
| 1
| 283
|
#include <stdio.h>
#include <stdlib.h>
#define inf 10e18
typedef long long ll;
int main(void) {
ll i, j, k, n, m;
scanf("%lld%lld%lld", &n, &m, &k);
ll a[n], **sum, dp[n];
sum = (ll **)malloc(sizeof(ll *) * (n + 10));
for (i = 0; i < n; ++i)
sum[i] = (ll *)malloc(sizeof(ll) * (m + 10));
for (i = 0; i < n; ++i)
scanf("%lld", &a[i]);
for (i = 0; i < n; ++i)
dp[i] = inf;
dp[0] = k;
for (i = 0; i < n; ++i) {
ll min = inf, max = 0;
for (j = 0; j < n; ++j) {
if (i - j >= 0) {
if (min > a[i - j])
min = a[i - j];
if (max < a[i - j])
max = a[i - j];
sum[i][j] = k + (j + 1) * (max - min);
}
}
}
for (i = 1; i < n; ++i) {
for (j = 0; j < m; ++j) {
if (i - j > 0) {
if (dp[i] > sum[i][j] + dp[i - j - 1])
dp[i] = sum[i][j] + dp[i - j - 1];
} else if (!(i - j)) {
if (dp[i] > sum[i][j])
dp[i] = sum[i][j];
}
}
}
printf("%lld\n", dp[n - 1]);
for (i = 0; i < n; ++i)
free(sum[i]);
free(sum);
return 0;
}
|
#include <stdio.h>
#include <stdlib.h>
#define inf 10e18
typedef long long ll;
int main(void) {
ll i, j, k, n, m;
scanf("%lld%lld%lld", &n, &m, &k);
ll a[n], **sum, dp[n];
sum = (ll **)malloc(sizeof(ll *) * (n + 10));
for (i = 0; i < n; ++i)
sum[i] = (ll *)malloc(sizeof(ll) * (m + 10));
for (i = 0; i < n; ++i)
scanf("%lld", &a[i]);
for (i = 0; i < n; ++i)
dp[i] = inf;
dp[0] = k;
for (i = 0; i < n; ++i) {
ll min = inf, max = 0;
for (j = 0; j < m; ++j) {
if (i - j >= 0) {
if (min > a[i - j])
min = a[i - j];
if (max < a[i - j])
max = a[i - j];
sum[i][j] = k + (j + 1) * (max - min);
}
}
}
for (i = 1; i < n; ++i) {
for (j = 0; j < m; ++j) {
if (i - j > 0) {
if (dp[i] > sum[i][j] + dp[i - j - 1])
dp[i] = sum[i][j] + dp[i - j - 1];
} else if (!(i - j)) {
if (dp[i] > sum[i][j])
dp[i] = sum[i][j];
}
}
}
printf("%lld\n", dp[n - 1]);
for (i = 0; i < n; ++i)
free(sum[i]);
free(sum);
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
| 1
| 453
|
#include <bits/stdc++.h>
using namespace std;
int N;
int L[50];
bool check(int x, int y) {
map<int, bool> visited;
queue<int> Q;
Q.push(0);
visited[0] = true;
while (!Q.empty()) {
int p = Q.front();
Q.pop();
if (p == N)
return true;
int sum = 0;
for (int to = p + 1; to <= N; to++) {
if (p == 0 && to == N)
continue;
sum += L[to - 1];
if (sum < x || y < sum)
continue;
if (!visited[to]) {
visited[to] = true;
Q.push(to);
}
}
}
return false;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> L[i];
}
for (int i = 1; i <= 1000; i++) {
for (int j = 1; j <= 25000; j++) {
if (check(j, i + j)) {
cout << i << endl;
return 0;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N;
int L[50];
bool check(int x, int y) {
map<int, bool> visited;
queue<int> Q;
Q.push(0);
visited[0] = true;
while (!Q.empty()) {
int p = Q.front();
Q.pop();
if (p == N)
return true;
int sum = 0;
for (int to = p + 1; to <= N; to++) {
if (p == 0 && to == N)
continue;
sum += L[to - 1];
if (sum < x || y < sum)
continue;
if (!visited[to]) {
visited[to] = true;
Q.push(to);
}
}
}
return false;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> L[i];
}
for (int i = 0; i <= 1000; i++) {
for (int j = 1; j <= 25000; j++) {
if (check(j, i + j)) {
cout << i << endl;
return 0;
}
}
}
return 0;
}
|
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
| 1
| 262
|
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2147483647;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr double HDINF = 50000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
template <typename T_char> T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char> T_char TU(T_char cX) { return toupper(cX); };
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), (c).end())
#define LOWER(s) transform(ALL((s)), (s).begin(), TL<char>)
#define UPPER(s) transform(ALL((s)), (s).begin(), TU<char>)
#define FOR(i, a, b) for (LL i = (a); i < (b); i++)
#define RFOR(i, a, b) for (LL i = (a); i >= (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define INIT \
std::ios::sync_with_stdio(false); \
std::cin.tie(0)
LL n, k, l, a[202020];
int cnt[202020];
template <class V> struct BIT {
BIT() {} // [L, R)
int NV;
vector<V> bit;
BIT(int n) { init(n); }
void init(int n) {
NV = 1;
while (NV < n)
NV *= 2;
bit.resize(NV);
}
V operator[](int e) {
V s = 0;
e++;
while (e)
s += bit[e - 1], e -= e & -e;
return s;
}
void add(int e, V v) {
e++;
while (e <= NV)
bit[e - 1] += v, e += e & -e;
}
int lower_bound(V val) {
V tv = 0;
int i, ent = 0;
for (i = NV - 1; i >= 0; i--)
if (tv + bit[ent + (1 << i) - 1] <= val)
tv += bit[ent + (1 << i) - 1], ent += (1 << i);
return ent;
}
V sum(int L, int R) {
V res = 0;
if (R)
res += operator[](R - 1);
if (L)
res -= operator[](L - 1);
return res;
}
void clear() { bit.clear(); }
};
LL calc(int mid) {
BIT<LL> bit(n + 1);
bit.add(0, 1);
REP(i, min(n, k - 1)) {
if (i)
cnt[i] = cnt[i - 1];
else
cnt[i] = 0;
if (a[i] <= mid)
cnt[i]++;
}
LL ret = 0;
for (int r = k - 1; r < n; r++) {
cnt[r] = cnt[r - 1];
if (a[r] <= mid)
cnt[r]++;
bit.add(cnt[r - k + 1], 1);
if (0 <= cnt[r] - k + 1)
ret += bit.sum(0, cnt[r] - k + 1);
}
return ret;
}
int main() {
cin >> n >> k >> l;
REP(i, n) cin >> a[i];
int left = 0, right = n;
while (right - left > 1) {
int mid = (left + right) / 2;
if (l < calc(mid))
right = mid;
else
left = mid;
}
cout << left << endl;
}
|
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2147483647;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr double HDINF = 50000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
template <typename T_char> T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char> T_char TU(T_char cX) { return toupper(cX); };
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), (c).end())
#define LOWER(s) transform(ALL((s)), (s).begin(), TL<char>)
#define UPPER(s) transform(ALL((s)), (s).begin(), TU<char>)
#define FOR(i, a, b) for (LL i = (a); i < (b); i++)
#define RFOR(i, a, b) for (LL i = (a); i >= (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define INIT \
std::ios::sync_with_stdio(false); \
std::cin.tie(0)
LL n, k, l, a[202020];
LL cnt[202020];
template <class V> struct BIT {
BIT() {} // [L, R)
int NV;
vector<V> bit;
BIT(int n) { init(n); }
void init(int n) {
NV = 1;
while (NV < n)
NV *= 2;
bit.resize(NV);
}
V operator[](int e) {
V s = 0;
e++;
while (e)
s += bit[e - 1], e -= e & -e;
return s;
}
void add(int e, V v) {
e++;
while (e <= NV)
bit[e - 1] += v, e += e & -e;
}
int lower_bound(V val) {
V tv = 0;
int i, ent = 0;
for (i = NV - 1; i >= 0; i--)
if (tv + bit[ent + (1 << i) - 1] <= val)
tv += bit[ent + (1 << i) - 1], ent += (1 << i);
return ent;
}
V sum(int L, int R) {
V res = 0;
if (R)
res += operator[](R - 1);
if (L)
res -= operator[](L - 1);
return res;
}
void clear() { bit.clear(); }
};
LL calc(int mid) {
BIT<LL> bit(n + 1);
bit.add(0, 1);
REP(i, min(n, k - 1)) {
if (i)
cnt[i] = cnt[i - 1];
else
cnt[i] = 0;
if (a[i] <= mid)
cnt[i]++;
}
LL ret = 0;
for (int r = k - 1; r < n; r++) {
cnt[r] = cnt[r - 1];
if (a[r] <= mid)
cnt[r]++;
bit.add(cnt[r - k + 1], 1);
if (0 <= cnt[r] - k + 1)
ret += bit.sum(0, cnt[r] - k + 1);
}
return ret;
}
int main() {
cin >> n >> k >> l;
REP(i, n) cin >> a[i];
int left = 0, right = n;
while (right - left > 1) {
int mid = (left + right) / 2;
if (l <= calc(mid))
right = mid;
else
left = mid;
}
cout << right << endl;
}
|
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 78], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22]]
| 1
| 770
|
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;
using Point = complex<ld>;
int main() {
int N;
cin >> N;
int M;
cin >> M;
vector<int> nums(N);
for (int i = 0; i < N; ++i) {
cin >> nums[i];
}
vector<int> sas;
for (int i = 0; i < N - 1; ++i) {
sas.emplace_back(nums[i + 1] - nums[i]);
}
sort(sas.begin(), sas.end());
int ans = N;
ans += accumulate(sas.begin(), sas.begin() + N - M, 0);
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;
using Point = complex<ld>;
int main() {
int N;
cin >> N;
int M;
cin >> M;
vector<int> nums(N);
for (int i = 0; i < N; ++i) {
cin >> nums[i];
}
vector<int> sas;
for (int i = 0; i < N - 1; ++i) {
sas.emplace_back(nums[i + 1] - nums[i] - 1);
}
sort(sas.begin(), sas.end());
int ans = N;
ans += accumulate(sas.begin(), sas.begin() + N - M, 0);
cout << ans << endl;
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 178
|
#include <bits/stdc++.h>
using ll = long long;
constexpr const ll INF = LLONG_MAX / 2;
using namespace std;
int main() {
int N;
cin >> N;
vector<pair<ll, ll>> AB(N);
for (int i = 0; i < N; i++) {
cin >> AB[i].first >> AB[i].second;
}
sort(AB.begin(), AB.end());
vector<ll> S(N), D(N);
for (ll i = 0, cursum = 0; i < N; i++) {
cursum += AB[i].second;
S[i] = cursum;
D[i] = cursum - AB[i].first;
}
vector<ll> M(N);
for (ll i = N - 1, curmax = -INF, curmaxidx = -INF; i >= 0; i--) {
if (curmax < D[i]) {
curmax = D[i];
curmaxidx = i;
}
M[i] = curmaxidx;
}
ll ma = -1;
for (ll i = 0; i < N; i++) {
ll s = S[M[i]] - (i != 0 ? S[i] : 0);
ma = max(ma, s - AB[M[i]].first + AB[i].first);
}
cout << ma << endl;
return 0;
}
|
#include <bits/stdc++.h>
using ll = long long;
constexpr const ll INF = LLONG_MAX / 2;
using namespace std;
int main() {
int N;
cin >> N;
vector<pair<ll, ll>> AB(N);
for (int i = 0; i < N; i++) {
cin >> AB[i].first >> AB[i].second;
}
sort(AB.begin(), AB.end());
vector<ll> S(N), D(N);
for (ll i = 0, cursum = 0; i < N; i++) {
cursum += AB[i].second;
S[i] = cursum;
D[i] = cursum - AB[i].first;
}
vector<ll> M(N);
for (ll i = N - 1, curmax = -INF, curmaxidx = -INF; i >= 0; i--) {
if (curmax < D[i]) {
curmax = D[i];
curmaxidx = i;
}
M[i] = curmaxidx;
}
ll ma = -INF;
for (ll i = 0; i < N; i++) {
ll s = S[M[i]] - (i != 0 ? S[i - 1] : 0);
ma = max(ma, s - AB[M[i]].first + AB[i].first);
}
cout << ma << endl;
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 91, 17, 33], ["+", 8, 9, 0, 43, 49, 50, 51, 91, 28, 22], ["+", 0, 41, 64, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 41, 64, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 305
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int goal = sc.nextInt();
int day = 1;
int coin = 0;
while (coin < goal) {
coin += a;
if (day % 7 == 0)
coin += b;
day++;
}
System.out.println(day);
}
}
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int goal = sc.nextInt();
int day = 1;
int coin = 0;
while (coin < goal) {
coin += a;
if (day % 7 == 0)
coin += b;
day++;
}
System.out.println(day - 1);
}
}
|
[["+", 0, 1, 0, 492, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 492, 3, 4, 0, 16, 12, 499]]
| 3
| 108
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n;
int id = 0;
while (cin >> n && n) {
int a[15][15];
memset(a, 0, sizeof(a));
bool up = true;
int b = 0, c = 0, v = 1;
rep(i, n) {
if (i % 2 == 0) {
b = i;
c = 0;
a[b][c] = v;
v++;
while (!(b == 0 && c == i)) {
b--;
c++;
a[b][c] = v;
v++;
}
} else {
b = 0;
c = i;
a[b][c] = v;
v++;
while (!(b == i && c == 0)) {
b++;
c--;
a[b][c] = v;
v++;
}
}
}
rep(i, n - 1) {
if (i % 2 == 0) {
b = i + 1;
c = n - 1;
a[b][c] = v;
v++;
while (!(b == n - 1 && c == i + 1)) {
b++;
c--;
a[b][c] = v;
v++;
}
} else {
b = n - 1;
c = i + 1;
a[b][c] = v;
v++;
while (!(b == i + 1 && c == n - 1)) {
b--;
c++;
a[b][c] = v;
v++;
}
}
}
cout << "Case " << id + 1 << ":" << endl;
id++;
rep(i, n) {
rep(j, n) {
cout.width(3);
cout << a[i][j];
}
cout << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n;
int id = 0;
while (cin >> n && n) {
int a[15][15];
memset(a, 0, sizeof(a));
bool up = true;
int b = 0, c = 0, v = 1;
rep(i, n) {
if (i % 2 == 0) {
b = i;
c = 0;
a[b][c] = v;
v++;
while (!(b == 0 && c == i)) {
b--;
c++;
a[b][c] = v;
v++;
}
} else {
b = 0;
c = i;
a[b][c] = v;
v++;
while (!(b == i && c == 0)) {
b++;
c--;
a[b][c] = v;
v++;
}
}
}
rep(i, n - 1) {
if (i % 2 == !(n % 2)) {
b = i + 1;
c = n - 1;
a[b][c] = v;
v++;
while (!(b == n - 1 && c == i + 1)) {
b++;
c--;
a[b][c] = v;
v++;
}
} else {
b = n - 1;
c = i + 1;
a[b][c] = v;
v++;
while (!(b == i + 1 && c == n - 1)) {
b--;
c++;
a[b][c] = v;
v++;
}
}
}
cout << "Case " << id + 1 << ":" << endl;
id++;
rep(i, n) {
rep(j, n) {
cout.width(3);
cout << a[i][j];
}
cout << endl;
}
}
return 0;
}
|
[["-", 0, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 91, 17, 111], ["+", 15, 339, 51, 16, 12, 91, 28, 23, 0, 24], ["+", 51, 16, 12, 91, 28, 23, 0, 16, 31, 22], ["+", 51, 16, 12, 91, 28, 23, 0, 16, 17, 109], ["+", 51, 16, 12, 91, 28, 23, 0, 16, 12, 13], ["+", 15, 339, 51, 16, 12, 91, 28, 23, 0, 25]]
| 1
| 464
|
#include <stdio.h>
int n, a[10][10], now;
void go(int x, int y) {
a[x][y] = now;
now++;
if (x == n - 1) {
if (y == n - 1)
return;
if ((x + y) % 2 == 1) {
go(x, y + 1);
return;
}
}
if (y == n - 1) {
if ((x + y) % 2 == 0) {
go(x + 1, y);
return;
}
}
if (y == 0) {
if (x % 2 == 1) {
go(x + 1, y);
return;
}
}
if (x == 0) {
if (y % 2 == 0) {
go(x, y + 1);
return;
}
}
if ((x + y) % 2 == 1) {
go(x + 1, y - 1);
return;
}
go(x - 1, y + 1);
return;
}
int main() {
int i = 1;
while (1) {
scanf("%d", &n);
if (n == 0)
return 0;
now = 1;
go(0, 0);
printf("Case %d:", i);
i++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d", a[i][j]);
}
printf("\n");
}
}
}
|
#include <stdio.h>
int n, a[10][10], now;
void go(int x, int y) {
a[x][y] = now;
now++;
if (x == n - 1) {
if (y == n - 1)
return;
if ((x + y) % 2 == 1) {
go(x, y + 1);
return;
}
}
if (y == n - 1) {
if ((x + y) % 2 == 0) {
go(x + 1, y);
return;
}
}
if (y == 0) {
if (x % 2 == 1) {
go(x + 1, y);
return;
}
}
if (x == 0) {
if (y % 2 == 0) {
go(x, y + 1);
return;
}
}
if ((x + y) % 2 == 1) {
go(x + 1, y - 1);
return;
}
go(x - 1, y + 1);
return;
}
int main() {
int i = 1;
while (1) {
scanf("%d", &n);
if (n == 0)
return 0;
now = 1;
go(0, 0);
printf("Case %d:\n", i);
i++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d", a[i][j]);
}
printf("\n");
}
}
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
| 1
| 330
|
#include <bits/stdc++.h>
using namespace std;
int A[10][10];
int main() {
int N, c = 1;
while (cin >> N, N) {
int x, y, id = 1;
N--;
for (int i = 0; i <= N * 2; ++i) {
if (i % 2 == 0) {
y = min(i, N);
x = i - y;
while (y >= 0 && x <= N) {
A[x][y] = id++;
y--;
x++;
}
} else {
x = min(i, N);
y = i - x;
while (x >= 0 && y <= N) {
A[x][y] = id++;
y++;
x--;
}
}
}
cout << "Case " << c++ << ":\n";
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= N; ++j) {
cout << setw(3) << A[i][j];
}
cout << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int A[10][10];
int main() {
int N, c = 1;
while (cin >> N, N) {
int x, y, id = 1;
N--;
for (int i = 0; i <= N * 2; ++i) {
if (i % 2 == 1) {
y = min(i, N);
x = i - y;
while (y >= 0 && x <= N) {
A[x][y] = id++;
y--;
x++;
}
} else {
x = min(i, N);
y = i - x;
while (x >= 0 && y <= N) {
A[x][y] = id++;
y++;
x--;
}
}
}
cout << "Case " << c++ << ":\n";
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= N; ++j) {
cout << setw(3) << A[i][j];
}
cout << '\n';
}
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
| 1
| 236
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int dy[] = {0, 1, 1, -1};
const int dx[] = {1, -1, 0, 1};
enum Situation {
RIGHT,
LEFTDOWN,
DOWN,
RIGHTUP,
};
int main() {
int n;
int cs = 0;
while (cin >> n && n != 0) {
cout << "Case " << cs << ":" << endl;
cs++;
int field[11][11];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
field[i][j] = -1;
int cx = 0;
int cy = 0;
Situation s = RIGHTUP;
field[0][0] = 1;
int cnt = 2;
while (1) {
if (cy == n - 1 && cx == n - 1)
break;
if (s == RIGHT) {
int ny = cy + dy[LEFTDOWN];
int nx = cx + dx[LEFTDOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = LEFTDOWN;
} else {
ny = cy + dy[RIGHTUP];
nx = cx + dx[RIGHTUP];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHTUP;
}
}
} else if (s == LEFTDOWN) {
int ny = cy + dy[LEFTDOWN];
int nx = cx + dx[LEFTDOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = LEFTDOWN;
} else {
// ¶ºÖ¢¯È¢Èç
ny = cy + dy[DOWN];
nx = cx + dx[DOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = DOWN;
} else {
// ºÖࢯȢÈç
ny = cy + dy[RIGHT];
nx = cx + dx[RIGHT];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHT;
}
}
}
} else if (s == DOWN) {
int ny = cy + dy[RIGHTUP];
int nx = cx + dx[RIGHTUP];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHTUP;
} else {
int ny = cy + dy[LEFTDOWN];
int nx = cx + dx[LEFTDOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = LEFTDOWN;
}
}
} else if (s == RIGHTUP) {
int ny = cy + dy[RIGHTUP];
int nx = cx + dx[RIGHTUP];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHTUP;
} else {
int ny = cy + dy[RIGHT];
int nx = cx + dx[RIGHT];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHT;
} else {
ny = cy + dy[DOWN];
nx = cx + dx[DOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = DOWN;
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// cout<<field[i][j]<<" ";
printf("%3d", field[i][j]);
}
cout << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int dy[] = {0, 1, 1, -1};
const int dx[] = {1, -1, 0, 1};
enum Situation {
RIGHT,
LEFTDOWN,
DOWN,
RIGHTUP,
};
int main() {
int n;
int cs = 1;
while (cin >> n && n != 0) {
cout << "Case " << cs << ":" << endl;
cs++;
int field[11][11];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
field[i][j] = -1;
int cx = 0;
int cy = 0;
Situation s = RIGHTUP;
field[0][0] = 1;
int cnt = 2;
while (1) {
if (cy == n - 1 && cx == n - 1)
break;
if (s == RIGHT) {
int ny = cy + dy[LEFTDOWN];
int nx = cx + dx[LEFTDOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = LEFTDOWN;
} else {
ny = cy + dy[RIGHTUP];
nx = cx + dx[RIGHTUP];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHTUP;
}
}
} else if (s == LEFTDOWN) {
int ny = cy + dy[LEFTDOWN];
int nx = cx + dx[LEFTDOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = LEFTDOWN;
} else {
// ¶ºÖ¢¯È¢Èç
ny = cy + dy[DOWN];
nx = cx + dx[DOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = DOWN;
} else {
// ºÖࢯȢÈç
ny = cy + dy[RIGHT];
nx = cx + dx[RIGHT];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHT;
}
}
}
} else if (s == DOWN) {
int ny = cy + dy[RIGHTUP];
int nx = cx + dx[RIGHTUP];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHTUP;
} else {
int ny = cy + dy[LEFTDOWN];
int nx = cx + dx[LEFTDOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = LEFTDOWN;
}
}
} else if (s == RIGHTUP) {
int ny = cy + dy[RIGHTUP];
int nx = cx + dx[RIGHTUP];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHTUP;
} else {
int ny = cy + dy[RIGHT];
int nx = cx + dx[RIGHT];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = RIGHT;
} else {
ny = cy + dy[DOWN];
nx = cx + dx[DOWN];
if (ny >= 0 && nx >= 0 && ny < n && nx < n && field[ny][nx] == -1) {
cy = ny;
cx = nx;
field[ny][nx] = cnt;
cnt++;
s = DOWN;
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d", field[i][j]);
}
cout << endl;
}
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 1,051
|
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const string mas[10][10] = {
/*気合い!! n < 10 だし!*/
{" 1"},
{" 1 2", " 3 4"},
{" 1 2 6", " 3 5 7", " 4 8 9"},
{" 1 2 6 7", " 3 5 8 13", " 4 9 12 14", " 10 11 15 16"},
{" 1 2 6 7 15", " 3 5 8 14 16", " 4 9 13 17 22", " 10 12 18 21 23",
" 11 19 20 24 25"},
{" 1 2 6 7 15 16", " 3 5 8 14 17 26", " 4 9 13 18 25 27",
" 10 12 19 24 28 33", " 11 20 23 29 32 34", " 21 22 30 31 35 36"},
{" 1 2 6 7 15 16 28", " 3 5 8 14 17 27 29", " 4 9 13 18 26 30 39",
" 10 12 19 25 31 38 40", " 11 20 24 32 37 41 46", " 21 23 33 36 42 45 47",
" 22 34 35 43 44 48 49"},
{" 1 2 6 7 15 16 28 29", " 3 5 8 14 17 27 30 43",
" 4 9 13 18 26 31 42 44", " 10 12 19 25 32 41 45 54",
" 11 20 24 33 40 36 53 55", " 21 23 34 39 47 52 56 61",
" 22 35 38 48 51 57 60 62", " 36 37 49 50 58 59 63 64"},
{" 1 2 6 7 15 16 28 29 45", " 3 5 8 14 17 27 30 44 46",
" 4 9 13 18 26 31 43 47 60", " 10 12 19 25 32 42 48 59 61",
" 11 20 24 33 41 49 58 62 71", " 21 23 34 40 50 57 63 70 72",
" 22 35 39 51 56 64 69 73 78", " 36 38 52 55 65 68 74 77 79",
" 37 53 54 66 67 75 76 80 81"},
};
int main() {
int n;
int q = 1;
while (cin >> n, n) {
cout << "Case " << q++ << ":" << endl;
for (int i = 0; i < n; i++) {
cout << mas[n - 1][i] << endl;
}
}
}
|
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const string mas[10][10] = {
/*気合い!! n < 10 だし!*/
{" 1"},
{" 1 2", " 3 4"},
{" 1 2 6", " 3 5 7", " 4 8 9"},
{" 1 2 6 7", " 3 5 8 13", " 4 9 12 14", " 10 11 15 16"},
{" 1 2 6 7 15", " 3 5 8 14 16", " 4 9 13 17 22", " 10 12 18 21 23",
" 11 19 20 24 25"},
{" 1 2 6 7 15 16", " 3 5 8 14 17 26", " 4 9 13 18 25 27",
" 10 12 19 24 28 33", " 11 20 23 29 32 34", " 21 22 30 31 35 36"},
{" 1 2 6 7 15 16 28", " 3 5 8 14 17 27 29", " 4 9 13 18 26 30 39",
" 10 12 19 25 31 38 40", " 11 20 24 32 37 41 46", " 21 23 33 36 42 45 47",
" 22 34 35 43 44 48 49"},
{" 1 2 6 7 15 16 28 29", " 3 5 8 14 17 27 30 43",
" 4 9 13 18 26 31 42 44", " 10 12 19 25 32 41 45 54",
" 11 20 24 33 40 46 53 55", " 21 23 34 39 47 52 56 61",
" 22 35 38 48 51 57 60 62", " 36 37 49 50 58 59 63 64"},
{" 1 2 6 7 15 16 28 29 45", " 3 5 8 14 17 27 30 44 46",
" 4 9 13 18 26 31 43 47 60", " 10 12 19 25 32 42 48 59 61",
" 11 20 24 33 41 49 58 62 71", " 21 23 34 40 50 57 63 70 72",
" 22 35 39 51 56 64 69 73 78", " 36 38 52 55 65 68 74 77 79",
" 37 53 54 66 67 75 76 80 81"},
};
int main() {
int n;
int q = 1;
while (cin >> n, n) {
cout << "Case " << q++ << ":" << endl;
for (int i = 0; i < n; i++) {
cout << mas[n - 1][i] << endl;
}
}
}
|
[["-", 49, 50, 51, 83, 0, 83, 0, 5, 0, 6], ["+", 49, 50, 51, 83, 0, 83, 0, 5, 0, 6]]
| 1
| 291
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
bool out(int x, int y, int n) { return (x < 0 || x >= n || y < 0 || y >= n); }
int main() {
int n;
int cnt = 1;
while (cin >> n && n) {
vector<vector<int>> field(n, vector<int>(n, 0));
int dx[] = {-1, 0, 1, 1};
int dy[] = {1, 1, -1, 0};
int cx = 0, cy = 0;
int dir = 2;
int n_order = 1;
while (1) {
field[cy][cx] = n_order++;
if (cy == n - 1 && cx == n - 1)
break;
int cdir = dir;
if (out(cx + dx[dir], cy + dy[dir], n)) {
if (!out(cx + dx[(dir + 1) % 4], cy + dy[(dir + 1) % 4], n)) {
cdir = (dir + 1) % 4;
} else {
cdir = (dir + 3) % 4;
}
dir ^= 2;
}
cx += dx[cdir];
cy += dy[cdir];
}
cout << "Case " << cnt << ":" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
printf("%3d", field[i][j]);
}
puts("");
}
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
bool out(int x, int y, int n) { return (x < 0 || x >= n || y < 0 || y >= n); }
int main() {
int n;
int cnt = 1;
while (cin >> n && n) {
vector<vector<int>> field(n, vector<int>(n, 0));
int dx[] = {-1, 0, 1, 1};
int dy[] = {1, 1, -1, 0};
int cx = 0, cy = 0;
int dir = 2;
int n_order = 1;
while (1) {
field[cy][cx] = n_order++;
if (cy == n - 1 && cx == n - 1)
break;
int cdir = dir;
if (out(cx + dx[dir], cy + dy[dir], n)) {
if (!out(cx + dx[(dir + 1) % 4], cy + dy[(dir + 1) % 4], n)) {
cdir = (dir + 1) % 4;
} else {
cdir = (dir + 3) % 4;
}
dir ^= 2;
}
cx += dx[cdir];
cy += dy[cdir];
}
cout << "Case " << cnt++ << ":" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
printf("%3d", field[i][j]);
}
puts("");
}
}
return 0;
}
|
[["+", 0, 16, 31, 16, 31, 16, 12, 27, 17, 29]]
| 1
| 352
|
class Map
def initialize(x,y,number,move)
@x = x
@y = y
@move = move
@number = number
end
attr_accessor :x,:y,:move,:number
def +(map)
x = self.x + map.x
y = self.y + map.y
m = self.move
n = self.number + 1
tmp = Map.new(x,y,n,m)
end
end
@b = []
@b << Map.new(1,0, 1,0) #sita
@b << Map.new(0,1, 1,1) #migi
@b << Map.new(1,-1,1,2) #hidarisita
@b << Map.new(-1,1,1,3) #migiue
def work(map,m)
n=m-1
if map.move==3 && map.x!=0 && map.y!=n then
return map+@b[3]
elsif map.move==2 && map.y!=0 && map.x!=n then
return map+@b[2]
elsif (map.move==3&&map.x==0)||(map.move==2&&map.x==n) then
if map.move==3 then
move = 2
else
move = 3
end
temp = map + @b[1]
temp.move = move
if map.y==n then
temp.move = 2
temp.x = 1
temp.y = n
return temp
else
return temp
end
else
if map.move==3 then
move = 2
else
move = 3
end
temp = map + @b[0]
temp.move = move
return temp
end
end
$m = Map.new(0,0,1,2)
$n = 3
def w(m,n)
return work( m, n )
end
$COU = 0
def printmap(n)
$COU = $COU + 1
puts "Case %d" % $COU
c=[$m]
i=1
while i<n*n
c << w(c[i-1],n)
i = i+1
end
i=0
while i < n
for mapp in c do
if mapp.y==i then
print "%3d" % mapp.number
end
end
puts
i=i+1
end
end
while true
a = gets.to_i
if a==0 then
break
end
printmap(a)
end
|
class Map
def initialize(x,y,number,move)
@x = x
@y = y
@move = move
@number = number
end
attr_accessor :x,:y,:move,:number
def +(map)
x = self.x + map.x
y = self.y + map.y
m = self.move
n = self.number + 1
tmp = Map.new(x,y,n,m)
end
end
@b = []
@b << Map.new(1,0, 1,0) #sita
@b << Map.new(0,1, 1,1) #migi
@b << Map.new(1,-1,1,2) #hidarisita
@b << Map.new(-1,1,1,3) #migiue
def work(map,m)
n=m-1
if map.move==3 && map.x!=0 && map.y!=n then
return map+@b[3]
elsif map.move==2 && map.y!=0 && map.x!=n then
return map+@b[2]
elsif (map.move==3&&map.x==0)||(map.move==2&&map.x==n) then
if map.move==3 then
move = 2
else
move = 3
end
temp = map + @b[1]
temp.move = move
if map.y==n then
temp.move = 2
temp.x = 1
temp.y = n
return temp
else
return temp
end
else
if map.move==3 then
move = 2
else
move = 3
end
temp = map + @b[0]
temp.move = move
return temp
end
end
$m = Map.new(0,0,1,2)
$n = 3
def w(m,n)
return work( m, n )
end
$COU = 0
def printmap(n)
$COU = $COU + 1
puts "Case %d:" % $COU
c=[$m]
i=1
while i<n*n
c << w(c[i-1],n)
i = i+1
end
i=0
while i < n
for mapp in c do
if mapp.y==i then
print "%3d" % mapp.number
end
end
puts
i=i+1
end
end
while true
a = gets.to_i
if a==0 then
break
end
printmap(a)
end
|
[["-", 0, 652, 3, 4, 0, 738, 31, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 738, 31, 557, 0, 6]]
| 4
| 473
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// int[] array = new int[1000001];
while (true) {
final int N = sc.nextInt();
if (N == 0) {
break;
}
System.gc();
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int i = 0; i < N; i++) {
final int A = sc.nextInt();
int value = 0;
if (map.containsKey(A)) {
value = map.get(A);
}
value++;
map.put(A, value);
}
boolean flag = true;
for (Entry<Integer, Integer> entry : map.entrySet()) {
// System.out.println(entry.getValue());
if (entry.getValue() > N / 2) {
System.out.println(entry.getKey());
flag = true;
break;
}
}
if (!flag) {
System.out.println("NO COLOR");
}
}
}
public static class Scanner {
private BufferedReader br;
private StringTokenizer tok;
public Scanner(InputStream is) throws IOException {
br = new BufferedReader(new InputStreamReader(is));
getLine();
}
private void getLine() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(br.readLine());
}
}
private boolean hasNext() { return tok.hasMoreTokens(); }
public String next() throws IOException {
if (hasNext()) {
return tok.nextToken();
} else {
getLine();
return tok.nextToken();
}
}
public int nextInt() throws IOException {
if (hasNext()) {
return Integer.parseInt(tok.nextToken());
} else {
getLine();
return Integer.parseInt(tok.nextToken());
}
}
public long nextLong() throws IOException {
if (hasNext()) {
return Long.parseLong(tok.nextToken());
} else {
getLine();
return Long.parseLong(tok.nextToken());
}
}
public double nextDouble() throws IOException {
if (hasNext()) {
return Double.parseDouble(tok.nextToken());
} else {
getLine();
return Double.parseDouble(tok.nextToken());
}
}
}
}
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// int[] array = new int[1000001];
while (true) {
final int N = sc.nextInt();
if (N == 0) {
break;
}
System.gc();
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int i = 0; i < N; i++) {
final int A = sc.nextInt();
int value = 0;
if (map.containsKey(A)) {
value = map.get(A);
}
value++;
map.put(A, value);
}
boolean flag = false;
for (Entry<Integer, Integer> entry : map.entrySet()) {
// System.out.println(entry.getValue());
if (entry.getValue() > N / 2) {
System.out.println(entry.getKey());
flag = true;
break;
}
}
if (!flag) {
System.out.println("NO COLOR");
}
}
}
public static class Scanner {
private BufferedReader br;
private StringTokenizer tok;
public Scanner(InputStream is) throws IOException {
br = new BufferedReader(new InputStreamReader(is));
getLine();
}
private void getLine() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(br.readLine());
}
}
private boolean hasNext() { return tok.hasMoreTokens(); }
public String next() throws IOException {
if (hasNext()) {
return tok.nextToken();
} else {
getLine();
return tok.nextToken();
}
}
public int nextInt() throws IOException {
if (hasNext()) {
return Integer.parseInt(tok.nextToken());
} else {
getLine();
return Integer.parseInt(tok.nextToken());
}
}
public long nextLong() throws IOException {
if (hasNext()) {
return Long.parseLong(tok.nextToken());
} else {
getLine();
return Long.parseLong(tok.nextToken());
}
}
public double nextDouble() throws IOException {
if (hasNext()) {
return Double.parseDouble(tok.nextToken());
} else {
getLine();
return Double.parseDouble(tok.nextToken());
}
}
}
}
|
[["-", 0, 52, 8, 196, 0, 503, 49, 200, 51, 146], ["+", 0, 52, 8, 196, 0, 503, 49, 200, 51, 147]]
| 3
| 549
|
/* template.cpp [[[ */
#include <bits/stdc++.h>
using namespace std;
#define get_macro(a, b, c, d, name, ...) name
#define rep(...) get_macro(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) \
get_macro(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define rep1(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, s) for (ll i = (a); i < (ll)(b); i += (ll)s)
#define rrep1(n) rrep2(i_, n)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep3(i, a, b) rrep4(i, a, b, 1)
#define rrep4(i, a, b, s) for (ll i = (ll)(b)-1; i >= (ll)(a); i -= (ll)s)
#define each(x, c) for (auto &&x : c)
#define fs first
#define sc second
#define all(c) begin(c), end(c)
using ui = unsigned;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
const ll mod = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[]{0, -1, 0, 1, -1, -1, 1, 1};
template <class T, class U> void chmin(T &x, const U &y) { x = min<T>(x, y); }
template <class T, class U> void chmax(T &x, const U &y) { x = max<T>(x, y); }
struct prepare_ {
prepare_() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
}
} prepare__;
/* ]]] */
int main() {
int n;
while (cin >> n, n) {
map<int, int> s;
int res = -1;
rep(i, n) {
int x;
cin >> x;
if (++s[x] > n / 2)
res = x;
}
cout << (res == -1 ? "NO COLOR\n" : to_string(res)) << endl;
}
}
|
/* template.cpp [[[ */
#include <bits/stdc++.h>
using namespace std;
#define get_macro(a, b, c, d, name, ...) name
#define rep(...) get_macro(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) \
get_macro(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define rep1(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, s) for (ll i = (a); i < (ll)(b); i += (ll)s)
#define rrep1(n) rrep2(i_, n)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep3(i, a, b) rrep4(i, a, b, 1)
#define rrep4(i, a, b, s) for (ll i = (ll)(b)-1; i >= (ll)(a); i -= (ll)s)
#define each(x, c) for (auto &&x : c)
#define fs first
#define sc second
#define all(c) begin(c), end(c)
using ui = unsigned;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
const ll mod = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[]{0, -1, 0, 1, -1, -1, 1, 1};
template <class T, class U> void chmin(T &x, const U &y) { x = min<T>(x, y); }
template <class T, class U> void chmax(T &x, const U &y) { x = max<T>(x, y); }
struct prepare_ {
prepare_() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
}
} prepare__;
/* ]]] */
int main() {
int n;
while (cin >> n, n) {
map<int, int> s;
int res = -1;
rep(i, n) {
int x;
cin >> x;
if (++s[x] > n / 2)
res = x;
}
cout << (res == -1 ? "NO COLOR" : to_string(res)) << endl;
}
}
|
[["-", 31, 16, 12, 23, 0, 41, 64, 5, 0, 44]]
| 1
| 416
|
#include <bits/stdc++.h>
using namespace std;
map<int, int> M;
int main() {
int n;
while (cin >> n, n) {
int ans = -1;
for (int i = 0, a; i < n; i++) {
cin >> a;
M[a]++;
if (M[a] > n / 2)
ans = a;
}
if (ans == -1)
cout << "NO COLOR" << endl;
else
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<int, int> M;
int main() {
int n;
while (cin >> n, n) {
int ans = -1;
M.clear();
for (int i = 0, a; i < n; i++) {
cin >> a;
M[a]++;
if (M[a] > n / 2)
ans = a;
}
if (ans == -1)
cout << "NO COLOR" << endl;
else
cout << ans << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
| 1
| 105
|
#include <algorithm>
#include <iostream>
using namespace std;
int A[1000000];
int main() {
int n;
while (1) {
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
sort(A, A + n);
int cnt;
int maxi = -1;
int maxA;
for (int i = 0; i < n;) {
int a = A[i];
cnt = 1;
for (i++; i < n && A[i] == a; i++) {
cnt++;
}
if (cnt > maxi) {
maxi = cnt;
maxA = a;
}
}
if (cnt > n / 2)
cout << maxA << endl;
else
cout << "NO COLOR" << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
int A[1000000];
int main() {
int n;
while (1) {
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
sort(A, A + n);
int cnt;
int maxi = -1;
int maxA;
for (int i = 0; i < n;) {
int a = A[i];
cnt = 1;
for (i++; i < n && A[i] == a; i++) {
cnt++;
}
if (cnt > maxi) {
maxi = cnt;
maxA = a;
}
}
if (maxi > n / 2)
cout << maxA << endl;
else
cout << "NO COLOR" << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
| 1
| 176
|
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main() {
int tmp;
int n;
while (cin >> n && n != 0) {
map<int, int> s;
int maxNum;
int maxCnt = 0;
// fill(s.begin(),s.end(),0);
for (int i = 0; i < n; i++) {
cin >> tmp;
s[tmp]++;
if (maxCnt < s[tmp]) {
maxCnt = max(maxCnt, s[tmp]);
maxNum = tmp;
}
}
if (maxNum > n / 2) {
cout << maxNum << endl;
} else
cout << "NO COLOR" << endl;
}
return 0;
}
|
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main() {
int tmp;
int n;
while (cin >> n && n != 0) {
map<int, int> s;
int maxNum;
int maxCnt = 0;
// fill(s.begin(),s.end(),0);
for (int i = 0; i < n; i++) {
cin >> tmp;
s[tmp]++;
if (maxCnt < s[tmp]) {
maxCnt = max(maxCnt, s[tmp]);
maxNum = tmp;
}
}
if (maxCnt > n / 2) {
cout << maxNum << endl;
} else
cout << "NO COLOR" << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
| 1
| 134
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
while (scanf("%d", &n), n) {
map<int, int> table;
int t, maxi = 0;
for (int x = 0; x < n; ++x) {
int j;
scanf("%d", &j);
table[j]++;
if (table[j] > maxi) {
maxi = table[j];
t = j;
}
}
if (t > n / 2) {
cout << t << endl;
} else {
cout << "NO COLOR" << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
while (scanf("%d", &n), n) {
map<int, int> table;
int t, maxi = 0;
for (int x = 0; x < n; ++x) {
int j;
scanf("%d", &j);
table[j]++;
if (table[j] > maxi) {
maxi = table[j];
t = j;
}
}
if (maxi > (n / 2)) {
cout << t << endl;
} else {
cout << "NO COLOR" << endl;
}
}
return 0;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 23, 0, 24], ["+", 0, 57, 15, 339, 51, 16, 12, 23, 0, 25]]
| 1
| 161
|
while True:
num=int(input())
if num==0:
break
stars=sorted(input().split())
print(stars)
if stars.count(stars[num//2]) > num/2 :
print(stars[num//2])
else:
print("NO COLOR")
|
while True:
num=int(input())
if num==0:
break
stars=sorted(input().split())
if stars.count(stars[num//2]) > num/2 :
print(stars[num//2])
else:
print("NO COLOR")
|
[["-", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 67
|
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return 0;
return gcd(b, a % b);
}
int main(void) {
int a, b;
while (scanf("%d %d", &a, &b) != EOF) {
printf("%d\n", gcd(a, b));
}
return 0;
}
|
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main(void) {
int a, b;
while (scanf("%d %d", &a, &b) != EOF) {
printf("%d\n", gcd(a, b));
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 57, 64, 37, 0, 13], ["+", 0, 14, 8, 9, 0, 57, 64, 37, 0, 22]]
| 0
| 81
|
#include <stdio.h>
long gcd(long a, long b) {
if (a % b == 0) {
return b;
}
gcd(b, a % b);
}
int main(void) {
long a, b, c, g;
while (scanf("%ld %ld", &a, &b) != EOF) {
if (a < b) {
c = a;
a = b;
b = c;
}
g = gcd(a, b);
printf("%ld", g);
}
return 0;
}
|
#include <stdio.h>
long gcd(long a, long b) {
if (a % b == 0) {
return b;
}
gcd(b, a % b);
}
int main(void) {
long a, b, c, g;
while (scanf("%ld %ld", &a, &b) != EOF) {
if (a < b) {
c = a;
a = b;
b = c;
}
g = gcd(a, b);
printf("%ld\n", g);
}
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
| 0
| 111
|
#include <stdio.h>
main() {
int a, b;
int i, temp;
while (1) {
if (scanf("%d %d", &a, &b) == EOF)
break;
if (a >= b) {
for (i = 2; i <= b; i++) {
if (a % i == 0 && b % i == 0)
temp = i;
}
printf("%d\n", temp);
} else {
for (i = 2; i <= a; i++) {
if (a % i == 0 && b % i == 0)
temp = i;
}
printf("%d\n", temp);
}
}
return 0;
}
|
#include <stdio.h>
main() {
int a, b;
int i, temp;
while (1) {
if (scanf("%d %d", &a, &b) == EOF)
break;
if (a >= b) {
for (i = 1; i <= b; i++) {
if (a % i == 0 && b % i == 0)
temp = i;
}
printf("%d\n", temp);
} else {
for (i = 1; i <= a; i++) {
if (a % i == 0 && b % i == 0)
temp = i;
}
printf("%d\n", temp);
}
}
return 0;
}
|
[["-", 0, 57, 64, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 57, 64, 9, 0, 7, 10, 11, 12, 13], ["-", 75, 76, 0, 9, 0, 7, 10, 11, 12, 13], ["+", 75, 76, 0, 9, 0, 7, 10, 11, 12, 13]]
| 0
| 143
|
#include <stdio.h>
#include <string.h>
long int gcd(long int a, long int b) { return (b == 0 ? a : gcd(b, a % b)); }
int main() {
long long a, b;
while (scanf("%lld %lld", &a, &b) != EOF) {
long long x = gcd(a, b);
printf("%d", x);
}
return 0;
}
|
#include <stdio.h>
#include <string.h>
long int gcd(long int a, long int b) { return (b == 0 ? a : gcd(b, a % b)); }
int main() {
long long a, b;
while (scanf("%lld %lld", &a, &b) != EOF) {
long long x = gcd(a, b);
printf("%d\n", x);
}
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
| 1
| 90
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int pgcd(int a, int b) { return b == 0 ? a : pgcd(b % a, a); }
int main() {
int a, b;
while (cin >> a >> b) {
cout << pgcd(a, b) << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int pgcd(int a, int b) { return a == 0 ? b : pgcd(b % a, a); }
int main() {
int a, b;
while (cin >> a >> b) {
cout << pgcd(a, b) << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 37, 0, 41, 15, 16, 31, 22], ["+", 8, 9, 0, 37, 0, 41, 15, 16, 31, 22], ["-", 0, 14, 8, 9, 0, 37, 0, 41, 64, 22], ["+", 0, 14, 8, 9, 0, 37, 0, 41, 64, 22]]
| 1
| 76
|
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
int a, b;
cin >> a >> b;
cout << __gcd(a, b) << endl;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
int a, b;
while (cin >> a >> b)
cout << __gcd(a, b) << endl;
}
|
[["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 24], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25]]
| 1
| 37
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return 0;
if (data[y] < data[x])
swap(x, y);
data[x] += data[y], data[y] = x;
return 1;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
class KRK { // c++11
public:
struct edge {
int from, to, cost;
};
vector<edge> G;
int n, m;
KRK(int size1, int size2) {
n = size1;
m = size2;
}
void add_edge(int a, int b, int c) {
edge e = {a, b, c};
G.pb(e);
}
int krk() {
int sum = 0;
sort(all(G), [](edge a, edge b) { return a.cost < b.cost; });
UnionFind uf(n + m);
rep(i, G.size()) {
edge e = G[i];
bool h1 = false, h2 = false;
rep(j, n) {
if (uf.findSet(e.from, m + j))
h1 = true;
if (uf.findSet(e.to, m + j))
h2 = true;
}
if (h1 + h2 != 2 && !uf.findSet(e.from, e.to)) {
sum += e.cost;
uf.unionSet(e.from, e.to);
}
}
return sum;
}
};
int main() {
int n, m;
while (cin >> n >> m) {
KRK krk(n, m);
rep(i, n) rep(j, m) {
int a;
cin >> a;
if (a)
krk.add_edge(j, m + i, a);
}
rep(i, m - 1) rep(j, m - i - 1) {
int a;
cin >> a;
if (a)
krk.add_edge(i, i + j + 1, a);
}
cout << krk.krk() << endl;
}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return 0;
if (data[y] < data[x])
swap(x, y);
data[x] += data[y], data[y] = x;
return 1;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
class KRK { // c++11
public:
struct edge {
int from, to, cost;
};
vector<edge> G;
int n, m;
KRK(int size1, int size2) {
n = size1;
m = size2;
}
void add_edge(int a, int b, int c) {
edge e = {a, b, c};
G.pb(e);
}
int krk() {
int sum = 0;
sort(all(G), [](edge a, edge b) { return a.cost < b.cost; });
UnionFind uf(n + m);
rep(i, G.size()) {
edge e = G[i];
bool h1 = false, h2 = false;
rep(j, n) {
if (uf.findSet(e.from, m + j))
h1 = true;
if (uf.findSet(e.to, m + j))
h2 = true;
}
if (h1 + h2 != 2 && !uf.findSet(e.from, e.to)) {
sum += e.cost;
uf.unionSet(e.from, e.to);
}
}
return sum;
}
};
int main() {
int n, m;
while (cin >> n >> m, n || m) {
KRK krk(n, m);
rep(i, n) rep(j, m) {
int a;
cin >> a;
if (a)
krk.add_edge(j, m + i, a);
}
rep(i, m - 1) rep(j, m - i - 1) {
int a;
cin >> a;
if (a)
krk.add_edge(i, i + j + 1, a);
}
cout << krk.krk() << endl;
}
}
|
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 17, 106], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 12, 22]]
| 1
| 708
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
e.src != f.src ? e.src < f.src
: e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
//
const int MAX_N = 51;
int SP[MAX_N][MAX_N];
int DI[MAX_N][MAX_N];
typedef priority_queue<Edge, vector<Edge>> QUEUE;
int main() {
int s, d;
while (cin >> s >> d && s && d) {
memset(SP, 0, sizeof(SP));
memset(DI, 0, sizeof(DI));
for (int i = 0; i < s; ++i) {
for (int j = 0; j < d; ++j) {
cin >> SP[i][j];
}
}
for (int i = 0; i < d - 1; ++i) {
for (int j = 0; j < d - i - 1; ++j) {
cin >> DI[j][i + j + 1];
DI[i + j + 1][j] = DI[j][i + j + 1];
}
}
int res = 0;
QUEUE Q;
for (int i = 0; i < s; ++i) {
for (int j = 0; j < d; ++j) {
if (SP[i][j] == 0)
continue;
Q.push(Edge(i, j + s, SP[i][j]));
}
}
for (int i = 0; i < d; ++i) {
for (int j = i + 1; j < d; ++j) {
if (DI[i][j] == 0)
continue;
Q.push(Edge(i + s, j + s, DI[i][j]));
}
}
UnionFind uf(s + d);
while (!Q.empty()) {
Edge e = Q.top();
Q.pop();
int a = e.src;
int b = e.dst;
if (uf.findSet(a, b))
continue;
bool f1 = false, f2 = false;
for (int i = 0; i < s; ++i) {
if (uf.findSet(i, a))
f1 = true;
if (uf.findSet(i, b))
f2 = true;
}
if (f1 && f2)
continue;
int w = e.weight;
res += w;
uf.unionSet(a, b);
}
cout << res << endl;
}
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
e.src != f.src ? e.src < f.src
: e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
//
const int MAX_N = 51;
int SP[MAX_N][MAX_N];
int DI[MAX_N][MAX_N];
typedef priority_queue<Edge, vector<Edge>> QUEUE;
int main() {
int s, d;
while (cin >> s >> d && s && d) {
memset(SP, 0, sizeof(SP));
memset(DI, 0, sizeof(DI));
for (int i = 0; i < s; ++i) {
for (int j = 0; j < d; ++j) {
cin >> SP[i][j];
}
}
for (int i = 0; i < d - 1; ++i) {
for (int j = 0; j < d - i - 1; ++j) {
cin >> DI[i][i + j + 1];
DI[i + j + 1][i] = DI[i][i + j + 1];
}
}
int res = 0;
QUEUE Q;
for (int i = 0; i < s; ++i) {
for (int j = 0; j < d; ++j) {
if (SP[i][j] == 0)
continue;
Q.push(Edge(i, j + s, SP[i][j]));
}
}
for (int i = 0; i < d; ++i) {
for (int j = i + 1; j < d; ++j) {
if (DI[i][j] == 0)
continue;
Q.push(Edge(i + s, j + s, DI[i][j]));
}
}
UnionFind uf(s + d);
while (!Q.empty()) {
Edge e = Q.top();
Q.pop();
int a = e.src;
int b = e.dst;
if (uf.findSet(a, b))
continue;
bool f1 = false, f2 = false;
for (int i = 0; i < s; ++i) {
if (uf.findSet(i, a))
f1 = true;
if (uf.findSet(i, b))
f2 = true;
}
if (f1 && f2)
continue;
int w = e.weight;
res += w;
uf.unionSet(a, b);
}
cout << res << endl;
}
return 0;
}
|
[["-", 0, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["-", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22]]
| 1
| 861
|
// 34
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
for (int s, dd; cin >> s >> dd, s | dd;) {
int d[100][100];
fill(d[0], d[100], 1 << 30);
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++) {
d[i + dd][j + dd] = 0;
}
}
for (int j = 0; j < s; j++) {
for (int i = 0; i < dd; i++) {
int cd;
cin >> cd;
if (cd) {
d[i][dd + j] = d[dd + j][i] = cd;
}
}
}
for (int j = 0; j < dd - 1; j++) {
for (int i = 0; i < dd - j - 1; i++) {
int cd;
cin >> cd;
if (cd) {
d[j][i + j + 1] = d[i + j + 1][i] = cd;
}
}
}
bool u[100] = {};
int l = 0;
int n[100];
fill(n, n + 100, 1 << 30);
n[dd] = 0;
for (int i = 0; i < s + dd; i++) {
int nm = 1 << 30;
int x;
for (int j = 0; j < s + dd; j++) {
if (!u[j] && nm > n[j]) {
x = j;
nm = n[j];
}
}
u[x] = true;
l += n[x];
for (int j = 0; j < s + dd; j++) {
n[j] = min(n[j], d[x][j]);
}
}
cout << l << endl;
}
return 0;
}
|
// 34
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
for (int s, dd; cin >> s >> dd, s | dd;) {
int d[100][100];
fill(d[0], d[100], 1 << 30);
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++) {
d[i + dd][j + dd] = 0;
}
}
for (int j = 0; j < s; j++) {
for (int i = 0; i < dd; i++) {
int cd;
cin >> cd;
if (cd) {
d[i][dd + j] = d[dd + j][i] = cd;
}
}
}
for (int j = 0; j < dd - 1; j++) {
for (int i = 0; i < dd - j - 1; i++) {
int cd;
cin >> cd;
if (cd) {
d[j][i + j + 1] = d[i + j + 1][j] = cd;
}
}
}
bool u[100] = {};
int l = 0;
int n[100];
fill(n, n + 100, 1 << 30);
n[dd] = 0;
for (int i = 0; i < s + dd; i++) {
int nm = 1 << 30;
int x;
for (int j = 0; j < s + dd; j++) {
if (!u[j] && nm > n[j]) {
x = j;
nm = n[j];
}
}
u[x] = true;
l += n[x];
for (int j = 0; j < s + dd; j++) {
n[j] = min(n[j], d[x][j]);
}
}
cout << l << endl;
}
return 0;
}
|
[["-", 0, 11, 12, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 11, 31, 69, 341, 342, 0, 22]]
| 1
| 427
|
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 1 << 27;
const int MAX = 51;
int dis_d[MAX][MAX], in, s, d, G[MAX];
int min_d[MAX];
bool used[MAX];
int prim() {
for (int i = 0; i < d; i++) {
min_d[i] = INF;
used[i] = false;
}
min_d[0] = 0;
int res = 0;
while (true) {
int v = -1;
for (int u = 0; u < d; u++) {
if (!used[u] && (v == -1 || min_d[u] < min_d[v]))
v = u;
}
if (v == -1)
break;
used[v] = true;
res += min_d[v];
for (int u = 0; u < d; u++) {
min_d[u] = min(min_d[u], dis_d[v][u]);
}
}
return res;
}
int main() {
while (cin >> s >> d, s + d) {
fill(G, G + MAX, INF);
for (int i = 0; i < s; i++) {
for (int j = 0; j < d; j++) {
cin >> in;
if (!in)
continue;
G[j] = min(G[j], in);
}
}
fill(dis_d[0], dis_d[MAX], INF);
for (int i = 0; i < d; i++) {
dis_d[i][d] = dis_d[d][i] = G[i];
}
for (int i = 0; i < d - 1; i++) {
for (int j = i + 1; j < d; j++) {
cin >> in;
if (!in)
continue;
dis_d[i][j] = dis_d[j][i] = in;
}
}
cout << prim() + 1 << endl;
}
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 1 << 27;
const int MAX = 51;
int dis_d[MAX][MAX], in, s, d, G[MAX];
int min_d[MAX];
bool used[MAX];
int prim() {
for (int i = 0; i < d; i++) {
min_d[i] = INF;
used[i] = false;
}
min_d[0] = 0;
int res = 0;
while (true) {
int v = -1;
for (int u = 0; u < d; u++) {
if (!used[u] && (v == -1 || min_d[u] < min_d[v]))
v = u;
}
if (v == -1)
break;
used[v] = true;
res += min_d[v];
for (int u = 0; u < d; u++) {
min_d[u] = min(min_d[u], dis_d[v][u]);
}
}
return res;
}
int main() {
while (cin >> s >> d, s + d) {
fill(G, G + MAX, INF);
for (int i = 0; i < s; i++) {
for (int j = 0; j < d; j++) {
cin >> in;
if (!in)
continue;
G[j] = min(G[j], in);
}
}
fill(dis_d[0], dis_d[MAX], INF);
for (int i = 0; i < d; i++) {
dis_d[i][d] = dis_d[d][i] = G[i];
}
for (int i = 0; i < d - 1; i++) {
for (int j = i + 1; j < d; j++) {
cin >> in;
if (!in)
continue;
dis_d[i][j] = dis_d[j][i] = in;
}
}
d++;
cout << prim() << endl;
}
return 0;
}
|
[["+", 0, 52, 8, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
| 1
| 440
|
while(1):
s,d=[int(x) for x in input().split()]
A=[999 for x in range(d)]
if s==0: break
for j in range(s):
l=[int(x) for x in input().split()]
for i in range(d):
if l[i]!=0:
A[i]=min(A[i], l[i])
B=[[999 for x in range(d)] for y in range(d)]
for j in range(d-1):
l=[int(x) for x in input().split()]
for i in range(d-1-j):
if l[i]!=0:
B[i][i+j+1]=l[i]
B[i+j+1][i]=l[i]
notevaled=[1 for x in range(d)]
ans=0
for i in range(d):
mini=A.index(min(A))
ans+=A[mini]
notevaled[mini]=0
A[mini]=999
for x in range(d):
if notevaled[x]:
A[x]=min( A[x], B[mini][x] )
print(ans)
|
while(1):
s,d=[int(x) for x in input().split()]
A=[999 for x in range(d)]
if s==0: break
for j in range(s):
l=[int(x) for x in input().split()]
for i in range(d):
if l[i]!=0:
A[i]=min(A[i], l[i])
B=[[999 for x in range(d)] for y in range(d)]
for j in range(d-1):
l=[int(x) for x in input().split()]
for i in range(d-1-j):
if l[i]!=0:
B[j][i+j+1]=l[i]
B[i+j+1][j]=l[i]
notevaled=[1 for x in range(d)]
ans=0
for i in range(d):
mini=A.index(min(A))
ans+=A[mini]
notevaled[mini]=0
A[mini]=999
for x in range(d):
if notevaled[x]:
A[x]=min( A[x], B[mini][x] )
print(ans)
|
[["-", 0, 1, 0, 662, 31, 206, 51, 206, 206, 22], ["+", 0, 1, 0, 662, 31, 206, 51, 206, 206, 22], ["-", 64, 196, 0, 1, 0, 662, 31, 206, 206, 22], ["+", 64, 196, 0, 1, 0, 662, 31, 206, 206, 22]]
| 5
| 293
|
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Dominating Set
public class Main {
int n, m, res, INF = 1 << 29;
List<Integer>[] adj;
int[] cover;
boolean[] special;
void f(int k, int num) {
if (res <= num)
return;
if (k == n) {
boolean ok = true;
for (int i = 0; i < n && ok; i++)
ok &= cover[i] != 0;
if (ok)
res = Math.min(res, num);
return;
}
if (special[k]) {
f(k + 1, num);
return;
}
for (int i = 0; i < k; i++)
if (cover[i] == 0) {
boolean e = false;
for (int x : adj[i])
if (k <= x) {
e = true;
break;
}
if (!e)
return;
}
if (cover[k] == 0 || n / 5 <= adj[k].size()) {
cover[k]++;
for (int x : adj[k])
cover[x]++;
f(k + 1, num + 1);
cover[k]--;
for (int x : adj[k])
cover[x]--;
f(k + 1, num);
} else {
f(k + 1, num);
cover[k]++;
for (int x : adj[k])
cover[x]++;
f(k + 1, num + 1);
cover[k]--;
for (int x : adj[k])
cover[x]--;
}
}
@SuppressWarnings("unchecked")
void run() {
Scanner sc = new Scanner(System.in);
for (;;) {
n = sc.nextInt();
m = sc.nextInt();
if ((n | m) == 0)
break;
adj = new List[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList<Integer>();
while (m-- != 0) {
int s = sc.nextInt(), t = sc.nextInt();
adj[s].add(t);
adj[t].add(s);
}
cover = new int[n];
special = new boolean[n];
res = INF;
int c = 0;
for (int i = 0; i < n; i++) {
if (adj[i].isEmpty()) {
c++;
cover[i]++;
special[i] = true;
continue;
}
boolean f = false;
for (int x : adj[i])
if (adj[x].size() == 1) {
f = true;
break;
}
if (f) {
c++;
special[i] = true;
cover[i]++;
for (int x : adj[i])
cover[x]++;
}
}
f(0, c);
System.out.println(res);
}
}
public static void main(String[] args) { new Main().run(); }
}
|
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Dominating Set
public class Main {
int n, m, res, INF = 1 << 29;
List<Integer>[] adj;
int[] cover;
boolean[] special;
void f(int k, int num) {
if (res <= num)
return;
if (k == n) {
boolean ok = true;
for (int i = 0; i < n && ok; i++)
ok &= cover[i] != 0;
if (ok)
res = Math.min(res, num);
return;
}
if (special[k]) {
f(k + 1, num);
return;
}
for (int i = 0; i < k; i++)
if (cover[i] == 0) {
boolean e = false;
for (int x : adj[i])
if (k <= x) {
e = true;
break;
}
if (!e)
return;
}
if (cover[k] == 0 || n / 5 <= adj[k].size()) {
cover[k]++;
for (int x : adj[k])
cover[x]++;
f(k + 1, num + 1);
cover[k]--;
for (int x : adj[k])
cover[x]--;
f(k + 1, num);
} else {
f(k + 1, num);
cover[k]++;
for (int x : adj[k])
cover[x]++;
f(k + 1, num + 1);
cover[k]--;
for (int x : adj[k])
cover[x]--;
}
}
@SuppressWarnings("unchecked")
void run() {
Scanner sc = new Scanner(System.in);
for (;;) {
n = sc.nextInt();
m = sc.nextInt();
if ((n | m) == 0)
break;
adj = new List[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList<Integer>();
while (m-- != 0) {
int s = sc.nextInt(), t = sc.nextInt();
adj[s].add(t);
adj[t].add(s);
}
cover = new int[n];
special = new boolean[n];
res = INF;
int c = 0;
for (int i = 0; i < n; i++) {
if (adj[i].isEmpty()) {
c++;
cover[i]++;
special[i] = true;
continue;
}
boolean f = false;
for (int x : adj[i])
if (adj[x].size() == 1 && !special[x]) {
f = true;
break;
}
if (f) {
c++;
special[i] = true;
cover[i]++;
for (int x : adj[i])
cover[x]++;
}
}
f(0, c);
System.out.println(res);
}
}
public static void main(String[] args) { new Main().run(); }
}
|
[["+", 0, 514, 8, 57, 15, 15, 0, 16, 17, 98], ["+", 8, 57, 15, 15, 0, 16, 12, 91, 17, 111], ["+", 15, 15, 0, 16, 12, 91, 439, 504, 516, 22], ["+", 15, 15, 0, 16, 12, 91, 439, 504, 0, 70], ["+", 15, 15, 0, 16, 12, 91, 439, 504, 71, 22], ["+", 15, 15, 0, 16, 12, 91, 439, 504, 0, 73]]
| 3
| 691
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
VVI E;
int n, m;
VI D;
int cnt;
VI F;
int answer;
const int inf = 1 << 24;
bool check() {
for (int i = 0; i < n; i++) {
if (!F[i]) {
bool flag = true;
for (int j = 0; j < (int)E[i].size() && flag; j++) {
if (F[E[i][j]])
flag = false;
}
if (flag)
return false;
}
}
return true;
}
void solve(int s) {
if (s == n) {
if (check())
answer = cnt;
return;
}
F[s] = 1;
cnt++;
solve(s + 1);
F[s] = 0;
cnt--;
solve(s + 1);
}
int main() {
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
F = VI(n, 0);
E = VVI(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
E[a].push_back(b);
E[b].push_back(a);
}
answer = n;
cnt = 0;
solve(0);
cout << answer << endl;
}
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
VVI E;
int n, m;
VI D;
int cnt;
VI F;
int answer;
const int inf = 1 << 24;
bool check() {
for (int i = 0; i < n; i++) {
if (!F[i]) {
bool flag = true;
for (int j = 0; j < (int)E[i].size() && flag; j++) {
if (F[E[i][j]])
flag = false;
}
if (flag)
return false;
}
}
return true;
}
void solve(int s) {
if (s == n) {
if (cnt < answer && check())
answer = cnt;
return;
}
F[s] = 1;
cnt++;
solve(s + 1);
F[s] = 0;
cnt--;
solve(s + 1);
}
int main() {
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
F = VI(n, 0);
E = VVI(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
E[a].push_back(b);
E[b].push_back(a);
}
answer = n;
cnt = 0;
solve(0);
cout << answer << endl;
}
return 0;
}
|
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 22], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 98]]
| 1
| 345
|
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
constexpr int MAX_N = 30;
int ans;
int n, m;
int cover[30];
int can[30];
int used;
int ok;
vector<vector<int>> edges;
void dfs(int idx, int sum) {
if (sum >= ans)
return;
if (used == ok) {
ans = sum;
return;
}
if (idx == n)
return;
if ((used | can[idx]) != ok)
return;
dfs(idx + 1, sum);
const int tmp = used;
used |= cover[idx];
dfs(idx + 1, sum + 1);
used = tmp;
}
int solve() {
used = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (edges[i].size() == 0) {
used |= (1 << i);
++cnt;
} else if (edges[i].size() == 1) {
const int neighbor = edges[i][0];
if (used & (1 << neighbor))
continue;
used |= cover[neighbor];
++cnt;
}
}
can[n - 1] = cover[n - 1];
for (int i = n - 1; i > 0; --i) {
can[i - 1] = (can[i] | cover[i]);
}
ok = (1 << n) - 1;
ans = n - __builtin_popcount(used) + cnt;
dfs(0, cnt);
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> n >> m && n) {
memset(cover, 0, sizeof(cover));
for (int i = 0; i < n; ++i) {
cover[i] = (1 << i);
}
edges.assign(n, vector<int>());
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
cover[a] |= (1 << b);
cover[b] |= (1 << a);
edges[a].emplace_back(b);
edges[b].emplace_back(a);
}
cout << solve() << endl;
}
return EXIT_SUCCESS;
}
|
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
constexpr int MAX_N = 30;
int ans;
int n, m;
int cover[30];
int can[30];
int used;
int ok;
vector<vector<int>> edges;
void dfs(int idx, int sum) {
if (sum >= ans)
return;
if (used == ok) {
ans = sum;
return;
}
if (idx == n)
return;
if ((used | can[idx]) != ok)
return;
dfs(idx + 1, sum);
const int tmp = used;
used |= cover[idx];
dfs(idx + 1, sum + 1);
used = tmp;
}
int solve() {
used = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (edges[i].size() == 0) {
used |= (1 << i);
++cnt;
} else if (edges[i].size() == 1) {
const int neighbor = edges[i][0];
if (used & (1 << neighbor))
continue;
used |= cover[neighbor];
++cnt;
}
}
can[n - 1] = cover[n - 1];
for (int i = n - 2; i >= 0; --i) {
can[i] = (can[i + 1] | cover[i]);
}
ok = (1 << n) - 1;
ans = n - __builtin_popcount(used) + cnt;
dfs(0, cnt);
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> n >> m && n) {
memset(cover, 0, sizeof(cover));
for (int i = 0; i < n; ++i) {
cover[i] = (1 << i);
}
edges.assign(n, vector<int>());
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
cover[a] |= (1 << b);
cover[b] |= (1 << a);
edges[a].emplace_back(b);
edges[b].emplace_back(a);
}
cout << solve() << endl;
}
return EXIT_SUCCESS;
}
|
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 47], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 20], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 16, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 0, 16, 31, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 502
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int V, d;
while (cin >> V >> d) {
vector<int> fib_mod(V);
fib_mod[0] = 2;
fib_mod[1] = 3;
for (int i = 2; i < V; i++) {
fib_mod[i] = fib_mod[i - 1] + fib_mod[i - 2];
fib_mod[i] %= 1001;
}
sort(fib_mod.begin(), fib_mod.end());
int count = 1;
for (int i = 1; i <= V; i++) {
if (fib_mod[i] - fib_mod[i - 1] >= d)
count++;
}
cout << count << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int V, d;
while (cin >> V >> d) {
vector<int> fib_mod(V);
fib_mod[0] = 2;
fib_mod[1] = 3;
for (int i = 2; i < V; i++) {
fib_mod[i] = fib_mod[i - 1] + fib_mod[i - 2];
fib_mod[i] %= 1001;
}
sort(fib_mod.begin(), fib_mod.end());
int count = 1;
for (int i = 1; i < V; i++) {
if (fib_mod[i] - fib_mod[i - 1] >= d)
count++;
}
cout << count << endl;
}
return 0;
}
|
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18]]
| 1
| 156
|
#include <bits/stdc++.h>
using namespace std;
#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r), end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout << #x << ": " << x << endl
#define fcout(n) cout << fixed << setprecision((n))
#define scout(n) cout << setw(n)
#define vary(type, name, size, init) vector<type> name(size, init)
#define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init))
#define mp(a, b) make_pair(a, b)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define repi(it, array) \
for (auto it = array.begin(), end = array.end(); it != end; ++it)
#define repa(n, array) for (auto &n : (array))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1000000007;
constexpr ll inf = ((1 << 30) - 1) * 2 + 1;
constexpr double PI = acos(-1.0);
double eps = 1e-10;
const int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1};
const int dx[] = {0, -1, 0, 1, 1, -1, -1, 1};
const int N = 1001;
vector<int> g[N + 4];
ll v, d;
void add_edge(int from, int to) {
g[from].push_back(to);
g[to].push_back(from);
}
vector<ll> f(v + 1, 0);
void dfs(int k, int c) {
if (f[k] == c)
return;
f[k] = c;
for (auto &&e : g[k]) {
dfs(e, c);
}
}
int solve() {
int res = 0;
f = vector<ll>(v + 1, 0);
REP(i, 2, v + 2) {
if (f[i] == 0) {
++res;
dfs(i, res);
}
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int fib[N + 4];
fib[0] = 1, fib[1] = 1;
REP(i, 2, N + 4) { fib[i] = (fib[i - 1] + fib[i - 2]) % N; }
while (cin >> v >> d) {
rep(i, N + 4) { g[i].clear(); }
REP(i, 2, v + 2) {
REP(j, i + 1, v + 2) {
if (abs(fib[i] - fib[j]) < d) {
add_edge(i, j);
}
}
}
cout << solve() << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r), end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout << #x << ": " << x << endl
#define fcout(n) cout << fixed << setprecision((n))
#define scout(n) cout << setw(n)
#define vary(type, name, size, init) vector<type> name(size, init)
#define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init))
#define mp(a, b) make_pair(a, b)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define repi(it, array) \
for (auto it = array.begin(), end = array.end(); it != end; ++it)
#define repa(n, array) for (auto &n : (array))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1000000007;
constexpr ll inf = ((1 << 30) - 1) * 2 + 1;
constexpr double PI = acos(-1.0);
double eps = 1e-10;
const int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1};
const int dx[] = {0, -1, 0, 1, 1, -1, -1, 1};
const int N = 1001;
vector<int> g[N + 4];
ll v, d;
void add_edge(int from, int to) {
g[from].push_back(to);
g[to].push_back(from);
}
vector<ll> f(N + 4, 0);
void dfs(int k, int c) {
if (f[k] == c)
return;
f[k] = c;
for (auto &&e : g[k]) {
dfs(e, c);
}
}
int solve() {
int res = 0;
f = vector<ll>(N + 4, 0);
REP(i, 2, v + 2) {
if (f[i] == 0) {
++res;
dfs(i, res);
}
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int fib[N + 4];
fib[0] = 1, fib[1] = 1;
REP(i, 2, N + 4) { fib[i] = (fib[i - 1] + fib[i - 2]) % N; }
while (cin >> v >> d) {
rep(i, N + 4) { g[i].clear(); }
REP(i, 2, v + 2) {
REP(j, i + 1, v + 2) {
if (abs(fib[i] - fib[j]) < d) {
add_edge(i, j);
}
}
}
cout << solve() << endl;
}
return 0;
}
|
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["-", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 583
|
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
int n, d, array[1005], count;
while (scanf("%d %d", &n, &d) != EOF) {
array[0] = 2;
array[1] = 3;
for (int i = 2; i < n; i++) {
array[i] = (array[i - 1] + array[i - 2]) % 1001;
}
std::sort(array, array + n - 1);
count = 1;
for (int i = 1; i < n; i++) {
if (abs(array[i] - array[i - 1]) >= d)
count++;
}
printf("%d\n", count);
}
return 0;
}
|
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
int n, d, array[1005], count;
while (scanf("%d %d", &n, &d) != EOF) {
array[0] = 2;
array[1] = 3;
for (int i = 2; i < n; i++) {
array[i] = (array[i - 1] + array[i - 2]) % 1001;
}
std::sort(array, array + n);
count = 1;
for (int i = 1; i < n; i++) {
if (abs(array[i] - array[i - 1]) >= d)
count++;
}
printf("%d\n", count);
}
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 168
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toStr(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
const int MOD = 1000000007;
struct UnionFind {
vi data;
UnionFind(int size) : data(size, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool find(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
int f[1001];
f[0] = 1, f[1] = 2;
FOR(i, 2, 1000) { f[i] = (f[i - 1] + f[i - 2]) % 1001; }
int V, d;
while (cin >> V >> d) {
UnionFind uf(V + 1);
FOR(i, 1, V) {
FOR(j, i + 1, V - 1) {
if (abs(f[i] - f[j]) < d) {
uf.unite(i, j);
break;
}
}
}
set<int> ans;
FOR(i, 1, V) { ans.insert(uf.root(i)); }
cout << ans.size() << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toStr(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
const int MOD = 1000000007;
struct UnionFind {
vi data;
UnionFind(int size) : data(size, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool find(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
int f[1001];
f[0] = 1, f[1] = 2;
FOR(i, 2, 1000) { f[i] = (f[i - 1] + f[i - 2]) % 1001; }
int V, d;
while (cin >> V >> d) {
UnionFind uf(V + 1);
FOR(i, 1, V) {
FOR(j, i + 1, V) {
if (abs(f[i] - f[j]) < d) {
uf.unite(i, j);
}
}
}
set<int> ans;
FOR(i, 1, V) { ans.insert(uf.root(i)); }
cout << ans.size() << endl;
}
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["-", 0, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["-", 0, 9, 0, 57, 64, 9, 0, 93, 0, 94]]
| 1
| 537
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toStr(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
struct unionfind {
vi data, rank;
// Initialize
unionfind(int size) {
REP(i, size) {
data.push_back(i);
rank.push_back(0);
}
}
// Find root
int find(int x) { return (data[x] == x) ? x : data[x] = find(data[x]); }
// Unite
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
data[x] = y;
} else {
data[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
// Whether x and y belong to the same set
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
vi f(1001);
f[0] = 1;
f[1] = 2;
FOR(i, 2, 1000) { f[i] = (f[i - 1] + f[i - 2]) % 1001; }
int N, d;
while (cin >> N >> d) {
unionfind uf(N + 1);
FOR(i, 1, N) {
FOR(j, i + 1, N) {
if (abs(f[i] - f[j]) < d) {
uf.unite(i, j);
}
}
}
int ans = 0;
vi memo(N, 0);
REP(i, N) {
int root = uf.find(i);
if (memo[root] == 0) {
memo[root] = 1;
ans++;
}
}
cout << ans << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toStr(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
struct unionfind {
vi data, rank;
// Initialize
unionfind(int size) {
REP(i, size) {
data.push_back(i);
rank.push_back(0);
}
}
// Find root
int find(int x) { return (data[x] == x) ? x : data[x] = find(data[x]); }
// Unite
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
data[x] = y;
} else {
data[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
// Whether x and y belong to the same set
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
vi f(1001);
f[0] = 1;
f[1] = 2;
FOR(i, 2, 1000) { f[i] = (f[i - 1] + f[i - 2]) % 1001; }
int N, d;
while (cin >> N >> d) {
unionfind uf(N + 1);
FOR(i, 1, N) {
FOR(j, i + 1, N) {
if (abs(f[i] - f[j]) < d) {
uf.unite(i, j);
}
}
}
int ans = 0;
vi memo(N + 1, 0);
FOR(i, 1, N) {
int root = uf.find(i);
if (memo[root] == 0) {
memo[root] = 1;
ans++;
}
}
cout << ans << endl;
}
return 0;
}
|
[["+", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 14, 49, 53, 49, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
| 1
| 557
|
// 1016.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
class UnionFind {
public:
int par[MAX_N]; // 親
int rank[MAX_N]; // 木の深さ
// n要素で初期化
UnionFind(int n) {
for (int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
}
}
// 木の根を求める
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
int v, d;
while (cin >> v >> d) {
int f[v];
for (int i = 0; i < v; ++i) {
if (i == 0)
f[i] = 1 + 1;
else if (i == 1)
f[i] = f[i - 1] + 1;
else
f[i] = (f[i - 1] + f[i - 2]) % 1001;
}
UnionFind u(v);
for (int i = 0; i < v - 1; ++i)
for (int j = i + 1; j < v; ++j)
if (abs(f[i] - f[j]) < d)
u.unite(i, j);
int ans;
for (int i = 0; i < v; ++i)
if (u.find(i) == i)
ans++;
cout << ans << endl;
}
return 0;
}
|
// 1016.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
class UnionFind {
public:
int par[MAX_N]; // 親
int rank[MAX_N]; // 木の深さ
// n要素で初期化
UnionFind(int n) {
for (int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
}
}
// 木の根を求める
int find(int x) {
if (par[x] == x)
return x;
else
return par[x] = find(par[x]);
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
int v, d;
while (cin >> v >> d) {
int f[v];
for (int i = 0; i < v; ++i) {
if (i == 0)
f[i] = 1 + 1;
else if (i == 1)
f[i] = f[i - 1] + 1;
else
f[i] = (f[i - 1] + f[i - 2]) % 1001;
}
UnionFind u(v);
for (int i = 0; i < v - 1; ++i)
for (int j = i + 1; j < v; ++j)
if (abs(f[i] - f[j]) < d)
u.unite(i, j);
int ans = 0;
for (int i = 0; i < v; ++i)
if (u.find(i) == i)
ans++;
cout << ans << endl;
}
return 0;
}
|
[["+", 0, 52, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 420
|
#include <algorithm>
#include <cstring>
#include <iostream>
#define N_F 1010
#define N_CONST 1001
using namespace std;
int v, d, f1, f2;
unsigned long long int f[N_F];
int node[N_CONST + 10][N_CONST + 10];
void rmNode(int i) {
// cout << " remove : "<<i<<endl;
node[i][i] = 0;
for (int j = 0; j < N_CONST; j++) {
if (node[j][j] == 0)
continue;
if (node[i][j] == 1) {
rmNode(j);
}
}
}
int main() {
f[0] = 1;
f[1] = 2;
for (int i = 2; i < N_F; i++) {
f[i] = f[i - 1] + f[i - 2];
// f[i] = (f[i-1] + f[i-2])% N_CONST;
// cout << i << "\t"<< f[i] << endl;
}
while (cin >> v >> d) {
// cout << v <<","<< d<< endl;
memset(node, 0, sizeof(node));
// cout << "test1" << endl;
for (int i = 1; i <= v; i++) {
f1 = f[i] % N_CONST; // node ÂÃ label
node[f1][f1] =
1; // nodeª¶Â÷Âé±ÂÃÂðÂ\·
// cout << "test3 i="<<i<<" f1="<<f1 << endl;
for (int j = 1; j <= v; j++) {
f2 = f[j] % N_CONST;
// if( abs( f[i]-f[j] ) < d ){
// cout << "test4 j="<<j<<" f[j]="<<f[j]<<" f2="<<f2 << endl;
if (abs(f1 - f2) < d) {
node[f1][f2] = 1;
node[f2][f1] = 1;
}
}
}
// cout << "test2" << endl;
int n = 0;
for (int i = 0; i < N_CONST; i++) {
if (node[i][i] ==
0) // ÂmÂ[Âhª¶ÂõÂâ
continue;
n++;
rmNode(
i); // i
// ÂÃÂñ³Âê½ÂmÂ[ÂhÂðÂSÂÃÂÃÂÂ
}
cout << n << endl;
// break;
}
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
#define N_F 1010
#define N_CONST 1001
using namespace std;
int v, d, f1, f2;
unsigned long long int f[N_F];
int node[N_CONST + 10][N_CONST + 10];
void rmNode(int i) {
// cout << " remove : "<<i<<endl;
node[i][i] = 0;
for (int j = 0; j < N_CONST; j++) {
if (node[j][j] == 0)
continue;
if (node[i][j] == 1) {
rmNode(j);
}
}
}
int main() {
f[0] = 1;
f[1] = 2;
for (int i = 2; i < N_F; i++) {
// f[i] = f[i-1] + f[i-2];
f[i] = (f[i - 1] + f[i - 2]) % N_CONST;
// cout << i << "\t"<< f[i] << endl;
}
while (cin >> v >> d) {
// cout << v <<","<< d<< endl;
memset(node, 0, sizeof(node));
// cout << "test1" << endl;
for (int i = 1; i <= v; i++) {
f1 = f[i] % N_CONST; // node ÂÃ label
node[f1][f1] =
1; // nodeª¶Â÷Âé±ÂÃÂðÂ\·
// cout << "test3 i="<<i<<" f1="<<f1 << endl;
for (int j = 1; j <= v; j++) {
f2 = f[j] % N_CONST;
// if( abs( f[i]-f[j] ) < d ){
// cout << "test4 j="<<j<<" f[j]="<<f[j]<<" f2="<<f2 << endl;
if (abs(f1 - f2) < d) {
node[f1][f2] = 1;
node[f2][f1] = 1;
}
}
}
// cout << "test2" << endl;
int n = 0;
for (int i = 0; i < N_CONST; i++) {
if (node[i][i] ==
0) // ÂmÂ[Âhª¶ÂõÂâ
continue;
n++;
rmNode(
i); // i
// ÂÃÂñ³Âê½ÂmÂ[ÂhÂðÂSÂÃÂÃÂÂ
}
cout << n << endl;
// break;
}
return 0;
}
|
[["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 25], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 109], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
| 1
| 351
|
#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
static const double EPS = 1e-8;
const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};
const static ll N = 1001;
short fib_table[10000000];
int fib(int n) {
if (n == -1 || n == 0)
return 1;
else if (fib_table[n] != -1)
return fib_table[n];
else
return (fib_table[n] = ((fib(n - 1) % N) + (fib(n - 2) % N)) % N);
}
int main() {
memset(fib_table, -1, sizeof(fib_table));
int V, d;
while (~scanf("%d %d", &V, &d)) {
int rank[N];
int par[N];
vector<int> fibs;
for (int i = 1; i <= V; i++) {
fibs.push_back(fib(i));
}
sort(fibs.begin(), fibs.end());
int res = 0;
for (int i = 1; i < fibs.size(); i++) {
if (fibs[i] - fibs[i - 1] >= d)
res++;
}
cout << res << endl;
}
}
|
#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
static const double EPS = 1e-8;
const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};
const static ll N = 1001;
short fib_table[10000000];
int fib(int n) {
if (n == -1 || n == 0)
return 1;
else if (fib_table[n] != -1)
return fib_table[n];
else
return (fib_table[n] = ((fib(n - 1) % N) + (fib(n - 2) % N)) % N);
}
int main() {
memset(fib_table, -1, sizeof(fib_table));
int V, d;
while (~scanf("%d %d", &V, &d)) {
int rank[N];
int par[N];
vector<int> fibs;
for (int i = 1; i <= V; i++) {
fibs.push_back(fib(i));
}
sort(fibs.begin(), fibs.end());
int res = 1;
for (int i = 1; i < fibs.size(); i++) {
if (fibs[i] - fibs[i - 1] >= d)
res++;
}
cout << res << endl;
}
}
|
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 336
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1001;
int main() {
int V, d;
while (cin >> V >> d) {
int p1 = 1, p2 = 1;
vector<int> fib(V);
for (int i = 0; i < V; ++i) {
fib[i] = p1 + p2;
p2 = p1;
p1 = fib[i];
}
sort(fib.begin(), fib.end());
int answer = 1;
for (int i = 1; i < V; ++i) {
if (fib[i] - fib[i - 1] >= d) {
++answer;
}
}
cout << answer << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1001;
int main() {
int V, d;
while (cin >> V >> d) {
int p1 = 1, p2 = 1;
vector<int> fib(V);
for (int i = 0; i < V; ++i) {
fib[i] = (p1 + p2) % MOD;
p2 = p1;
p1 = fib[i];
}
sort(fib.begin(), fib.end());
int answer = 1;
for (int i = 1; i < V; ++i) {
if (fib[i] - fib[i - 1] >= d) {
++answer;
}
}
cout << answer << endl;
}
return 0;
}
|
[["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 25], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 109], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
| 1
| 157
|
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#define N 1001
using namespace std;
vector<int> par;
void init(int);
int root(int);
void uni(int, int);
bool same(int, int);
int main() {
int V, d;
while (cin >> V >> d) {
init(V);
vector<int> f(V);
int temp1, temp2;
for (int i = 0; i < V; i++) {
if (i == 0)
f[i] = temp1 = 2;
else if (i == 1)
f[i] = temp2 = 3;
else {
f[i] = temp1 + temp2;
temp1 = temp2;
temp2 = f[i];
}
}
sort(f.begin(), f.end());
for (int i = 0; i < V - 1; i++)
if (f[i + 1] - f[i] < d)
uni(i, i + 1);
int count = 1;
for (int i = 0; i < V - 1; i++)
if (root(i) != root(i + 1))
count++;
cout << count << endl;
}
return 0;
}
//初期化
void init(int n) {
par.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i; //最初自分の集合の代表は自分
}
}
//木の根を求める
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
//xとyの属する集合を併合
void uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
par[x] = y;
}
//xとyが同じ集合に属するか否か
bool same(int x, int y) { return root(x) == root(y); }
|
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#define N 1001
using namespace std;
vector<int> par;
void init(int);
int root(int);
void uni(int, int);
bool same(int, int);
int main() {
int V, d;
while (cin >> V >> d) {
init(V);
vector<int> f(V);
int temp1, temp2;
for (int i = 0; i < V; i++) {
if (i == 0)
f[i] = temp1 = 2;
else if (i == 1)
f[i] = temp2 = 3;
else {
f[i] = (temp1 + temp2) % N;
temp1 = temp2;
temp2 = f[i];
}
}
sort(f.begin(), f.end());
for (int i = 0; i < V - 1; i++)
if (f[i + 1] - f[i] < d)
uni(i, i + 1);
int count = 1;
for (int i = 0; i < V - 1; i++)
if (root(i) != root(i + 1))
count++;
cout << count << endl;
}
return 0;
}
//初期化
void init(int n) {
par.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i; //最初自分の集合の代表は自分
}
}
//木の根を求める
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
//xとyの属する集合を併合
void uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
par[x] = y;
}
//xとyが同じ集合に属するか否か
bool same(int x, int y) { return root(x) == root(y); }
|
[["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 31, 23, 0, 25], ["+", 0, 9, 0, 1, 0, 11, 12, 16, 17, 109], ["+", 0, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
| 1
| 402
|
N = 1001
def init_fib
fib = Array.new(1002)
fib[0] = 1
fib[1] = 2
2.upto(N) do |i|
fib[i] = (fib[i-1] + fib[i-2]) % N
end
fib
end
def root(tree, i)
if tree[i] == i
i
else
root(tree, tree[i])
end
end
if __FILE__ == $0
fib = init_fib
STDIN.each_line do |input|
count = 0
v, d = input.split.map(&:to_i)
tree = Array.new(v+1)
0.upto(v) do |i|
tree[i] = i
end
1.upto(v) do |i|
(i+1).upto(v) do |j|
tree[j] = root(tree, i) if (fib[j] - fib[i]).abs < d
end
end
1.upto(v) do |i|
count += 1 if root(tree, i) == i
end
puts count.to_s
end
end
|
N = 1001
def init_fib
fib = Array.new(1002)
fib[0] = 1
fib[1] = 2
2.upto(N) do |i|
fib[i] = (fib[i-1] + fib[i-2]) % N
end
fib
end
def root(tree, i)
if tree[i] == i
i
else
root(tree, tree[i])
end
end
if __FILE__ == $0
fib = init_fib
STDIN.each_line do |input|
count = 0
v, d = input.split.map(&:to_i)
tree = Array.new(v+1)
# treeの初期化
# tree[i] == i の場合、rootである
0.upto(v) do |i|
tree[i] = i
end
# 条件を満たすleafの統合
# 常にrootに新しいleafをつける
1.upto(v) do |i|
(i+1).upto(v) do |j|
tree[root(tree, j)] = root(tree, i) if (fib[j] - fib[i]).abs < d
end
end
# rootの数を数える
1.upto(v) do |i|
count += 1 if root(tree, i) == i
end
# p tree
puts count.to_s
end
end
|
[["+", 0, 751, 8, 662, 31, 742, 0, 652, 735, 22], ["+", 8, 662, 31, 742, 0, 652, 3, 4, 0, 24], ["+", 8, 662, 31, 742, 0, 652, 3, 4, 0, 22], ["+", 8, 662, 31, 742, 0, 652, 3, 4, 0, 21], ["+", 8, 662, 31, 742, 0, 652, 3, 4, 0, 25]]
| 4
| 226
|
while 1:
try:
v,d=map(int,input().split())
f=[0]*(v+1)
f[0],f[1]=1,2
for i in range(2,v+1):
f[i]=f[i-1]+f[i-2]
f.sort()
c=1
for i in range(2,v+1):
if f[i]-f[i-1]>=d: c+=1
print(c)
except:break
|
while 1:
try:
v,d=map(int,input().split())
f=[0]*(v+1)
f[0],f[1]=1,2
for i in range(2,v+1):
f[i]=(f[i-1]+f[i-2])%1001
f.sort()
c=1
for i in range(2,v+1):
if f[i]-f[i-1]>=d: c+=1
print(c)
except:break
|
[["+", 0, 1, 0, 662, 12, 657, 31, 23, 0, 24], ["+", 0, 1, 0, 662, 12, 657, 31, 23, 0, 25], ["+", 8, 196, 0, 1, 0, 662, 12, 657, 17, 109], ["+", 8, 196, 0, 1, 0, 662, 12, 657, 12, 612]]
| 5
| 120
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.