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
|
|---|---|---|---|---|
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, K, A, B, dp[11][2001];
vector<int> R[10];
int main() {
scanf("%d", &N);
scanf("%d", &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A);
scanf("%d", &B);
R[B - 1].push_back(A);
}
for (int i = 0; i < 10; i++) {
sort(R[i].begin(), R[i].end(), greater<int>());
for (int j = 1; j < R[i].size(); j++) {
R[i][j] = R[i][j] + R[i][j - 1] + j * 2;
}
}
for (int i = 1; i < 11; i++) {
for (int j = 1; j <= K; j++) {
dp[i][j] = dp[i - 1][j];
for (int k = 0; k < R[i].size(); k++) {
if (j - k >= 1) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - k - 1] + R[i][k]);
}
}
}
}
printf("%d\n", dp[10][K]);
return 0;
}
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, K, A, B, dp[11][2001];
vector<int> R[10];
int main() {
scanf("%d", &N);
scanf("%d", &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A);
scanf("%d", &B);
R[B - 1].push_back(A);
}
for (int i = 0; i < 10; i++) {
sort(R[i].begin(), R[i].end(), greater<int>());
for (int j = 1; j < R[i].size(); j++) {
R[i][j] = R[i][j] + R[i][j - 1] + j * 2;
}
}
for (int i = 1; i < 11; i++) {
for (int j = 1; j <= K; j++) {
dp[i][j] = dp[i - 1][j];
for (int k = 0; k < R[i - 1].size(); k++) {
if (j - k >= 1) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - k - 1] + R[i - 1][k]);
}
}
}
}
printf("%d\n", dp[10][K]);
return 0;
}
|
[["+", 63, 118, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 63, 118, 28, 69, 341, 342, 0, 16, 12, 13], ["+", 12, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 12, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 354
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
int main(void) {
int n, k, i, j, T;
int c[2001], g[2001], sum;
int jan[11][2001], cnt[11], x, kk, temp;
int janaf[11][2001], dp[11][2001];
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++)
scanf("%d %d", &c[i], &g[i]);
for (i = 1; i <= 10; i++)
cnt[i] = 0;
for (i = 0; i <= 10; i++) {
for (j = 0; j < 2000; j++) {
jan[i][j] = 0;
janaf[i][j] = 0;
}
}
for (i = 0; i < n; i++) {
x = g[i];
jan[x][cnt[x]] = c[i];
cnt[x]++;
}
for (i = 1; i <= n; i++)
sort(jan[i], jan[i] + cnt[i], greater<int>());
sum = 0;
for (i = 1; i <= n; i++) {
for (j = 0; j < cnt[i]; j++) {
sum += jan[i][j];
janaf[i][j] = sum;
}
sum = 0;
}
for (i = 1; i <= n; i++) {
for (j = 0; j < cnt[i]; j++)
janaf[i][j] += (j + 1) * j;
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++)
dp[i][j] = -1;
}
dp[0][0] = 0;
for (i = 1; i <= 10; i++) {
for (j = 0; j <= k; j++) {
for (kk = 0; kk <= cnt[i]; kk++) {
if (dp[i - 1][j] > dp[i][j])
dp[i][j] = dp[i - 1][j];
if (k >= j + (kk + 1)) {
if (dp[i][j + (kk + 1)] < dp[i - 1][j] + janaf[i][kk]) {
dp[i][j + (kk + 1)] = dp[i - 1][j] + janaf[i][kk];
}
}
}
}
}
printf("%d\n", dp[10][k]);
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
int main(void) {
int n, k, i, j, T;
int c[2001], g[2001], sum;
int jan[11][2001], cnt[11], x, kk, temp;
int janaf[11][2001], dp[11][2001];
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++)
scanf("%d %d", &c[i], &g[i]);
for (i = 1; i <= 10; i++)
cnt[i] = 0;
for (i = 0; i <= 10; i++) {
for (j = 0; j < 2000; j++) {
jan[i][j] = 0;
janaf[i][j] = 0;
}
}
for (i = 0; i < n; i++) {
x = g[i];
jan[x][cnt[x]] = c[i];
cnt[x]++;
}
for (i = 1; i <= 10; i++)
sort(jan[i], jan[i] + cnt[i], greater<int>());
sum = 0;
for (i = 1; i <= 10; i++) {
for (j = 0; j < cnt[i]; j++) {
sum += jan[i][j];
janaf[i][j] = sum;
}
sum = 0;
}
for (i = 1; i <= 10; i++) {
for (j = 0; j < cnt[i]; j++)
janaf[i][j] += (j + 1) * j;
}
for (i = 0; i <= 10; i++) {
for (j = 0; j <= k; j++)
dp[i][j] = -1;
}
dp[0][0] = 0;
for (i = 1; i <= 10; i++) {
for (j = 0; j <= k; j++) {
for (kk = 0; kk <= cnt[i]; kk++) {
if (dp[i - 1][j] > dp[i][j])
dp[i][j] = dp[i - 1][j];
if (k >= j + (kk + 1)) {
if (dp[i][j + (kk + 1)] < dp[i - 1][j] + janaf[i][kk]) {
dp[i][j + (kk + 1)] = dp[i - 1][j] + janaf[i][kk];
}
}
}
}
}
printf("%d\n", dp[10][k]);
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
| 1
| 620
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
long long int lis[11][2003], lissize[10] = {0};
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] = -2000000000;
}
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
lis[b][lissize[b] + 1] = a;
lissize[b]++;
}
for (int i = 0; i < 11; i++) {
sort(lis[i] + 1, lis[i] + 2003);
reverse(lis[i] + 1, lis[i] + 2003);
}
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] += lis[i][j - 1] + (j - 1) * 2;
}
/*
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(0<=lis[i][j])printf("[%lld]",lis[i][j]);
}
puts("");
}
*/
long long int fie[12][4003] = {0};
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2001; j++) {
for (int k = 0; k < 2001; k++) {
if (j + k <= 2000)
fie[i + 1][j + k] = max(fie[i + 1][j + k], fie[i][j] + lis[i][k]);
}
}
}
cout << fie[10][K] << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
long long int lis[11][2003], lissize[11] = {0};
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] = -2000000000;
}
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
lis[b][lissize[b] + 1] = a;
lissize[b]++;
}
for (int i = 0; i < 11; i++) {
sort(lis[i] + 1, lis[i] + 2003);
reverse(lis[i] + 1, lis[i] + 2003);
}
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] += lis[i][j - 1] + (j - 1) * 2;
}
/*
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(0<=lis[i][j])printf("[%lld]",lis[i][j]);
}
puts("");
}
*/
long long int fie[13][4003] = {0};
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2001; j++) {
for (int k = 0; k < 2001; k++) {
if (j + k <= 2000)
fie[i + 1][j + k] = max(fie[i + 1][j + k], fie[i][j] + lis[i + 1][k]);
}
}
}
cout << fie[10][K] << endl;
return 0;
}
|
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 43, 49, 50, 49, 80, 49, 80, 81, 13], ["+", 0, 43, 49, 50, 49, 80, 49, 80, 81, 13], ["+", 12, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["+", 12, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 373
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai \
ios::sync_with_stdio(false); \
cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
os << "(" << p.fst << ", " << p.scd << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
rep(i, v.size()) {
if (i)
os << ", ";
os << v[i];
}
return os;
}
template <typename T> T &max(T &a, T &b) {
if (a >= b)
return a;
return b;
}
template <typename T> T &min(T &a, T &b) {
if (a < b)
return a;
return b;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;
int N, K;
int C[2005], G[2005];
vi books[10];
int dp[11][2005]; // dp[i][j] :=
// i?¨?????????§???j????????£?????????????????????????????§???
int main() {
cin >> N >> K;
rep(i, N) {
cin >> C[i] >> G[i];
books[G[i]].PB(C[i]);
}
rep(i, 10) {
sort(all(books[i]), greater<int>());
books[i].insert(begin(books[i]), 0);
for (int j = 1; j < books[i].size(); ++j) {
books[i][j] += books[i][j - 1];
books[i][j] += (j - 1) * 2;
}
}
for (int i = 1; i <= 10; ++i) {
for (int j = 0; j <= K; ++j) {
for (int k = 0; j + k <= K; ++k) {
if (k >= books[i].size())
break;
chmax(dp[i][j + k], dp[i - 1][j] + books[i][k]);
}
}
}
int ma = 0;
for (int i = 0; i <= 10; ++i)
for (int j = 0; j <= K; ++j)
chmax(ma, dp[i][j]);
cout << ma << endl;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai \
ios::sync_with_stdio(false); \
cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
os << "(" << p.fst << ", " << p.scd << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
rep(i, v.size()) {
if (i)
os << ", ";
os << v[i];
}
return os;
}
template <typename T> T &max(T &a, T &b) {
if (a >= b)
return a;
return b;
}
template <typename T> T &min(T &a, T &b) {
if (a < b)
return a;
return b;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;
int N, K;
int C[2005], G[2005];
vi books[11];
int dp[11][2005]; // dp[i][j] :=
// i?¨?????????§???j????????£?????????????????????????????§???
int main() {
cin >> N >> K;
rep(i, N) {
cin >> C[i] >> G[i];
books[G[i]].PB(C[i]);
}
rep1(i, 10) {
sort(all(books[i]), greater<int>());
books[i].insert(begin(books[i]), 0);
for (int j = 1; j < books[i].size(); ++j) {
books[i][j] += books[i][j - 1];
books[i][j] += (j - 1) * 2;
}
}
for (int i = 1; i <= 10; ++i) {
for (int j = 0; j <= K; ++j) {
for (int k = 0; j + k <= K; ++k) {
if (k >= books[i].size())
break;
chmax(dp[i][j + k], dp[i - 1][j] + books[i][k]);
}
}
}
int ma = 0;
for (int i = 0; i <= 10; ++i)
for (int j = 0; j <= K; ++j)
chmax(ma, dp[i][j]);
cout << ma << endl;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22]]
| 1
| 718
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
int g_idx[10] = {0};
int books[10][2000] = {{0}};
int table[10][2001] = {{0}};
int dp[10][2001];
int solve(int idx, int cnt) {
if (idx == 10)
return 0;
if (dp[idx][cnt] != -1)
return dp[idx][cnt];
int ret = 0;
for (int i = 0; i <= g_idx[idx]; ++i) {
if (cnt >= i && !(i != 0 && table[idx][i] == 0)) {
ret = max(ret, solve(idx + 1, cnt - i) + table[idx][i]);
}
}
return dp[idx][cnt] = ret;
}
int main() {
int c, g;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> c >> g;
--g;
books[g][g_idx[g]++] = c;
}
for (int i = 0; i < 10; ++i) {
sort(&books[i][0], &books[i][0] + k, greater<int>());
}
for (int i = 0; i < 10; ++i) {
for (int j = 1; j <= g_idx[i]; ++j) {
table[i][j] = table[i][j - 1] + books[i][j - 1] + 2 * (j - 1);
}
}
/*
for(int i = 0; i < 10; ++i) {
for(int j = 0; j <= g_idx[i]; ++j) {
cout << table[i][j] << " ";
}
cout << endl;
}
cout << endl;
*/
memset(dp, -1, sizeof(dp));
cout << solve(0, k) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
int g_idx[10] = {0};
int books[10][2000] = {{0}};
int table[10][2001] = {{0}};
int dp[10][2001];
int solve(int idx, int cnt) {
if (idx == 10)
return 0;
if (dp[idx][cnt] != -1)
return dp[idx][cnt];
int ret = 0;
for (int i = 0; i <= g_idx[idx]; ++i) {
if (cnt >= i && !(i != 0 && table[idx][i] == 0)) {
ret = max(ret, solve(idx + 1, cnt - i) + table[idx][i]);
}
}
return dp[idx][cnt] = ret;
}
int main() {
int c, g;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> c >> g;
--g;
books[g][g_idx[g]++] = c;
}
for (int i = 0; i < 10; ++i) {
sort(&books[i][0], &books[i][0] + g_idx[i], greater<int>());
}
for (int i = 0; i < 10; ++i) {
for (int j = 1; j <= g_idx[i]; ++j) {
table[i][j] = table[i][j - 1] + books[i][j - 1] + 2 * (j - 1);
}
}
/*
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < n; ++j) {
cout << books[i][j] << " ";
}
cout << endl;
}
cout << endl;
for(int i = 0; i < 10; ++i) {
for(int j = 0; j <= g_idx[i]; ++j) {
cout << table[i][j] << " ";
}
cout << endl;
}
cout << endl;
*/
memset(dp, -1, sizeof(dp));
cout << solve(0, k) << endl;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22], ["+", 0, 2, 3, 4, 0, 16, 12, 69, 28, 22], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 70], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 73]]
| 1
| 384
|
#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10 << 10
int dp[2048] = {0};
int main() {
int n, k;
cin >> n >> k;
vector<int> cg[16], list[16];
for (int i = 0; i < n; ++i) {
int c, g;
cin >> c >> g;
cg[g].push_back(c);
}
for (int i = 0; i < 10; ++i) {
if (cg[i].empty())
continue;
sort(cg[i].begin(), cg[i].end(), greater<int>());
list[i].push_back(cg[i][0]);
for (int i2 = 1; i2 < cg[i].size(); ++i2) {
int sum = 0, cnt = 0;
for (int j = i2; j >= 0; --j) {
sum += cg[i][j];
cnt++;
}
list[i].push_back(sum + (cnt - 1) * cnt);
}
}
for (int i = 0; i < 10; ++i) {
if (list[i].empty())
continue;
for (int j = k; j > 0; --j) {
for (int i2 = 0; i2 < list[i].size() && j - (i2 + 1) >= 0; ++i2) {
dp[j] = max(dp[j - (i2 + 1)] + list[i][i2], dp[j]);
}
}
}
cout << dp[k] << endl;
return 0;
}
|
#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 10 << 10
int dp[2048] = {0};
int main() {
int n, k;
cin >> n >> k;
vector<int> cg[16], list[16];
for (int i = 0; i < n; ++i) {
int c, g;
cin >> c >> g;
cg[g].push_back(c);
}
for (int i = 1; i <= 10; ++i) {
if (cg[i].empty())
continue;
sort(cg[i].begin(), cg[i].end(), greater<int>());
list[i].push_back(cg[i][0]);
for (int i2 = 1; i2 < cg[i].size(); ++i2) {
int sum = 0, cnt = 0;
for (int j = i2; j >= 0; --j) {
sum += cg[i][j];
cnt++;
}
list[i].push_back(sum + (cnt - 1) * cnt);
}
}
for (int i = 1; i <= 10; ++i) {
if (list[i].empty())
continue;
for (int j = k; j >= 0; --j) {
for (int i2 = 0; i2 < list[i].size() && j - (i2 + 1) >= 0; ++i2) {
dp[j] = max(dp[j - (i2 + 1)] + list[i][i2], dp[j]);
}
}
}
cout << dp[k] << endl;
return 0;
}
|
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 47], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 20]]
| 1
| 380
|
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 9000000000000000000
#define INF 2000000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;
class Union_find {
private:
vector<int> par;
vector<int> rank;
int n;
public:
Union_find(int a) {
n = a;
for (int i = 0; i < n; i++) {
par.push_back(i);
rank.push_back(0);
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
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]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
int main(void) {
int N, K;
vector<int> g[10];
vector<ll> sum[10];
ll dp[10][2020] = {};
ll res = 0;
cin >> N >> K;
REP(i, N) {
int C, G;
cin >> C >> G;
g[G - 1].push_back(C);
}
REP(i, 10) {
sum[i].push_back(0);
sort(all(g[i]), greater<int>());
REP(j, g[i].size()) { sum[i].push_back(sum[i][j] + g[i][j] + 2 * j); }
}
REP(i, sum[i].size()) { dp[0][i] = sum[0][i]; }
REP(i, 9) {
REP(j, K) {
REP(k, sum[i + 1].size()) {
if (k - 1 > j)
break;
dp[i + 1][j + 1] =
max(dp[i + 1][j + 1], dp[i][j - k + 1] + sum[i + 1][k]);
}
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
}
}
cout << dp[9][K] << endl;
}
|
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 9000000000000000000
#define INF 2000000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;
class Union_find {
private:
vector<int> par;
vector<int> rank;
int n;
public:
Union_find(int a) {
n = a;
for (int i = 0; i < n; i++) {
par.push_back(i);
rank.push_back(0);
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
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]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
int main(void) {
int N, K;
vector<int> g[10];
vector<ll> sum[10];
ll dp[10][2020] = {};
ll res = 0;
cin >> N >> K;
REP(i, N) {
int C, G;
cin >> C >> G;
g[G - 1].push_back(C);
}
REP(i, 10) {
sum[i].push_back(0);
sort(all(g[i]), greater<int>());
REP(j, g[i].size()) { sum[i].push_back(sum[i][j] + g[i][j] + 2 * j); }
}
REP(i, sum[0].size()) { dp[0][i] = sum[0][i]; }
REP(i, 9) {
REP(j, K) {
REP(k, sum[i + 1].size()) {
if (k - 1 > j)
break;
dp[i + 1][j + 1] =
max(dp[i + 1][j + 1], dp[i][j - k + 1] + sum[i + 1][k]);
}
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
}
}
cout << dp[9][K] << endl;
}
|
[["-", 0, 2, 63, 118, 28, 69, 341, 342, 0, 22], ["+", 0, 2, 63, 118, 28, 69, 341, 342, 0, 13]]
| 1
| 638
|
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define DBG 1
#define dump(o) \
if (DBG) { \
cerr << #o << " " << o << endl; \
}
#define dumpc(o) \
if (DBG) { \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
void print_vv(vector<vector<int>> v) {
rep(i, 0, v.size()) {
rep(j, 0, v[i].size()) { cout << v[i][j] << " "; }
cout << endl;
}
}
void print_v(vector<int> v) {
rep(i, 0, v.size()) cout << v[i] << " ";
cout << endl;
}
int N, K;
vector<vector<int>> table(11, vector<int>(1, 0));
vector<vector<int>> dp(11, vector<int>(2048, -1));
int dfs(int n, int k) {
if (n == 10)
return 0;
if (dp[n][k] != -1)
return dp[n][k];
int res(0);
rep(i, 0, table[n].size()) {
if (k + i > K)
break;
res = max(res, dfs(n + 1, k + i) + table[n][i]);
}
return dp[n][k] = res;
}
signed main() {
cin >> N >> K;
vector<int> v(N), kind(N), num(11, 0);
rep(i, 0, N) {
cin >> v[i] >> kind[i];
table[--kind[i]].push_back(v[i]);
num[kind[i]]++;
}
rep(i, 1, 11) {
sort(table[i].begin() + 1, table[i].end(), greater<int>());
// print_v(table[i]);
rep(j, 1, table[i].size()) { table[i][j] += table[i][j - 1]; }
// print_v(table[i]);
rep(j, 1, table[i].size()) { table[i][j] += (j - 1) * j; }
// print_v(table[i]);
}
// print_vv(table);
// vector<vector<int>>dp(table);
// rep(i, 1, 10) {
// rep(j, 1, k + 1) {
// if (dp[i][j] == 0)break;
// for (int l = 0; l + j < k + 1; l++) {
// bool f1(false), f2(false);
// if (dp[i + 1][j + l] == 0)f1 = true;
// if (dp[i + 1][j + l] < dp[i][j] + dp[i + 1][l])f2 =
//true; dp[i + 1][j + l] = max(dp[i + 1][j + l], dp[i][j] + dp[i + 1][l]); if
//(f1 || f2)break;
// }
// //print_vv(dp);
// }
//}
cout << dfs(0, 0) << endl;
return 0;
}
|
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define DBG 1
#define dump(o) \
if (DBG) { \
cerr << #o << " " << o << endl; \
}
#define dumpc(o) \
if (DBG) { \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
void print_vv(vector<vector<int>> v) {
rep(i, 0, v.size()) {
rep(j, 0, v[i].size()) { cout << v[i][j] << " "; }
cout << endl;
}
}
void print_v(vector<int> v) {
rep(i, 0, v.size()) cout << v[i] << " ";
cout << endl;
}
int N, K;
vector<vector<int>> table(11, vector<int>(1, 0));
vector<vector<int>> dp(11, vector<int>(2048, -1));
int dfs(int n, int k) {
if (n == 10)
return 0;
if (dp[n][k] != -1)
return dp[n][k];
int res(0);
rep(i, 0, table[n].size()) {
if (k + i > K)
break;
res = max(res, dfs(n + 1, k + i) + table[n][i]);
}
return dp[n][k] = res;
}
signed main() {
cin >> N >> K;
vector<int> v(N), kind(N), num(11, 0);
rep(i, 0, N) {
cin >> v[i] >> kind[i];
table[--kind[i]].push_back(v[i]);
num[kind[i]]++;
}
rep(i, 0, 11) {
sort(table[i].begin() + 1, table[i].end(), greater<int>());
// print_v(table[i]);
rep(j, 1, table[i].size()) { table[i][j] += table[i][j - 1]; }
// print_v(table[i]);
rep(j, 1, table[i].size()) { table[i][j] += (j - 1) * j; }
// print_v(table[i]);
}
// print_vv(table);
// vector<vector<int>>dp(table);
// rep(i, 1, 10) {
// rep(j, 1, k + 1) {
// if (dp[i][j] == 0)break;
// for (int l = 0; l + j < k + 1; l++) {
// bool f1(false), f2(false);
// if (dp[i + 1][j + l] == 0)f1 = true;
// if (dp[i + 1][j + l] < dp[i][j] + dp[i + 1][l])f2 =
//true; dp[i + 1][j + l] = max(dp[i + 1][j + l], dp[i][j] + dp[i + 1][l]); if
//(f1 || f2)break;
// }
// //print_vv(dp);
// }
//}
cout << dfs(0, 0) << endl;
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
| 1
| 583
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 2005;
vector<int> a[10];
vector<int> b[10];
int dp[10][MAX_N];
int n, K;
int main() {
int x, y;
scanf("%d%d", &n, &K);
rep(i, n) {
scanf("%d%d", &x, &y);
a[y - 1].push_back(x);
}
rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
rep(i, 10) {
int suma = 0;
rep(j, a[i].size()) {
suma += a[i][j] + 2 * j;
b[i].push_back(suma);
}
}
rep(j, K + 1) { dp[0][j] = 0; }
rep(i, 10) {
rep(j, K + 1) {
dp[i + 1][j] = dp[i][j];
for (int k = 1; k <= min((int)b[i].size(), j); k++) {
dp[i + 1][j] =
max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
}
}
}
printf("%d\n", dp[10][K]);
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 2005;
vector<int> a[11];
vector<int> b[11];
int dp[11][MAX_N];
int n, K;
int main() {
int x, y;
scanf("%d%d", &n, &K);
rep(i, n) {
scanf("%d%d", &x, &y);
a[y - 1].push_back(x);
}
rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
rep(i, 10) {
int suma = 0;
rep(j, a[i].size()) {
suma += a[i][j] + 2 * j;
b[i].push_back(suma);
}
}
rep(j, K + 1) { dp[0][j] = 0; }
rep(i, 10) {
rep(j, K + 1) {
dp[i + 1][j] = dp[i][j];
for (int k = 1; k <= min((int)b[i].size(), j); k++) {
dp[i + 1][j] =
max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
}
}
}
printf("%d\n", dp[10][K]);
return 0;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13]]
| 1
| 381
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int c, g, dp[2001];
vector<int> bks[11];
for (int i = 0; i < n; ++i) {
cin >> c >> g;
bks[g].push_back(c);
}
for (int i = 0; i < 10; ++i) {
sort(bks[i].begin(), bks[i].end(), greater<int>());
}
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 0; i < 10; ++i) {
if (bks[i].empty())
continue;
for (int j = k - 1; j >= 0; --j) {
if (dp[j] == -1)
continue;
int sum = 0;
for (int l = 0; l < bks[i].size() && j + l < k; ++l) {
sum += bks[i][l];
dp[j + l + 1] = max(dp[j + l + 1], dp[j] + sum + (l + 1) * l);
}
}
}
cout << dp[k] << endl;
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int c, g, dp[2001];
vector<int> bks[11];
for (int i = 0; i < n; ++i) {
cin >> c >> g;
bks[g - 1].push_back(c);
}
for (int i = 0; i < 10; ++i) {
sort(bks[i].begin(), bks[i].end(), greater<int>());
}
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 0; i < 10; ++i) {
if (bks[i].empty())
continue;
for (int j = k - 1; j >= 0; --j) {
if (dp[j] == -1)
continue;
int sum = 0;
for (int l = 0; l < bks[i].size() && j + l < k; ++l) {
sum += bks[i][l];
dp[j + l + 1] = max(dp[j + l + 1], dp[j] + sum + (l + 1) * l);
}
}
}
cout << dp[k] << endl;
return 0;
}
|
[["+", 63, 118, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 63, 118, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 293
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
using namespace std;
int n, k;
int dp[11][2001];
int main() {
vector<int> vcc[11];
vector<int> vc[11];
scanf("%d %d", &n, &k);
int a, b;
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
vcc[b].push_back(a);
}
for (int i = 1; i < 11; i++) {
sort(vcc[i].begin(), vcc[i].end(), greater<int>());
for (int j = 0; j <= vcc[i].size(); j++) {
int aa = 0;
for (int l = 0; l < j; l++) {
aa += vcc[i][l] + j - 1;
}
vc[i].push_back(aa);
}
}
fill(&dp[0][0], &dp[10][2000], -1);
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
if (j == 0) {
dp[i][j] = 0;
continue;
}
int m = dp[i - 1][j];
for (int l = 0; l < vc[i].size(); l++) {
if (j - l < 0) {
break;
}
if (dp[i - 1][j - l] == -1) {
continue;
}
int a = dp[i - 1][j - l] + vc[i][l];
if (a > m) {
m = a;
}
}
dp[i][j] = m;
}
}
printf("%d\n", dp[n][k]);
}
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
using namespace std;
int n, k;
int dp[11][2001];
int main() {
vector<int> vcc[11];
vector<int> vc[11];
scanf("%d %d", &n, &k);
int a, b;
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
vcc[b].push_back(a);
}
for (int i = 1; i < 11; i++) {
sort(vcc[i].begin(), vcc[i].end(), greater<int>());
for (int j = 0; j <= vcc[i].size(); j++) {
int aa = 0;
for (int l = 0; l < j; l++) {
aa += vcc[i][l] + j - 1;
}
vc[i].push_back(aa);
}
}
fill(&dp[0][0], &dp[10][2000], -1);
dp[0][0] = 0;
for (int i = 1; i <= 10; i++) {
for (int j = 0; j <= k; j++) {
if (j == 0) {
dp[i][j] = 0;
continue;
}
int m = dp[i - 1][j];
for (int l = 0; l < vc[i].size(); l++) {
if (j - l < 0) {
break;
}
if (dp[i - 1][j - l] == -1) {
continue;
}
int a = dp[i - 1][j - l] + vc[i][l];
if (a > m) {
m = a;
}
}
dp[i][j] = m;
}
}
printf("%d\n", dp[10][k]);
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 13]]
| 1
| 433
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <vector>
using namespace std;
#define INF (1 << 30)
long long int dp[11][2012], ans;
vector<long long int> books[11];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
int c, g;
scanf("%d%d", &c, &g);
books[g].push_back(c);
}
for (int i = 1; i <= 10; i++) {
sort(books[i].begin(), books[i].end(), greater<int>());
for (int j = 1; j < books[i].size(); j++) {
books[i][j] += books[i][j - 1];
}
for (int j = 0; j < books[i].size(); j++) {
books[i][j] += j * (j + 1);
}
}
for (int i = 0; i <= 10; i++) {
for (int j = 0; j < 2012; j++) {
dp[i][j] = -INF;
}
}
dp[0][0] = 0;
for (int i = 1; i <= 10; i++) {
dp[i][0] = 0;
for (int j = 1; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
for (int l = 1; l <= j && l < books[i].size(); l++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - l] + books[i][l - 1]);
}
ans = max(ans, dp[i][j]);
}
}
printf("%lld\n", ans);
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <vector>
using namespace std;
#define INF (1 << 30)
int dp[11][2012], ans;
vector<int> books[11];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
int c, g;
scanf("%d%d", &c, &g);
books[g].push_back(c);
}
for (int i = 1; i <= 10; i++) {
sort(books[i].begin(), books[i].end(), greater<int>());
for (int j = 1; j < books[i].size(); j++) {
books[i][j] += books[i][j - 1];
}
for (int j = 0; j < books[i].size(); j++) {
books[i][j] += j * (j + 1);
}
}
for (int i = 0; i <= 10; i++) {
for (int j = 0; j < 2012; j++) {
dp[i][j] = -INF;
}
}
dp[0][0] = 0;
for (int i = 1; i <= 10; i++) {
dp[i][0] = 0;
for (int j = 1; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
for (int l = 1; l <= j && l <= books[i].size(); l++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - l] + books[i][l - 1]);
}
ans = max(ans, dp[i][j]);
}
}
printf("%d\n", ans);
}
|
[["-", 36, 36, 0, 30, 0, 43, 39, 86, 0, 96], ["-", 39, 344, 3, 347, 0, 77, 39, 86, 0, 96], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 18], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 19], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
| 1
| 439
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
typedef pair<p, p> p2;
#define pu push
#define pb push_back
#define mp make_pair
#define INF 100000000
int value[2001];
int jan[2001];
vector<int> vec[11];
vector<int> rui[11];
int dp[2005] = {};
int main() {
int n;
int k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &value[i]);
scanf("%d", &jan[i]);
}
for (int i = 0; i < n; i++) {
vec[jan[i]--].pb(value[i]);
}
for (int i = 0; i < 10; i++) {
sort(vec[i].begin(), vec[i].end(), greater<int>());
}
for (int i = 0; i < 10; i++) {
rui[i] = vector<int>(vec[i].size());
if (vec[i].size() > 0) {
int rp = vec[i].size();
rui[i][0] = vec[i][0];
for (int w = 1; w < rp; w++) {
rui[i][w] = rui[i][w - 1] + vec[i][w] + 2 * w;
}
for (int w = k; w >= 0; w--) {
for (int l = 0; l < min(w, rp); l++) {
dp[w] = max(dp[w], dp[w - l - 1] + rui[i][l]);
}
}
}
}
printf("%d\n", dp[k]);
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
typedef pair<p, p> p2;
#define pu push
#define pb push_back
#define mp make_pair
#define INF 100000000
int value[2001];
int jan[2001];
vector<int> vec[11];
vector<int> rui[11];
int dp[2005] = {};
int main() {
int n;
int k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &value[i]);
scanf("%d", &jan[i]);
}
for (int i = 0; i < n; i++) {
vec[jan[i] - 1].pb(value[i]);
}
for (int i = 0; i < 10; i++) {
sort(vec[i].begin(), vec[i].end(), greater<int>());
}
for (int i = 0; i < 10; i++) {
rui[i] = vector<int>(vec[i].size());
if (vec[i].size() > 0) {
int rp = vec[i].size();
rui[i][0] = vec[i][0];
for (int w = 1; w < rp; w++) {
rui[i][w] = rui[i][w - 1] + vec[i][w] + 2 * w;
}
for (int w = k; w >= 0; w--) {
for (int l = 0; l < min(w, rp); l++) {
dp[w] = max(dp[w], dp[w - l - 1] + rui[i][l]);
}
}
}
}
printf("%d\n", dp[k]);
return 0;
}
|
[["-", 63, 118, 28, 69, 341, 342, 0, 27, 17, 68], ["+", 63, 118, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 63, 118, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 456
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
typedef vector<int> VI;
int n, k;
VI topic[10];
int DP[10][2001];
int main() {
int i, t, u, qsize[10] = {0};
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> t >> u;
u--;
topic[u].push_back(t);
qsize[u]++;
}
for (i = 0; i < 10; i++) {
for (int j = 0; j < 2001; j++) {
DP[i][j] = -1;
}
sort(topic[i].begin(), topic[i].end(), greater<int>());
}
DP[0][0] = 0;
//ジャンル番号の低いほうから売る本の数(0<=x<=size)を考える。
//売った本の冊数と、今から売ることが可能な冊数が同じならば、
//その時の売り上げが最大のものをとってみる。
for (i = 0; i < 9; i++) {
for (int j = 0; j <= k; j++) {
if (DP[i][j] == -1)
continue;
int gokei;
gokei = 0;
for (int l = 0; l <= qsize[i] && j + l <= k; l++) {
if (DP[i + 1][j + l] < DP[i][j] + gokei + (l > 1 ? l * l - l : 0)) {
DP[i + 1][j + l] = DP[i][j] + gokei + (l > 1 ? l * l - l : 0);
}
if (l < qsize[i])
gokei += topic[i][l];
}
}
}
//あとはk冊売った集合から最大値を貪欲法で探す
int saidai = 0;
for (i = 0; i < 10; i++) {
if (saidai < DP[i][k])
saidai = DP[i][k];
}
cout << saidai << endl;
return 0;
}
|
//問題文をしっかり読む!。最後と空っぽに注意!メモリをしっかり確保すること。
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
typedef vector<int> VI;
int n, k;
VI topic[11];
int DP[11][2001];
int main() {
int i, t, u, qsize[10] = {0};
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> t >> u;
u--;
topic[u].push_back(t);
qsize[u]++;
}
for (i = 0; i < 11; i++) {
for (int j = 0; j < 2001; j++) {
DP[i][j] = -1;
}
sort(topic[i].begin(), topic[i].end(), greater<int>());
}
DP[0][0] = 0;
//ジャンル番号の低いほうから売る本の数(0<=x<=size)を考える。
//売った本の冊数と、今から売ることが可能な冊数が同じならば、
//その時の売り上げが最大のものをとってみる。
for (i = 0; i < 10; i++) {
for (int j = 0; j <= k; j++) {
if (DP[i][j] == -1)
continue;
int gokei;
gokei = 0;
for (int l = 0; l <= qsize[i] && j + l <= k; l++) {
if (DP[i + 1][j + l] < DP[i][j] + gokei + (l > 1 ? l * l - l : 0)) {
DP[i + 1][j + l] = DP[i][j] + gokei + (l > 1 ? l * l - l : 0);
}
if (l < qsize[i])
gokei += topic[i][l];
}
}
}
//あとはk冊売った集合から最大値を貪欲法で探す
int saidai = 0;
for (i = 0; i < 11; i++) {
if (saidai < DP[i][k])
saidai = DP[i][k];
}
cout << saidai << endl;
return 0;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
| 1
| 413
|
### constant
MAXGE = 10
### main
n, k = gets.strip.split(' ').map{|s| s.to_i}
books = MAXGE.times.map{[]}
nge = 0
n.times do
ci, gi = gets.strip.split(' ').map{|s| s.to_i}
books[gi - 1] << ci
nge = gi if nge < gi
end
for g in (1...nge)
books[g].sort!{|a, b| b <=> a}
end
#p nge
#p books
costs = [0]
for g in (0...nge)
bs = books[g]
bslen = bs.length
costs0 = costs.clone
clen = costs.length
csum = 0
for nbk in (0...bslen)
csum += bs[nbk]
csum0 = csum + nbk * (nbk + 1)
for i in (0...clen)
j = i + nbk + 1
break if j > k
nc = costs[i] + csum0
if costs0[j].nil? || costs0[j] < nc
costs0[j] = nc
end
end
end
costs = costs0
end
#p costs
puts costs[k]
|
### constant
MAXGE = 10
### main
n, k = gets.strip.split(' ').map{|s| s.to_i}
books = MAXGE.times.map{[]}
nge = 0
n.times do
ci, gi = gets.strip.split(' ').map{|s| s.to_i}
books[gi - 1] << ci
nge = gi if nge < gi
end
for g in (0...nge)
books[g].sort!{|a, b| b <=> a}
end
#p nge
#p books
costs = [0]
for g in (0...nge)
bs = books[g]
bslen = bs.length
costs0 = costs.clone
clen = costs.length
csum = 0
for nbk in (0...bslen)
csum += bs[nbk]
csum0 = csum + nbk * (nbk + 1)
for i in (0...clen)
j = i + nbk + 1
break if j > k
nc = costs[i] + csum0
if costs0[j].nil? || costs0[j] < nc
costs0[j] = nc
end
end
end
costs = costs0
#p [g, costs]
end
puts costs[k]
|
[["-", 0, 88, 51, 267, 0, 739, 0, 475, 756, 612], ["+", 0, 88, 51, 267, 0, 739, 0, 475, 756, 612]]
| 4
| 237
|
def knapsack(items, budget)
table = (0..1).map{ Array.new(budget+1, 0) }
for i in 0..items.size-1
c, v = items[i]
for j in 0..budget
x = table[0][j]
y = (j >= c ? table[0][j-c] + v : 0)
table[1][j] = x > y ? x : y
end
table << table.shift
end
table[0].max
end
n, k = gets.split.map(&:to_i)
hash = {}
n.times {
price, kind = gets.split.map(&:to_i)
hash[kind] ||= []
hash[kind] << price
}
hash.each{|kind, prices|
i = 0
hash[kind] = prices
.sort
.reverse
.inject([0]){|a, p|
i += 1
a + [a[-1] + p + 2 * i - 2]
}
}
a = hash.values
table = (0..1).map{ Array.new(k+1, 0) }
for i in 0..a.size-1
for j in 0..k
table[1][j] =
(0..[a[i].size-1, j].min)
.map{|l| table[0][j-l] + a[i][l]}
.max
end
p table[1]
table << table.shift
end
p table[0][k]
|
def knapsack(items, budget)
table = (0..1).map{ Array.new(budget+1, 0) }
for i in 0..items.size-1
c, v = items[i]
for j in 0..budget
x = table[0][j]
y = (j >= c ? table[0][j-c] + v : 0)
table[1][j] = x > y ? x : y
end
table << table.shift
end
table[0].max
end
n, k = gets.split.map(&:to_i)
hash = {}
n.times {
price, kind = gets.split.map(&:to_i)
hash[kind] ||= []
hash[kind] << price
}
hash.each{|kind, prices|
i = 0
hash[kind] = prices
.sort
.reverse
.inject([0]){|a, p|
i += 1
a + [a[-1] + p + 2 * i - 2]
}
}
a = hash.values
table = (0..1).map{ Array.new(k+1, 0) }
for i in 0..a.size-1
for j in 0..k
table[1][j] =
(0..[a[i].size-1, j].min)
.map{|l| table[0][j-l] + a[i][l]}
.max
end
table << table.shift
end
p table[0][k]
|
[["-", 0, 493, 0, 88, 8, 170, 0, 652, 735, 22], ["-", 8, 170, 0, 652, 3, 4, 0, 742, 0, 70], ["-", 8, 170, 0, 652, 3, 4, 0, 742, 0, 612], ["-", 8, 170, 0, 652, 3, 4, 0, 742, 0, 73], ["-", 0, 493, 0, 88, 8, 170, 0, 738, 31, 22]]
| 4
| 330
|
n,k=list(map(int,input().split()))
g=[[] for _ in range(10)]
for i in range(n):
v,j=list(map(int,input().split()))
g[j-1].append(v)
for i in range(10):
g[i].sort()
g[i].reverse()
books=[[0]*(k+1) for _ in range(10)]
for i in range(10):
for j in range(1,len(g[i])+1):
books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)
dp=[[0]*(k+1) for _ in range(11)]
for i in range(10):
for j in range(1,k+1):
for l in range(j+1):
dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])
print((dp[10][k]))
|
n,k=list(map(int,input().split()))
g=[[] for _ in range(10)]
for i in range(n):
v,j=list(map(int,input().split()))
g[j-1].append(v)
for i in range(10):
g[i].sort()
g[i].reverse()
books=[[0]*2005 for _ in range(10)]
for i in range(10):
for j in range(1,len(g[i])+1):
books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)
dp=[[0]*(k+1) for _ in range(11)]
for i in range(10):
for j in range(1,k+1):
for l in range(j+1):
dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])
print((dp[10][k]))
|
[["-", 0, 662, 12, 658, 8, 657, 12, 23, 0, 24], ["-", 12, 658, 8, 657, 12, 23, 0, 657, 31, 22], ["-", 12, 658, 8, 657, 12, 23, 0, 657, 17, 72], ["-", 12, 658, 8, 657, 12, 23, 0, 657, 12, 612], ["-", 0, 662, 12, 658, 8, 657, 12, 23, 0, 25], ["+", 0, 1, 0, 662, 12, 658, 8, 657, 12, 612]]
| 5
| 277
|
[N,K]=list(map(int, input().split()))
C=[[] for i in range(11)]
D=[[0 for j in range(K+1)] for i in range(11)]
M=[0 for i in range(11)]
for i in range(N):
c,g=list(map(int, input().split()))
C[g].append(c)
M[g]+=1
for i in range(1,11):
y=sorted(C[i], reverse=True)
for j in range(len(y)):
n = j+1
D[i][n] =y[j] + D[i][n-1] + 2*(n-1)
DP=[[0 for i in range(K+1)] for j in range(11)]
for g in range(1,11):
for k in range(K+1):
DP[g][k] = max( [ DP[g-1][k-x] + D[g][x] for x in range(min( k+1, M[g]+1 ) ) ] + [0] )
print(DP[10][K])
|
[N,K]=list(map(int, input().split()))
C=[[] for i in range(11)]
D=[[0 for j in range(N+1)] for i in range(11)]
M=[0 for i in range(11)]
for i in range(N):
c,g=list(map(int, input().split()))
C[g].append(c)
M[g]+=1
for i in range(1,11):
y=sorted(C[i], reverse=True)
for j in range(len(y)):
n = j+1
D[i][n] =y[j] + D[i][n-1] + 2*(n-1)
DP=[[0 for i in range(K+1)] for j in range(11)]
for g in range(1,11):
for k in range(K+1):
DP[g][k] = max( [ DP[g-1][k-x] + D[g][x] for x in range(min( k+1, M[g]+1 ) ) ] + [0] )
print(DP[10][K])
|
[["-", 0, 659, 12, 652, 3, 4, 0, 657, 31, 22], ["+", 0, 659, 12, 652, 3, 4, 0, 657, 31, 22]]
| 5
| 288
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define INF (1e9)
#define LLINF (1e18)
#define long long
#define MAX_V 3000
using namespace std;
typedef struct edge {
public:
int to;
double cost;
edge() {}
edge(int _to, double _cost) {
to = _to;
cost = _cost;
}
bool operator<(const edge &a) const {
if (cost == a.cost)
return to > a.to;
return cost > a.cost; // for asc
}
} edge;
double dist[MAX_V + 1];
vector<edge> G[MAX_V + 1];
priority_queue<edge> pque;
int shops[MAX_V + 1];
int V;
int E;
int K;
double ans;
double calc(double x, double y, double e) {
double a;
a = (y - x + e) / 2;
return a;
}
void solve() {
for (int i = 0; i < MAX_V + 1; i++)
dist[i] = INF;
for (int i = 0; i < K; i++) {
pque.push(edge(shops[i], 1));
}
#ifndef EVAL
// puts("###DBG1");
#endif
while (!pque.empty()) {
edge e = pque.top();
pque.pop();
if (dist[e.to] != INF)
continue;
dist[e.to] = e.cost;
for (int i = 0; i < G[e.to].size(); i++) {
int _to = G[e.to][i].to;
double _cost = G[e.to][i].cost + e.cost;
pque.push(edge(_to, _cost));
}
}
#ifndef EVAL
// puts("###DBG2");
#endif
ans = 0;
for (int i = 1; i < V + 1; i++) {
for (int j = 0; j < G[i].size(); j++) {
// printf("###%lf\n",dist[i] +
// calc(dist[i],dist[G[i][j].to],G[i][j].cost));
ans = max(ans, dist[i] + calc(dist[i], dist[G[i][j].to], G[i][j].cost));
}
}
return;
}
int main() {
scanf("%d%d%d", &V, &E, &K);
int _u, _v;
double _cost;
for (int i = 0; i < E; i++) {
scanf("%d%d", &_u, &_v);
scanf("%lf", &_cost);
G[_u].push_back(edge(_v, _cost));
G[_v].push_back(edge(_u, _cost));
}
for (int i = 0; i < K; i++) {
scanf("%d", shops + i);
}
#ifndef EVAL
// puts("###DBG");
#endif
solve();
// printf("***%lf\n",ans);
printf("%d\n", (int)(ans));
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define INF (1e9)
#define LLINF (1e18)
#define long long
#define MAX_V 3000
using namespace std;
typedef struct edge {
public:
int to;
double cost;
edge() {}
edge(int _to, double _cost) {
to = _to;
cost = _cost;
}
bool operator<(const edge &a) const {
if (cost == a.cost)
return to > a.to;
return cost > a.cost; // for asc
}
} edge;
double dist[MAX_V + 1];
vector<edge> G[MAX_V + 1];
priority_queue<edge> pque;
int shops[MAX_V + 1];
int V;
int E;
int K;
double ans;
double calc(double x, double y, double e) {
double a;
a = (y - x + e) / 2;
return a;
}
void solve() {
for (int i = 0; i < MAX_V + 1; i++)
dist[i] = INF;
for (int i = 0; i < K; i++) {
pque.push(edge(shops[i], 0));
}
#ifndef EVAL
// puts("###DBG1");
#endif
while (!pque.empty()) {
edge e = pque.top();
pque.pop();
if (dist[e.to] != INF)
continue;
dist[e.to] = e.cost;
for (int i = 0; i < G[e.to].size(); i++) {
int _to = G[e.to][i].to;
double _cost = G[e.to][i].cost + e.cost;
pque.push(edge(_to, _cost));
}
}
#ifndef EVAL
// puts("###DBG2");
#endif
ans = 0;
for (int i = 1; i < V + 1; i++) {
for (int j = 0; j < G[i].size(); j++) {
// printf("###%lf\n",dist[i] +
// calc(dist[i],dist[G[i][j].to],G[i][j].cost));
ans = max(ans, dist[i] + calc(dist[i], dist[G[i][j].to], G[i][j].cost));
}
}
return;
}
int main() {
scanf("%d%d%d", &V, &E, &K);
int _u, _v;
double _cost;
for (int i = 0; i < E; i++) {
scanf("%d%d", &_u, &_v);
scanf("%lf", &_cost);
G[_u].push_back(edge(_v, _cost));
G[_v].push_back(edge(_u, _cost));
}
for (int i = 0; i < K; i++) {
scanf("%d", shops + i);
}
#ifndef EVAL
// puts("###DBG");
#endif
solve();
// printf("***%lf\n",ans);
printf("%d\n", (int)(ans + 0.5));
return 0;
}
|
[["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 3, 4, 0, 74, 51, 23, 0, 16, 17, 72], ["+", 3, 4, 0, 74, 51, 23, 0, 16, 12, 13]]
| 1
| 612
|
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#define INF 500000000
using namespace std;
typedef pair<int, int> P;
int n, m, k;
int l[100100], a[100100], b[100100];
vector<P> g[5000];
int d[5000], w[5000];
int s[5000];
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> l[i];
a[i]--;
b[i]--;
g[a[i]].push_back(P(l[i], b[i]));
g[b[i]].push_back(P(l[i], a[i]));
}
for (int i = 0; i < k; i++) {
cin >> s[i];
s[i]--;
}
for (int i = 0; i < n; i++)
d[i] = INF;
priority_queue<P, vector<P>, greater<P>> q;
for (int i = 0; i < k; i++) {
d[s[i]] = 0;
q.push(P(0, i));
}
while (q.size()) {
P p = q.top();
q.pop();
int cost = p.first, pos = p.second;
for (int i = 0; i < (int)g[pos].size(); i++) {
int dis = cost + g[pos][i].first;
int next = g[pos][i].second;
if (dis < d[next]) {
d[next] = dis;
q.push(P(dis, next));
}
}
}
double ans = 0.0;
for (int i = 0; i < m; i++) {
if (fabs(d[a[i]] - d[b[i]]) >= l[i]) {
ans = max(ans, (double)max(d[a[i]], d[b[i]]));
} else {
ans = max(ans, (double)(l[i] + d[a[i]] + d[b[i]]) / 2);
}
}
cout << (int)(ans + 1e-9 + 0.5) << endl;
}
|
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#define INF 500000000
using namespace std;
typedef pair<int, int> P;
int n, m, k;
int l[100100], a[100100], b[100100];
vector<P> g[5000];
int d[5000], w[5000];
int s[5000];
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> l[i];
a[i]--;
b[i]--;
g[a[i]].push_back(P(l[i], b[i]));
g[b[i]].push_back(P(l[i], a[i]));
}
for (int i = 0; i < k; i++) {
cin >> s[i];
s[i]--;
}
for (int i = 0; i < n; i++)
d[i] = INF;
priority_queue<P, vector<P>, greater<P>> q;
for (int i = 0; i < k; i++) {
d[s[i]] = 0;
q.push(P(0, s[i]));
}
while (q.size()) {
P p = q.top();
q.pop();
int cost = p.first, pos = p.second;
for (int i = 0; i < (int)g[pos].size(); i++) {
int dis = cost + g[pos][i].first;
int next = g[pos][i].second;
if (dis < d[next]) {
d[next] = dis;
q.push(P(dis, next));
}
}
}
double ans = 0.0;
for (int i = 0; i < m; i++) {
if (fabs(d[a[i]] - d[b[i]]) >= l[i]) {
ans = max(ans, (double)max(d[a[i]], d[b[i]]));
} else {
ans = max(ans, (double)(l[i] + d[a[i]] + d[b[i]]) / 2);
}
}
cout << (int)(ans + 1e-9 + 0.5) << endl;
}
|
[["+", 3, 4, 0, 2, 3, 4, 0, 69, 28, 22], ["+", 0, 2, 3, 4, 0, 69, 341, 342, 0, 70], ["+", 0, 2, 3, 4, 0, 69, 341, 342, 0, 73]]
| 1
| 539
|
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000
typedef long long LL;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
vector<edge> G[3390];
int dis[3390];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> Q;
fill(dis, dis + 3389, INF);
dis[s] = 0;
Q.push(P(0, s));
while (Q.size()) {
P p = Q.top();
Q.pop();
int v = p.second;
if (dis[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (dis[e.to] > dis[v] + e.cost) {
dis[e.to] = dis[v] + e.cost;
Q.push(P(dis[e.to], e.to));
}
}
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
G[a - 1].push_back({b - 1, c});
G[b - 1].push_back({a - 1, c});
}
for (int i = 0; i < k; i++) {
int a;
cin >> a;
G[3235].push_back({a - 1, 0});
}
dijkstra(3235);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < G[i].size(); j++) {
ans = max(ans, max(dis[i], dis[G[i][j].to]));
int ct = 0;
ct = G[i][j].cost;
ct += dis[i];
ct += dis[G[i][j].to];
ct = ct + 1 / 2;
ans = max(ans, ct);
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000
typedef long long LL;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
vector<edge> G[3390];
int dis[3390];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> Q;
fill(dis, dis + 3389, INF);
dis[s] = 0;
Q.push(P(0, s));
while (Q.size()) {
P p = Q.top();
Q.pop();
int v = p.second;
if (dis[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (dis[e.to] > dis[v] + e.cost) {
dis[e.to] = dis[v] + e.cost;
Q.push(P(dis[e.to], e.to));
}
}
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
G[a - 1].push_back({b - 1, c});
G[b - 1].push_back({a - 1, c});
}
for (int i = 0; i < k; i++) {
int a;
cin >> a;
G[3235].push_back({a - 1, 0});
}
dijkstra(3235);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < G[i].size(); j++) {
ans = max(ans, max(dis[i], dis[G[i][j].to]));
int ct = 0;
ct = G[i][j].cost;
ct += dis[i];
ct += dis[G[i][j].to];
ct++;
ct /= 2;
ans = max(ans, ct);
}
}
cout << ans << endl;
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35], ["-", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 0, 1, 0, 11, 12, 16, 12, 16, 31, 13], ["-", 0, 1, 0, 11, 12, 16, 12, 16, 17, 85], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 90]]
| 1
| 505
|
#include <bits/stdc++.h>
#define N 3002
using namespace std;
typedef pair<int, int> P;
vector<P> G[N];
bool num[N];
int n, m, K;
double dijkstra() {
vector<int> D(N, 1e9);
priority_queue<P, vector<P>, greater<P>> Q;
for (int i = 0; i < n; i++)
if (num[i])
Q.push(P(0, i)), D[i] = 0;
while (!Q.empty()) {
P t = Q.top();
Q.pop();
int cost = t.first;
int pos = t.second;
if (D[pos] < cost)
continue;
for (int i = 0; i < G[pos].size(); i++) {
int npos = G[pos][i].first;
int ncost = cost + G[pos][i].second;
if (D[npos] <= ncost)
continue;
Q.push(P(ncost, pos));
D[npos] = ncost;
}
}
double res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < (int)G[i].size(); j++) {
int npos = G[i][j].first;
int ncost = G[i][j].second;
res = max(res, (D[i] + D[npos] + ncost) / 2.0);
}
return res;
}
int main() {
cin >> n >> m >> K;
for (int i = 0, a, b, c; i < m; i++) {
cin >> a >> b >> c;
G[a - 1].push_back(P(b - 1, c));
G[b - 1].push_back(P(a - 1, c));
}
for (int i = 0, a; i < K; i++)
cin >> a, num[a - 1] = 1;
printf("%d\n", (int)(dijkstra() + 0.5));
return 0;
}
|
#include <bits/stdc++.h>
#define N 3002
using namespace std;
typedef pair<int, int> P;
vector<P> G[N];
bool num[N];
int n, m, K;
double dijkstra() {
vector<int> D(N, 1e9);
priority_queue<P, vector<P>, greater<P>> Q;
for (int i = 0; i < n; i++)
if (num[i])
Q.push(P(0, i)), D[i] = 0;
while (!Q.empty()) {
P t = Q.top();
Q.pop();
int cost = t.first;
int pos = t.second;
if (D[pos] < cost)
continue;
for (int i = 0; i < G[pos].size(); i++) {
int npos = G[pos][i].first;
int ncost = cost + G[pos][i].second;
if (D[npos] <= ncost)
continue;
Q.push(P(ncost, npos));
D[npos] = ncost;
}
}
double res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < (int)G[i].size(); j++) {
int npos = G[i][j].first;
int ncost = G[i][j].second;
res = max(res, (D[i] + D[npos] + ncost) / 2.0);
}
return res;
}
int main() {
cin >> n >> m >> K;
for (int i = 0, a, b, c; i < m; i++) {
cin >> a >> b >> c;
G[a - 1].push_back(P(b - 1, c));
G[b - 1].push_back(P(a - 1, c));
}
for (int i = 0, a; i < K; i++)
cin >> a, num[a - 1] = 1;
printf("%d\n", (int)(dijkstra() + 0.5));
return 0;
}
|
[["-", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22]]
| 1
| 475
|
#include <bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int, int> P;
/*int main(){
cout<<8979879<<endl;
}*/
int ri[3001][3001];
int main(void) {
int n, m, k, i, j, mx, t, ans, a[100001], b[100001], l[100001], s[3000],
d[3001];
priority_queue<P, vector<P>, greater<P>> que;
cin >> n >> m >> k;
for (i = 0; i < m; i++)
cin >> a[i] >> b[i] >> l[i];
for (i = 0; i < k; i++)
cin >> s[i];
// for(i=0;i<k;i++) cout<<s[i]<<endl;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
ri[i][j] = M;
for (i = 0; i < m; i++)
ri[a[i]][b[i]] = l[i], ri[b[i]][a[i]] = l[i];
/* for(i=1;i<=n;i++){
for(j=1;j<=n;j++)cout<<ri[i][j]<<' ';
cout<<endl;
}*/
int leng[3001];
for (i = 1; i <= n; i++)
leng[i] = M;
for (i = 0; i < k; i++) {
leng[s[i]] = 0;
que.push(P(0, s[i]));
}
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (leng[v] < p.first)
continue;
for (j = 1; j <= n; j++) {
t = leng[v] + ri[v][j];
// cout<<j<<' '<<leng[j]<<' '<<t<<endl;
if (leng[j] > t) {
leng[j] = t;
// cout<<leng[j]<<' '<<j<<endl;
que.push(P(leng[j], j));
}
}
}
/* for(i=1;i<=n;i++) cout<<leng[i]<<' ';
cout<<endl;*/
ans = 0;
for (i = 1; i <= n; i++) {
int mx = 0, y;
double x;
for (j = 1; j <= n; j++) {
if (ri[i][j] != M) {
mx = max(mx, ri[i][j] + leng[j]);
}
}
x = (double)mx / 2;
// cout<<x<<' '<<mx<<' '<<mi<<endl;
y = round(x);
ans = max(ans, y);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int, int> P;
/*int main(){
cout<<8979879<<endl;
}*/
int ri[3001][3001];
int main(void) {
int n, m, k, i, j, mx, t, ans, a[100001], b[100001], l[100001], s[3000],
d[3001];
priority_queue<P, vector<P>, greater<P>> que;
cin >> n >> m >> k;
for (i = 0; i < m; i++)
cin >> a[i] >> b[i] >> l[i];
for (i = 0; i < k; i++)
cin >> s[i];
// for(i=0;i<k;i++) cout<<s[i]<<endl;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
ri[i][j] = M;
for (i = 0; i < m; i++)
ri[a[i]][b[i]] = l[i], ri[b[i]][a[i]] = l[i];
/* for(i=1;i<=n;i++){
for(j=1;j<=n;j++)cout<<ri[i][j]<<' ';
cout<<endl;
}*/
int leng[3001];
for (i = 1; i <= n; i++)
leng[i] = M;
for (i = 0; i < k; i++) {
leng[s[i]] = 0;
que.push(P(0, s[i]));
}
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (leng[v] < p.first)
continue;
for (j = 1; j <= n; j++) {
t = leng[v] + ri[v][j];
// cout<<j<<' '<<leng[j]<<' '<<t<<endl;
if (leng[j] > t) {
leng[j] = t;
// cout<<leng[j]<<' '<<j<<endl;
que.push(P(leng[j], j));
}
}
}
/* for(i=1;i<=n;i++) cout<<leng[i]<<' ';
cout<<endl;*/
ans = 0;
for (i = 1; i <= n; i++) {
int mx = 0, y;
double x;
for (j = 1; j <= n; j++) {
if (ri[i][j] != M) {
mx = max(mx, ri[i][j] + leng[j] + leng[i]);
}
}
x = (double)mx / 2;
// cout<<x<<' '<<mx<<' '<<mi<<endl;
y = round(x);
ans = max(ans, y);
}
cout << ans << endl;
return 0;
}
|
[["+", 0, 11, 12, 2, 3, 4, 0, 16, 17, 72], ["+", 12, 2, 3, 4, 0, 16, 12, 69, 28, 22], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 70], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 73]]
| 1
| 531
|
// 27
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int t[3001];
fill(t, t + 3001, 1 << 29);
int a[10000][2], l[10000];
for (int i = 0; i < m; i++) {
cin >> a[i][0] >> a[i][1] >> l[i];
}
for (int i = 0; i < k; i++) {
int s;
cin >> s;
t[s] = 0;
}
for (int o = 0; o < n; o++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < 2; j++) {
t[a[i][j]] = min(t[a[i][j]], t[a[i][!j]] + l[i]);
}
}
}
int ll = 0;
for (int i = 0; i < m; i++) {
ll = max(ll, (ll - abs(t[a[i][0]] - t[a[i][1]]) + 1) / 2 +
max(t[a[i][0]], t[a[i][1]]));
}
cout << ll << endl;
return 0;
}
|
// 27
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int t[3001];
fill(t, t + 3001, 1 << 29);
int a[100000][2], l[100000];
for (int i = 0; i < m; i++) {
cin >> a[i][0] >> a[i][1] >> l[i];
}
for (int i = 0; i < k; i++) {
int s;
cin >> s;
t[s] = 0;
}
for (int o = 0; o < n; o++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < 2; j++) {
t[a[i][j]] = min(t[a[i][j]], t[a[i][!j]] + l[i]);
}
}
}
int ll = 0;
for (int i = 0; i < m; i++) {
ll = max(ll, (l[i] - abs(t[a[i][0]] - t[a[i][1]]) + 1) / 2 +
max(t[a[i][0]], t[a[i][1]]));
}
cout << ll << endl;
return 0;
}
|
[["-", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 31, 16, 31, 23, 0, 16, 31, 16, 31, 22], ["+", 31, 23, 0, 16, 31, 16, 31, 69, 28, 22], ["+", 0, 16, 31, 16, 31, 69, 341, 342, 0, 70], ["+", 0, 16, 31, 16, 31, 69, 341, 342, 0, 22], ["+", 0, 16, 31, 16, 31, 69, 341, 342, 0, 73]]
| 1
| 319
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int MAXN = 1000 + 20;
int dp[MAXN][MAXN];
int maze[MAXN][MAXN];
int h, w, n, sx[10], sy[10], temp;
void input() {
memset(maze, -1, sizeof(maze));
cin >> h >> w >> n;
cin.get();
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
char x;
x = cin.get();
if (x == '.')
maze[i][j] = 0;
if (x == 'X')
maze[i][j] = -1;
if (x >= '0' && x <= '9') {
maze[i][j] = x - '0';
sx[x - '0'] = i;
sy[x - '0'] = j;
}
if (x == 'S') {
maze[i][j] = 0;
sx[0] = i;
sy[0] = j;
}
}
cin.get();
}
/* for(int i=1;i<=h;++i){
for(int j=1;j<=w;++j){
cout<<setw(4)<<maze[i][j];
}
cout<<endl;
}*/
}
void bfs(int xx, int yy, int nn) {
queue<int> tx;
queue<int> ty;
tx.push(xx);
ty.push(yy);
while (tx.size() && ty.size()) {
int qx = tx.front(), qy = ty.front();
tx.pop();
ty.pop();
for (int i = 0; i < 4; ++i) {
int x = qx + dx[i], y = qy + dy[i];
if (maze[x][y] == -1 || dp[x][y] >= 0)
continue;
tx.push(x);
ty.push(y);
dp[x][y] = dp[qx][qy] + 1;
if (x == sx[nn] && y == sy[nn]) {
temp = dp[qx][qy];
// cout<<temp<<endl;
break;
}
// cout<<x<<' '<<y<<' '<<dp[x][y]<<endl;
}
}
// cout<<endl;
}
void solve() {
for (int i = 0; i < n; ++i) {
// cout<<temp<<endl;
memset(dp, -1, sizeof(dp));
// cout<<sx[i+1]<<' '<<sy[i+1]<<endl;
dp[sx[i]][sy[i]] = temp;
bfs(sx[i], sy[i], i + 1);
}
cout << dp[sx[n]][sy[n]] + 1 << endl;
}
int main() {
input();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int MAXN = 1000 + 20;
int dp[MAXN][MAXN];
int maze[MAXN][MAXN];
int h, w, n, sx[10], sy[10], temp;
void input() {
memset(maze, -1, sizeof(maze));
cin >> h >> w >> n;
cin.get();
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
char x;
x = cin.get();
if (x == '.')
maze[i][j] = 0;
if (x == 'X')
maze[i][j] = -1;
if (x >= '0' && x <= '9') {
maze[i][j] = x - '0';
sx[x - '0'] = i;
sy[x - '0'] = j;
}
if (x == 'S') {
maze[i][j] = 0;
sx[0] = i;
sy[0] = j;
}
}
cin.get();
}
// for(int i=0;i<10;++i) cout<<sx[i]<<' '<<sy[i]<<endl;
/* for(int i=1;i<=h;++i){
for(int j=1;j<=w;++j){
cout<<setw(4)<<maze[i][j];
}
cout<<endl;
}*/
}
void bfs(int xx, int yy, int nn) {
queue<int> tx;
queue<int> ty;
tx.push(xx);
ty.push(yy);
while (tx.size() && ty.size()) {
int qx = tx.front(), qy = ty.front();
tx.pop();
ty.pop();
for (int i = 0; i < 4; ++i) {
int x = qx + dx[i], y = qy + dy[i];
if (maze[x][y] == -1 || dp[x][y] >= 0)
continue;
tx.push(x);
ty.push(y);
dp[x][y] = dp[qx][qy] + 1;
if (x == sx[nn] && y == sy[nn]) {
temp = dp[qx][qy];
// cout<<temp<<endl;
break;
}
// cout<<x<<' '<<y<<' '<<dp[x][y]<<endl;
}
}
// cout<<endl;
}
void solve() {
for (int i = 0; i < n; ++i) {
// cout<<temp<<endl;
memset(dp, -1, sizeof(dp));
// cout<<sx[i+1]<<' '<<sy[i+1]<<endl;
dp[sx[i]][sy[i]] = temp;
bfs(sx[i], sy[i], i + 1);
}
cout << dp[sx[n]][sy[n]] + n - 1 << endl;
}
int main() {
input();
solve();
return 0;
}
|
[["+", 0, 16, 31, 16, 12, 16, 31, 16, 12, 22], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33]]
| 1
| 602
|
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int map[1002][1002], was[1002][1002];
int main(void) {
int count = 0, H, W, N, i, j;
char c = 0;
pair<int, int> q, a[10];
queue<pair<int, int>> que;
queue<int> quec;
quec.push(0);
scanf("%d %d %d", &H, &W, &N);
for (i = 0; i <= H + 1; i++) {
map[i][0] = -1;
map[i][W + 1] = -1;
was[i][0] = -1; /*?????????????£???????(???)*/
was[i][W + 1] = -1;
}
for (j = 1; j <= W; j++) {
map[0][j] = -1;
map[H + 1][j] = -1; /*?????????????£???????????¨????*/
was[0][j] = -1;
was[H + 1][j] = -1;
}
for (i = 1; i <= H; i++) {
for (
j = 1; j <= W;
j++) /*???????????????(??????????????°???????????????????????????????????¨??°??????????????????*/
{
cin >> c;
if (c == 'X')
map[i][j] = -1;
if (c == 'S') {
q.first = i;
q.second = j;
}
if (c == '1') {
a[1].first = i;
a[1].second = j;
}
if (c == '2') {
a[2].first = i;
a[2].second = j;
}
if (c == '3') {
a[3].first = i;
a[3].second = j;
}
if (c == '4') {
a[4].first = i;
a[4].second = j;
}
if (c == '5') {
a[5].first = i;
a[5].second = j;
}
if (c == '6') {
a[6].first = i;
a[6].second = j;
}
if (c == '7') {
a[7].first = i;
a[7].second = j;
}
if (c == '8') {
a[8].first = i;
a[8].second = j;
}
if (c == '9') {
a[9].first = i;
a[9].second = j;
}
}
}
/*for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cout << map[i][j];
}
cout << endl;
}*/
que.push(q);
i = 1;
while (i <= N) {
count = quec.front();
quec.pop();
q = que.front();
que.pop();
if (q == a[i]) {
i++;
memset(was, 0, sizeof(was));
while (!que.empty())
que.pop();
while (!quec.empty())
quec.pop();
quec.push(count);
que.push(q);
continue;
}
if (map[q.first][q.second + 1] != -1 && was[q.first][q.second + 1] != -1) {
was[q.first][q.second + 1] = -1;
q.second++;
que.push(q); /*???????????????*/
count++;
quec.push(count);
count--;
q.second--;
}
if (map[q.first][q.second - 1] != -1 && was[q.first][q.second - 1] != -1) {
was[q.first][q.second - 1] = -1;
q.second--;
que.push(q);
count++; /*???????????????*/
quec.push(count);
count--;
q.second--;
}
if (map[q.first + 1][q.second] != -1 && was[q.first + 1][q.second] != -1) {
was[q.first + 1][q.second] = -1;
q.first++;
que.push(q);
count++; /*???????????????*/
quec.push(count);
count--;
q.first--;
}
if (map[q.first - 1][q.second] != -1 && was[q.first - 1][q.second] != -1) {
was[q.first][q.second] = -1;
q.first--;
que.push(q);
count++; /*???????????????*/
quec.push(count);
count--;
q.first++;
}
}
printf("%d\n", count); /*????????£???????????????????¨??????????*/
return 0;
}
|
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int map[1002][1002], was[1002][1002];
int main(void) {
int count = 0, H, W, N, i, j;
char c = 0;
pair<int, int> q, a[10];
queue<pair<int, int>> que;
queue<int> quec;
quec.push(0);
scanf("%d %d %d", &H, &W, &N);
for (i = 0; i <= H + 1; i++) {
map[i][0] = -1;
map[i][W + 1] = -1;
was[i][0] = -1; /*?????????????£???????(???)*/
was[i][W + 1] = -1;
}
for (j = 1; j <= W; j++) {
map[0][j] = -1;
map[H + 1][j] = -1; /*?????????????£???????????¨????*/
was[0][j] = -1;
was[H + 1][j] = -1;
}
for (i = 1; i <= H; i++) {
for (
j = 1; j <= W;
j++) /*???????????????(??????????????°???????????????????????????????????¨??°??????????????????*/
{
cin >> c;
if (c == 'X')
map[i][j] = -1;
if (c == 'S') {
q.first = i;
q.second = j;
}
if (c == '1') {
a[1].first = i;
a[1].second = j;
}
if (c == '2') {
a[2].first = i;
a[2].second = j;
}
if (c == '3') {
a[3].first = i;
a[3].second = j;
}
if (c == '4') {
a[4].first = i;
a[4].second = j;
}
if (c == '5') {
a[5].first = i;
a[5].second = j;
}
if (c == '6') {
a[6].first = i;
a[6].second = j;
}
if (c == '7') {
a[7].first = i;
a[7].second = j;
}
if (c == '8') {
a[8].first = i;
a[8].second = j;
}
if (c == '9') {
a[9].first = i;
a[9].second = j;
}
}
}
/*for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cout << map[i][j];
}
cout << endl;
}*/
que.push(q);
i = 1;
while (i <= N) {
count = quec.front();
quec.pop();
q = que.front();
que.pop();
if (q == a[i]) {
i++;
memset(was, 0, sizeof(was));
while (!que.empty())
que.pop();
while (!quec.empty())
quec.pop();
quec.push(count);
que.push(q);
continue;
}
if (map[q.first][q.second + 1] != -1 && was[q.first][q.second + 1] != -1) {
was[q.first][q.second + 1] = -1;
q.second++;
que.push(q); /*???????????????*/
count++;
quec.push(count);
count--;
q.second--;
}
if (map[q.first][q.second - 1] != -1 && was[q.first][q.second - 1] != -1) {
was[q.first][q.second - 1] = -1;
q.second--;
que.push(q);
count++; /*???????????????*/
quec.push(count);
count--;
q.second++;
}
if (map[q.first + 1][q.second] != -1 && was[q.first + 1][q.second] != -1) {
was[q.first + 1][q.second] = -1;
q.first++;
que.push(q);
count++; /*???????????????*/
quec.push(count);
count--;
q.first--;
}
if (map[q.first - 1][q.second] != -1 && was[q.first - 1][q.second] != -1) {
was[q.first - 1][q.second] = -1;
q.first--;
que.push(q);
count++; /*???????????????*/
quec.push(count);
count--;
q.first++;
}
}
printf("%d\n", count); /*????????£???????????????????¨??????????*/
return 0;
}
|
[["-", 0, 57, 64, 9, 0, 1, 0, 27, 17, 68], ["+", 0, 57, 64, 9, 0, 1, 0, 27, 17, 29], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 1,022
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define P(p) cout << (p) << endl
#define rep(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(), greater<int>());
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define ret return
#define C(i) cin >> i
#define C2(i, j) cin >> i >> j
#define C3(i, j, k) cin >> i >> j >> k
#define C4(i, j, k, m) cin >> i >> j >> k >> m
////////////////////////////////////////////////////////////
struct S {
int num;
int x;
int y;
};
int main() {
int h, w, n;
C3(h, w, n);
int f[h][w] = {};
pair<int, int> g[n + 1];
pair<int, int> s[n + 1];
rep(i, 0, h) {
rep(j, 0, w) {
char c;
cin >> c;
if (c == 'X') {
f[i][j] = -1;
} else if (c == '.') {
f[i][j] = 0;
} else if (c == 'S') {
pair<int, int> tt = {j, i};
s[1] = tt;
} else {
int t = c - '0';
f[i][j] = t;
pair<int, int> tt = {j, i};
g[t] = tt;
if (t < n) {
s[t + 1] = tt;
}
}
}
}
int ans = 0;
rep(i, 1, n + 1) {
int sx = s[i].first;
int sy = s[i].second;
int gx = g[i].first;
int gy = g[i].second;
// printf("sx:%d sy:%d gx:%d gy:%d\n",sx,sy,gx,gy);
queue<struct S> q;
struct S tmp = {0, sx, sy};
q.push(tmp);
int fNum[h][w];
rep(i, 0, h) rep(j, 0, w) fNum[i][j] = 0xFFFFFFFF;
while (!q.empty()) {
struct S s = q.front();
// printf("i:%d n:%d x:%d y:%d\n",i,s.num,s.x,s.y);
q.pop();
if (f[s.y][s.x] == -1) {
continue;
}
if (s.num >= fNum[s.y][s.x] && s.num != 0)
continue;
fNum[s.y][s.x] = s.num;
if (s.y == gy && s.x == gx)
continue;
else {
if (s.y > 0) {
if (f[s.y - 1][s.x] != -1 && fNum[s.y - 1][s.x] > s.num + 1) {
struct S t = {s.num + 1, s.x, s.y - 1};
q.push(t);
}
}
if (s.x + 1 <= w - 1) {
if (f[s.y][s.x + 1] != -1 && fNum[s.y][s.x + 1] > s.num + 1) {
struct S r = {s.num + 1, s.x + 1, s.y};
q.push(r);
}
}
if (s.x > 0) {
if (f[s.y][s.x - 1] != -1 && fNum[s.y][s.x - 1] > s.num + 1) {
struct S l = {s.num + 1, s.x - 1, s.y};
q.push(l);
}
}
if (s.y + 1 <= h - 1) {
if (f[s.y + 1][s.x] != -1 && fNum[s.y + 1][s.x] > s.num + 1) {
struct S b = {s.num + 1, s.x, s.y + 1};
q.push(b);
}
}
}
}
ans += fNum[gy][gx];
}
P(ans);
ret 0;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define P(p) cout << (p) << endl
#define rep(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(), greater<int>());
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define ret return
#define C(i) cin >> i
#define C2(i, j) cin >> i >> j
#define C3(i, j, k) cin >> i >> j >> k
#define C4(i, j, k, m) cin >> i >> j >> k >> m
////////////////////////////////////////////////////////////
struct S {
int num;
int x;
int y;
};
int main() {
int h, w, n;
C3(h, w, n);
int f[h][w] = {};
pair<int, int> g[n + 1];
pair<int, int> s[n + 1];
rep(i, 0, h) {
rep(j, 0, w) {
char c;
cin >> c;
if (c == 'X') {
f[i][j] = -1;
} else if (c == '.') {
f[i][j] = 0;
} else if (c == 'S') {
pair<int, int> tt = {j, i};
s[1] = tt;
} else {
int t = c - '0';
f[i][j] = t;
pair<int, int> tt = {j, i};
g[t] = tt;
if (t < n) {
s[t + 1] = tt;
}
}
}
}
int ans = 0;
rep(i, 1, n + 1) {
int sx = s[i].first;
int sy = s[i].second;
int gx = g[i].first;
int gy = g[i].second;
// printf("sx:%d sy:%d gx:%d gy:%d\n",sx,sy,gx,gy);
queue<struct S> q;
struct S tmp = {0, sx, sy};
q.push(tmp);
int fNum[h][w];
rep(i, 0, h) rep(j, 0, w) fNum[i][j] = 0xFFFFFFF;
while (!q.empty()) {
struct S s = q.front();
// printf("i:%d n:%d x:%d y:%d\n",i,s.num,s.x,s.y);
q.pop();
if (f[s.y][s.x] == -1) {
continue;
}
if (s.num >= fNum[s.y][s.x] && s.num != 0)
continue;
fNum[s.y][s.x] = s.num;
if (s.y == gy && s.x == gx)
continue;
else {
if (s.y > 0) {
if (f[s.y - 1][s.x] != -1 && fNum[s.y - 1][s.x] > s.num + 1) {
struct S t = {s.num + 1, s.x, s.y - 1};
q.push(t);
}
}
if (s.x + 1 <= w - 1) {
if (f[s.y][s.x + 1] != -1 && fNum[s.y][s.x + 1] > s.num + 1) {
struct S r = {s.num + 1, s.x + 1, s.y};
q.push(r);
}
}
if (s.x > 0) {
if (f[s.y][s.x - 1] != -1 && fNum[s.y][s.x - 1] > s.num + 1) {
struct S l = {s.num + 1, s.x - 1, s.y};
q.push(l);
}
}
if (s.y + 1 <= h - 1) {
if (f[s.y + 1][s.x] != -1 && fNum[s.y + 1][s.x] > s.num + 1) {
struct S b = {s.num + 1, s.x, s.y + 1};
q.push(b);
}
}
}
}
ans += fNum[gy][gx];
}
P(ans);
ret 0;
}
|
[["-", 8, 9, 0, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 9, 0, 1, 0, 11, 12, 13]]
| 1
| 928
|
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int h, w, n;
const int MAX_N = 1005;
char map[MAX_N][MAX_N];
int visit[MAX_N][MAX_N];
struct point {
int x, y;
int step;
point() {
x = 0;
y = 0;
step = 0;
}
point(int xx, int yy, int ss) {
x = xx;
y = yy;
step = ss;
}
};
struct point p[16];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int ok(int x, int y) {
if (x >= 0 && x < h && y >= 0 && y < w)
return 1;
return 0;
}
int bfs(int next) {
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_N; j++) {
visit[i][j] = 0;
}
}
int nextcheese = next + 1;
queue<point> q;
point cur(p[next].x, p[next].y, 0);
q.push(cur);
while (q.size()) {
cur = q.front();
q.pop();
int x = cur.x, y = cur.y;
int nx, ny;
for (int i = 0; i < 4; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (ok(nx, ny)) {
if ((map[nx][ny] - '0') == nextcheese)
return ++cur.step;
if ((map[nx][ny] == '.') && visit[nx][ny] == 0) {
q.push(point(nx, ny, cur.step + 1));
visit[nx][ny] = 1;
}
}
}
}
}
void solve() {
cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char ch = map[i][j];
switch (ch) {
case '.':
break;
case 'X':
break;
case 'S':
p[0].x = i;
p[0].y = j;
map[i][j] = '.';
break;
default:
int t = (ch - '0');
p[t].x = i;
p[t].y = j;
}
/* if(map[i][j]=='S'){
p[0].x=i; p[0].y = j;
map[i][j]='.';
}
else if(isdigit(map[i][j])){
int index=map[i][j]-'0';
p[index].x=i; p[index].y=j;
}*/
}
}
int step = 0;
for (int i = 0; i < n; i++) {
step += bfs(i);
}
printf("%d\n", step);
}
int main() { solve(); }
|
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int h, w, n;
const int MAX_N = 1005;
char map[MAX_N][MAX_N];
int visit[MAX_N][MAX_N];
struct point {
int x, y;
int step;
point() {
x = 0;
y = 0;
step = 0;
}
point(int xx, int yy, int ss) {
x = xx;
y = yy;
step = ss;
}
};
struct point p[16];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int ok(int x, int y) {
if (x >= 0 && x < h && y >= 0 && y < w)
return 1;
return 0;
}
int bfs(int next) {
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_N; j++) {
visit[i][j] = 0;
}
}
int nextcheese = next + 1;
queue<point> q;
point cur(p[next].x, p[next].y, 0);
q.push(cur);
while (q.size()) {
cur = q.front();
q.pop();
int x = cur.x, y = cur.y;
int nx, ny;
for (int i = 0; i < 4; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (ok(nx, ny)) {
if ((map[nx][ny] - '0') == nextcheese)
return ++cur.step;
if ((map[nx][ny] != 'X') && visit[nx][ny] == 0) {
q.push(point(nx, ny, cur.step + 1));
visit[nx][ny] = 1;
}
}
}
}
}
void solve() {
cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char ch = map[i][j];
switch (ch) {
case '.':
break;
case 'X':
break;
case 'S':
p[0].x = i;
p[0].y = j;
map[i][j] = '.';
break;
default:
int t = (ch - '0');
p[t].x = i;
p[t].y = j;
}
/* if(map[i][j]=='S'){
p[0].x=i; p[0].y = j;
map[i][j]='.';
}
else if(isdigit(map[i][j])){
int index=map[i][j]-'0';
p[index].x=i; p[index].y=j;
}*/
}
}
int step = 0;
for (int i = 0; i < n; i++) {
step += bfs(i);
}
printf("%d\n", step);
}
int main() { solve(); }
|
[["-", 15, 339, 51, 16, 31, 23, 0, 16, 17, 60], ["+", 15, 339, 51, 16, 31, 23, 0, 16, 17, 79], ["-", 51, 16, 31, 23, 0, 16, 12, 103, 0, 125], ["+", 51, 16, 31, 23, 0, 16, 12, 103, 0, 125]]
| 1
| 662
|
#define LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define SIZE 1010
using namespace std;
char map[SIZE][SIZE];
int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int bfs(int &x, int &y, int h, int w, int target) {
int step[SIZE][SIZE];
for (int i = 0; i < h; i++)
fill(step[i], step[i] + w, -1);
step[x][y] = 0;
queue<pair<int, int>> q;
q.push(make_pair(x, y));
while (!q.empty()) {
pair<int, int> temp = q.front();
q.pop();
if (map[temp.first][temp.second] == target + '0') {
x = temp.first;
y = temp.second;
return step[x][y];
}
for (int i = 0; i < 4; i++) {
int newx = temp.first + dir[i][0];
int newy = temp.second + dir[i][1];
if (0 <= newx && newx < h && 0 <= newy && newy < w &&
step[newx][newy] == -1 && map[newx][newy] != 'X') {
q.push(make_pair(newx, newy));
step[newx][newy] = step[temp.first][temp.second] + 1;
}
}
}
}
int main(void) {
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
int n, w, h;
int x, y;
scanf("%d%d%d", &h, &w, &n);
getchar();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%c", &map[i][j]);
if (map[i][j] == 'S') {
x = i;
y = j;
}
}
getchar();
}
int result = 0;
for (int i = 1; i <= n; i++)
result += bfs(x, y, h, w, i);
printf("%d\n", result);
return 0;
}
|
//#define LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define SIZE 1010
using namespace std;
char map[SIZE][SIZE];
int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int bfs(int &x, int &y, int h, int w, int target) {
int step[SIZE][SIZE];
for (int i = 0; i < h; i++)
fill(step[i], step[i] + w, -1);
step[x][y] = 0;
queue<pair<int, int>> q;
q.push(make_pair(x, y));
while (!q.empty()) {
pair<int, int> temp = q.front();
q.pop();
if (map[temp.first][temp.second] == target + '0') {
x = temp.first;
y = temp.second;
return step[x][y];
}
for (int i = 0; i < 4; i++) {
int newx = temp.first + dir[i][0];
int newy = temp.second + dir[i][1];
if (0 <= newx && newx < h && 0 <= newy && newy < w &&
step[newx][newy] == -1 && map[newx][newy] != 'X') {
q.push(make_pair(newx, newy));
step[newx][newy] = step[temp.first][temp.second] + 1;
}
}
}
}
int main(void) {
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
int n, w, h;
int x, y;
scanf("%d%d%d", &h, &w, &n);
getchar();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%c", &map[i][j]);
if (map[i][j] == 'S') {
x = i;
y = j;
}
}
getchar();
}
int result = 0;
for (int i = 1; i <= n; i++)
result += bfs(x, y, h, w, i);
printf("%d\n", result);
return 0;
}
|
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 0, 148], ["-", 36, 36, 36, 36, 0, 30, 0, 58, 141, 22]]
| 1
| 521
|
#include <cstdlib>
#include <iostream>
#include <queue>
#define INF 1000000
using namespace std;
typedef pair<int, int> P;
int H, W, N;
char field[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int d[1000][1000];
int ans = 0;
void bfs(int sx, int sy, int n) {
int x, y;
queue<P> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
que.push(P(sx, sy));
d[sx][sy] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
x = p.first;
y = p.second;
if (field[x][y] == n + '1')
break; //???
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' &&
d[nx][ny] == INF) {
que.push(P(nx, ny));
d[nx][ny] = d[x][y] + 1;
}
}
}
ans += d[x][y];
if (n < 8)
bfs(x, y, n + 1);
else
return;
}
void solve() {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 'S') {
bfs(i, j, 0);
cout << ans << endl;
return;
}
}
}
}
int main() {
string str;
cin >> H >> W >> N;
getline(cin, str);
for (int i = 0; i < H; i++) {
getline(cin, str);
for (int j = 0; j < W; j++) {
field[i][j] = *str.substr(j, 1).c_str();
}
}
solve();
}
|
#include <cstdlib>
#include <iostream>
#include <queue>
#define INF 1000000
using namespace std;
typedef pair<int, int> P;
int H, W, N;
char field[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int d[1000][1000];
int ans = 0;
void bfs(int sx, int sy, int n) {
int x, y;
queue<P> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
que.push(P(sx, sy));
d[sx][sy] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
x = p.first;
y = p.second;
if (field[x][y] == n + '1')
break; //???
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' &&
d[nx][ny] == INF) {
que.push(P(nx, ny));
d[nx][ny] = d[x][y] + 1;
}
}
}
ans += d[x][y];
if (n < N - 1)
bfs(x, y, n + 1);
else
return;
}
void solve() {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 'S') {
bfs(i, j, 0);
cout << ans << endl;
return;
}
}
}
}
int main() {
string str;
cin >> H >> W >> N;
getline(cin, str);
for (int i = 0; i < H; i++) {
getline(cin, str);
for (int j = 0; j < W; j++) {
field[i][j] = *str.substr(j, 1).c_str();
}
}
solve();
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 33], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
| 1
| 526
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
int gy = y[num + 1], gx = x[num + 1];
int d[1001][1001];
for (int i = 0; i <= 1001; i++) {
for (int j = 0; j <= 1001; j++) {
d[i][j] = INF;
}
}
queue<int> qx;
queue<int> qy;
qx.push(x[num]);
qy.push(y[num]);
d[y[num]][x[num]] = 0;
while (qx.size() + qy.size() != 0) {
int ny, nx;
ny = qy.front();
qy.pop();
nx = qx.front();
qx.pop();
if (ny == gy && nx == gx) {
break;
}
for (int i = 0; i < 4; i++) {
int mx = nx + d1[i], my = ny + d2[i];
if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
d[my][mx] == INF) {
d[my][mx] = d[ny][nx] + 1;
qx.push(mx);
qy.push(my);
}
}
}
return d[gy][gx];
}
int main() {
int ans = 0;
cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
if (map[i][j] == 'S') {
y[0] = i;
x[0] = j;
} else if ('0' < map[i][j] && map[i][j] <= '9') {
y[(int)(map[i][j] - '0')] = i;
x[(int)(map[i][j] - '0')] = j;
}
}
}
for (int i = 0; i < n; i++) {
ans += bfs(i);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
int gy = y[num + 1], gx = x[num + 1];
int d[1001][1001];
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
d[i][j] = INF;
}
}
queue<int> qx;
queue<int> qy;
qx.push(x[num]);
qy.push(y[num]);
d[y[num]][x[num]] = 0;
while (qx.size() + qy.size() != 0) {
int ny, nx;
ny = qy.front();
qy.pop();
nx = qx.front();
qx.pop();
if (ny == gy && nx == gx) {
break;
}
for (int i = 0; i < 4; i++) {
int mx = nx + d1[i], my = ny + d2[i];
if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
d[my][mx] == INF) {
d[my][mx] = d[ny][nx] + 1;
qx.push(mx);
qy.push(my);
}
}
}
return d[gy][gx];
}
int main() {
int ans = 0;
cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
if (map[i][j] == 'S') {
y[0] = i;
x[0] = j;
} else if ('0' < map[i][j] && map[i][j] <= '9') {
y[(int)(map[i][j] - '0')] = i;
x[(int)(map[i][j] - '0')] = j;
}
}
}
for (int i = 0; i < n; i++) {
ans += bfs(i);
}
cout << ans << endl;
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18]]
| 1
| 593
|
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int search(int sx, int sy, int dx, int dy, vector<vector<char>> &f, int w,
int h) {
vector<bool> visit;
queue<pair<pair<int, int>, int>> q;
visit.resize(w * h, false);
q.push(make_pair(make_pair(sx, sy), 0));
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int cost = q.front().second;
q.pop();
if (visit[x * w + y])
continue;
visit[x * w + y] = true;
if (f[x][y] == 'X')
continue;
if (x == dx && y == dy)
return cost;
int ix[4] = {-1, 0, 0, 1};
int iy[4] = {0, -1, 1, 0};
for (int i = 0; i < 4; ++i) {
int nx = ix[i] + x;
int ny = iy[i] + y;
if (nx < 0 || ny < 0 || nx >= w || ny >= h)
continue;
q.push(make_pair(make_pair(nx, ny), cost + 1));
}
}
return -1;
}
int main() {
int h, w, n;
vector<vector<char>> f;
vector<pair<int, int>> pos;
scanf("%d%d%d", &h, &w, &n);
f.resize(h);
pos.resize(n + 1);
for (int i = 0; i < h; ++i) {
char temp[1024];
scanf("%s", temp);
f[i].resize(w);
for (int j = 0; j < w; ++j) {
f[i][j] = temp[j];
if (f[i][j] == 'S')
pos[0] = make_pair(i, j);
if ('0' <= f[i][j] && f[i][j] <= '9')
pos[f[i][j] - '0'] = make_pair(i, j);
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
int sx = pos[i - 1].first;
int sy = pos[i - 1].second;
int dx = pos[i].first;
int dy = pos[i].second;
ans += search(sx, sy, dx, dy, f, w, h);
}
printf("%d\n", ans);
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int search(int sx, int sy, int dx, int dy, vector<vector<char>> &f, int w,
int h) {
vector<bool> visit;
queue<pair<pair<int, int>, int>> q;
visit.resize(w * h, false);
q.push(make_pair(make_pair(sx, sy), 0));
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int cost = q.front().second;
q.pop();
if (visit[x * w + y])
continue;
visit[x * w + y] = true;
if (f[x][y] == 'X')
continue;
if (x == dx && y == dy)
return cost;
int ix[4] = {-1, 0, 0, 1};
int iy[4] = {0, -1, 1, 0};
for (int i = 0; i < 4; ++i) {
int nx = ix[i] + x;
int ny = iy[i] + y;
if (nx < 0 || ny < 0 || nx >= h || ny >= w)
continue;
q.push(make_pair(make_pair(nx, ny), cost + 1));
}
}
return -1;
}
int main() {
int h, w, n;
vector<vector<char>> f;
vector<pair<int, int>> pos;
scanf("%d%d%d", &h, &w, &n);
f.resize(h);
pos.resize(n + 1);
for (int i = 0; i < h; ++i) {
char temp[1024];
scanf("%s", temp);
f[i].resize(w);
for (int j = 0; j < w; ++j) {
f[i][j] = temp[j];
if (f[i][j] == 'S')
pos[0] = make_pair(i, j);
if ('0' <= f[i][j] && f[i][j] <= '9')
pos[f[i][j] - '0'] = make_pair(i, j);
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
int sx = pos[i - 1].first;
int sy = pos[i - 1].second;
int dx = pos[i].first;
int dy = pos[i].second;
ans += search(sx, sy, dx, dy, f, w, h);
}
printf("%d\n", ans);
return 0;
}
|
[["-", 15, 339, 51, 16, 31, 16, 12, 16, 12, 22], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 12, 22], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22]]
| 1
| 618
|
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int c[1000][1000];
int dat[1000][1000];
int cost[1000][1000];
int destination = 1;
int startposx, startposy;
int h, w, n;
struct QUEUE {
int x, y;
};
QUEUE queue[256];
int qbegin = 0, qend = 0;
void bfsToDestination(int x, int y);
void PushQueue(int x, int y);
QUEUE PopQueue();
bool isQueueEmpty();
int main(void) {
int i, j;
scanf("%d%d%d", &w, &h, &n);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
dat[j][i] = fgetc(stdin);
if (dat[j][i] == '\n')
dat[j][i] = fgetc(stdin);
if (dat[j][i] == 'S') {
startposx = j;
startposy = i;
cost[j][i] = 0;
}
}
}
// for(i = 0; i < h; i++) {
// for(j = 0; j < w; j++) {
// printf("%c", dat[j][i]);
// }
// printf("\n");
//}
for (i = 0; i < n; i++) {
memcpy(c, dat, sizeof(dat));
bfsToDestination(startposx, startposy);
}
printf("%d\n", cost[startposx][startposy]);
return 0;
}
void bfsToDestination(int x, int y) {
int i;
QUEUE ret;
qbegin = 0;
qend = 0;
// map
// int j;
// printf("---map\n");
// for(i = 0; i < h; i++) {
// for(j = 0; j < w; j++) {
// printf("%c", c[j][i]);
// }
// printf("\n");
//}
PushQueue(x, y);
c[x][y] = 'X';
while (!isQueueEmpty()) {
ret = PopQueue();
// printf("ret.x:%d ret.y:%d cost.x,y:%d\n", ret.x, ret.y,
// cost[ret.x][ret.y]);
for (i = 0; i < 4; i++) {
int nx = ret.x + dx[i];
int ny = ret.y + dy[i];
if ((nx >= 0) && (ny >= 0) && (nx < w) && (ny < h)) {
if (c[nx][ny] != 'X') {
PushQueue(nx, ny);
cost[nx][ny] = cost[ret.x][ret.y] + 1;
if ((c[nx][ny] - '0') == destination) {
destination++;
startposx = nx;
startposy = ny;
return;
}
c[nx][ny] = 'X';
}
}
}
}
}
void PushQueue(int x, int y) {
queue[qend].x = x;
queue[qend].y = y;
qend++;
}
QUEUE PopQueue() {
QUEUE ret;
ret.x = queue[qbegin].x;
ret.y = queue[qbegin].y;
qbegin++;
return ret;
}
bool isQueueEmpty() {
if (qend == qbegin)
return true;
return false;
}
|
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int c[1000][1000];
int dat[1000][1000];
int cost[1000][1000];
int destination = 1;
int startposx, startposy;
int h, w, n;
struct QUEUE {
int x, y;
};
QUEUE queue[1000000];
int qbegin = 0, qend = 0;
void bfsToDestination(int x, int y);
void PushQueue(int x, int y);
QUEUE PopQueue();
bool isQueueEmpty();
int main(void) {
int i, j;
scanf("%d%d%d", &h, &w, &n);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
dat[j][i] = fgetc(stdin);
if (dat[j][i] == '\n')
dat[j][i] = fgetc(stdin);
if (dat[j][i] == 'S') {
startposx = j;
startposy = i;
cost[j][i] = 0;
}
}
}
// for(i = 0; i < h; i++) {
// for(j = 0; j < w; j++) {
// printf("%c", dat[j][i]);
// }
// printf("\n");
//}
for (i = 0; i < n; i++) {
memcpy(c, dat, sizeof(dat));
bfsToDestination(startposx, startposy);
}
printf("%d\n", cost[startposx][startposy]);
return 0;
}
void bfsToDestination(int x, int y) {
int i;
QUEUE ret;
qbegin = 0;
qend = 0;
// map
// int j;
// printf("---map\n");
// for(i = 0; i < h; i++) {
// for(j = 0; j < w; j++) {
// printf("%c", c[j][i]);
// }
// printf("\n");
//}
PushQueue(x, y);
c[x][y] = 'X';
while (!isQueueEmpty()) {
ret = PopQueue();
// printf("ret.x:%d ret.y:%d cost.x,y:%d\n", ret.x, ret.y,
// cost[ret.x][ret.y]);
for (i = 0; i < 4; i++) {
int nx = ret.x + dx[i];
int ny = ret.y + dy[i];
if ((nx >= 0) && (ny >= 0) && (nx < w) && (ny < h)) {
if (c[nx][ny] != 'X') {
PushQueue(nx, ny);
cost[nx][ny] = cost[ret.x][ret.y] + 1;
if ((c[nx][ny] - '0') == destination) {
destination++;
startposx = nx;
startposy = ny;
return;
}
c[nx][ny] = 'X';
}
}
}
}
}
void PushQueue(int x, int y) {
queue[qend].x = x;
queue[qend].y = y;
qend++;
}
QUEUE PopQueue() {
QUEUE ret;
ret.x = queue[qbegin].x;
ret.y = queue[qbegin].y;
qbegin++;
return ret;
}
bool isQueueEmpty() {
if (qend == qbegin)
return true;
return false;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 66, 28, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 66, 28, 22]]
| 1
| 642
|
p = min(eval(input()), input, eval(input()))
j = min(eval(input()), eval(input()))
print((p + j) - 50)
|
p = min(eval(input()), eval(input()), eval(input()))
j = min(eval(input()), eval(input()))
print((p + j) - 50)
|
[["+", 0, 662, 12, 652, 3, 4, 0, 652, 63, 22], ["+", 12, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25], ["+", 12, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
| 5
| 48
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
/**
* Soccer
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while ((line = br.readLine()) != null && !line.isEmpty()) {
int n = Short.parseShort(line);
int[][] points = new int[n + 1][3];
for (int i = 0; i < n + 1; i++) {
points[i][0] = i;
}
int g = (n * (n - 1)) / 2;
int t1, t2, p1, p2;
StringTokenizer st;
for (int i = 0; i < g; i++) {
st = new StringTokenizer(br.readLine());
t1 = Integer.parseInt(st.nextToken());
t2 = Integer.parseInt(st.nextToken());
p1 = Integer.parseInt(st.nextToken());
p2 = Integer.parseInt(st.nextToken());
if (p1 > p2) {
points[t1][1] += 3;
} else if (p1 == p2) {
points[t1][1] += 1;
points[t2][1] += 1;
} else {
points[t2][1] += 3;
}
}
Arrays.sort(points, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[1] - o1[1];
}
});
int rank, prev;
rank = 1;
prev = points[0][1];
for (int i = 0; i < n + 1; i++) {
if (points[i][1] != prev) {
rank++;
}
points[i][2] = rank;
prev = points[i][1];
}
Arrays.sort(points, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
StringBuilder sb = new StringBuilder();
for (int i = 1; i < n + 1; i++) {
sb.append(points[i][2] + "\n");
}
System.out.print(sb.toString());
}
}
}
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
/**
* Soccer
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while ((line = br.readLine()) != null && !line.isEmpty()) {
int n = Short.parseShort(line);
int[][] points = new int[n + 1][3];
for (int i = 0; i < n + 1; i++) {
points[i][0] = i;
}
int g = (n * (n - 1)) / 2;
int t1, t2, p1, p2;
StringTokenizer st;
for (int i = 0; i < g; i++) {
st = new StringTokenizer(br.readLine());
t1 = Integer.parseInt(st.nextToken());
t2 = Integer.parseInt(st.nextToken());
p1 = Integer.parseInt(st.nextToken());
p2 = Integer.parseInt(st.nextToken());
if (p1 > p2) {
points[t1][1] += 3;
} else if (p1 == p2) {
points[t1][1] += 1;
points[t2][1] += 1;
} else {
points[t2][1] += 3;
}
}
Arrays.sort(points, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[1] - o1[1];
}
});
int rank, prev;
rank = 1;
prev = points[0][1];
for (int i = 0; i < n + 1; i++) {
if (points[i][1] != prev) {
rank = i + 1;
}
points[i][2] = rank;
prev = points[i][1];
}
Arrays.sort(points, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
StringBuilder sb = new StringBuilder();
for (int i = 1; i < n + 1; i++) {
sb.append(points[i][2] + "\n");
}
System.out.print(sb.toString());
}
}
}
|
[["-", 0, 57, 64, 196, 0, 1, 0, 27, 0, 29], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 17, 32], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 31, 22], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 64, 196, 0, 1, 0, 11, 12, 16, 12, 499]]
| 3
| 555
|
#include <stdio.h>
int main(void) {
int game, n, i, j, team[100];
int a, b, c, d, ban[100];
scanf("%d", &n);
for (i = 0; i < n; i++) {
team[i] = 0;
ban[i] = 1;
}
game = n * (n - 1) / 2;
for (i = 0; i < game; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
if (c > d) {
team[a - 1] += 3;
} else if (c == d) {
team[a - 1] += 1;
team[b - 1] += 1;
} else {
team[b - 1] += 3;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (team[i] < team[j]) {
ban[i]++;
}
printf("%d\n", ban[i]);
}
}
return 0;
}
|
#include <stdio.h>
int main(void) {
int game, n, i, j, team[100];
int a, b, c, d, ban[100];
scanf("%d", &n);
for (i = 0; i < n; i++) {
team[i] = 0;
ban[i] = 1;
}
game = n * (n - 1) / 2;
for (i = 0; i < game; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
if (c > d) {
team[a - 1] += 3;
} else if (c == d) {
team[a - 1] += 1;
team[b - 1] += 1;
} else {
team[b - 1] += 3;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (team[i] < team[j]) {
ban[i]++;
}
}
printf("%d\n", ban[i]);
}
return 0;
}
|
[["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
| 0
| 244
|
#include <stdio.h>
int main(void) {
int n, i, j, no[2], point[2], z, team[100] = {0};
scanf("%d", &n);
if (n) {
int prank[n];
for (i = 0; i < n * (n - 1) / 2; i++) {
scanf("%d %d", &no[0], &no[1]);
scanf("%d %d", &point[0], &point[1]);
if (point[0] > point[1])
team[no[0] - 1] += 2;
else if (point[0] < point[1])
team[no[1] - 1] += 2;
else {
team[no[0] - 1]++;
team[no[1] - 1]++;
}
}
for (i = 0; i < n; i++) {
prank[i] = team[i];
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (prank[i] < prank[j]) {
z = prank[i];
prank[i] = prank[j];
prank[j] = z;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (team[i] == prank[j]) {
printf("%d\n", ++j);
break;
}
}
}
}
return (0);
}
|
#include <stdio.h>
int main(void) {
int n, i, j, no[2], point[2], z, team[100] = {0};
scanf("%d", &n);
if (n) {
int prank[n];
for (i = 0; i < n * (n - 1) / 2; i++) {
scanf("%d %d", &no[0], &no[1]);
scanf("%d %d", &point[0], &point[1]);
if (point[0] > point[1])
team[no[0] - 1] += 3;
else if (point[0] < point[1])
team[no[1] - 1] += 3;
else {
team[no[0] - 1]++;
team[no[1] - 1]++;
}
}
for (i = 0; i < n; i++) {
prank[i] = team[i];
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (prank[i] < prank[j]) {
z = prank[i];
prank[i] = prank[j];
prank[j] = z;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (team[i] == prank[j]) {
printf("%d\n", ++j);
break;
}
}
}
}
return (0);
}
|
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["-", 75, 76, 0, 57, 64, 1, 0, 11, 12, 13], ["+", 75, 76, 0, 57, 64, 1, 0, 11, 12, 13]]
| 0
| 353
|
#include <stdio.h>
int main(void) {
int n, i, j, team[100], score[100], a, b, c, d;
scanf("%d", &n);
for (i = 0; i < n; i++) {
team[i] = 0;
}
for (i = 0; i < n * (n - 1) / 2; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
if (c > d) {
team[a - 1] += 3;
} else if (c < d) {
team[b - 1] += 3;
} else {
team[a - 1]++;
team[b - 1]++;
}
}
for (i = 0; i < n; i++) {
score[i] = 1;
for (j = 0; j < n; j++) {
if (team[i] < team[j]) {
score[j]++;
}
}
}
for (i = 0; i < n; i++) {
printf("%d\n", score[i]);
}
return 0;
}
|
#include <stdio.h>
int main(void) {
int n, i, j, team[100], score[100], a, b, c, d;
scanf("%d", &n);
for (i = 0; i < n; i++) {
team[i] = 0;
}
for (i = 0; i < n * (n - 1) / 2; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
if (c > d) {
team[a - 1] += 3;
} else if (c < d) {
team[b - 1] += 3;
} else {
team[a - 1]++;
team[b - 1]++;
}
}
for (i = 0; i < n; i++) {
score[i] = 1;
for (j = 0; j < n; j++) {
if (team[i] < team[j]) {
score[i]++;
}
}
}
for (i = 0; i < n; i++) {
printf("%d\n", score[i]);
}
return 0;
}
|
[["-", 64, 9, 0, 1, 0, 27, 28, 69, 71, 22], ["+", 64, 9, 0, 1, 0, 27, 28, 69, 71, 22]]
| 0
| 250
|
#include <iostream>
using namespace std;
int main() {
int N, n[110], cnt = 1, A, B, C, D;
cin >> N;
for (int i = 0; i < N * (N - 1) / 2; i++) {
cin >> A >> B >> C >> D;
if (C == D) {
n[A] += 1;
n[B] += 1;
}
if (C > D) {
n[A] += 3;
}
if (D > C) {
n[B] += 3;
}
}
for (int i = 1; i <= N; i++) {
cnt = 1;
for (int j = 1; j <= N; j++) {
if (i != j && n[i] < n[j]) {
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int N, n[110] = {}, cnt = 1, A, B, C, D;
cin >> N;
for (int i = 0; i < N * (N - 1) / 2; i++) {
cin >> A >> B >> C >> D;
if (C == D) {
n[A] += 1;
n[B] += 1;
}
if (C > D) {
n[A] += 3;
}
if (D > C) {
n[B] += 3;
}
}
for (int i = 1; i <= N; i++) {
cnt = 1;
for (int j = 1; j <= N; j++) {
if (i != j && n[i] < n[j]) {
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 45], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 46]]
| 1
| 188
|
#include <iostream>
using namespace std;
int main() {
int N;
int A, B, C, D;
int sc[101] = {0};
int rank[101];
int n;
int max, mi;
int c;
cin >> N;
for (int i = 0; i < N * (N - 1) / 2; i++) {
cin >> A >> B >> C >> D;
if (C > D)
sc[A] += 3;
if (C == D)
sc[A]++, sc[B]++;
if (C < D)
sc[B] += 3;
}
n = 0;
while (n < N) {
max = sc[1], mi = 1;
for (int i = 2; i <= N; i++) {
if (sc[i] > max) {
max = sc[i];
mi = i;
}
}
c = 0;
for (int i = 1; i <= N; i++) {
if (sc[i] == max) {
sc[i] = -1;
c++;
rank[i] = n;
}
}
n += c;
}
for (int i = 1; i <= N; i++) {
cout << rank[i] << endl;
}
}
|
#include <iostream>
using namespace std;
int main() {
int N;
int A, B, C, D;
int sc[101] = {0};
int rank[101];
int n;
int max, mi;
int c;
cin >> N;
for (int i = 0; i < N * (N - 1) / 2; i++) {
cin >> A >> B >> C >> D;
if (C > D)
sc[A] += 3;
if (C == D)
sc[A]++, sc[B]++;
if (C < D)
sc[B] += 3;
}
n = 0;
while (n < N) {
max = sc[1], mi = 1;
for (int i = 2; i <= N; i++) {
if (sc[i] > max) {
max = sc[i];
mi = i;
}
}
c = 0;
for (int i = 1; i <= N; i++) {
if (sc[i] == max) {
sc[i] = -1;
c++;
rank[i] = n + 1;
}
}
n += c;
}
for (int i = 1; i <= N; i++) {
cout << rank[i] << endl;
}
}
|
[["+", 64, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
| 1
| 271
|
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#include <windows.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
typedef unsigned int uint;
typedef long long int llint;
typedef pair<llint, llint> pii;
#define mpq(T) priority_queue<T, vector<T>, greater<T>>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) for (int i = 0; i < (a); i++)
class BIT {
private:
int s;
llint *d;
public:
BIT(int size) {
s = size;
d = (llint *)malloc(sizeof(llint) * s);
for (int i = 0; i < s; i++) {
d[i] = 0;
}
}
void add(int b, int x) {
for (int i = b + 1; i <= s; i += (i & -i)) {
d[i - 1] += x;
}
}
llint sum(int b) {
llint all = 0;
for (int i = b + 1; i > 0; i -= (i & -i)) {
all += d[i - 1];
}
return all;
}
~BIT() { free(d); }
};
class unionfind {
private:
int s;
int *o;
int *r;
public:
unionfind(int dsize) {
s = dsize;
o = (int *)malloc(sizeof(int) * s);
r = (int *)malloc(sizeof(int) * s);
for (int i = 0; i < s; i++) {
o[i] = i;
r[i] = 0;
}
}
int mfind(int b) {
if (o[b] != b) {
o[b] = mfind(o[b]);
}
return o[b];
}
void munion(int a, int b) {
int aw = mfind(a);
int bw = mfind(b);
if (aw == bw) {
return;
}
if (r[aw] > r[bw]) {
o[bw] = a;
} else if (r[aw] < r[bw]) {
o[aw] = bw;
} else {
o[bw] = aw;
r[aw]++;
}
s--;
}
int size() { return s; }
~unionfind() {
free(o);
free(r);
}
};
template <class T> int lbound(T *d, T key, int s, int e) {
s--;
while (e - s > 1) {
int m = (s + e) / 2;
if (d[m] < key) {
s = m;
} else {
e = m;
}
}
return e;
}
template <class T> int ubound(T *d, T key, int s, int e) {
s--;
while (e - s > 1) {
int m = (s + e) / 2;
if (key < d[m]) {
e = m;
} else {
s = m;
}
}
return e;
}
class zpress {
private:
int ms;
pii *md;
int n;
int s;
llint *d;
int *t;
public:
zpress(int dsize = 1000000) {
int ms = dsize;
md = (pii *)malloc(sizeof(pii) * ms);
d = (llint *)malloc(sizeof(llint) * ms);
t = (int *)malloc(sizeof(int) * ms);
n = 0;
}
void in(llint x) {
in2(x, n);
n++;
}
void in2(llint x, int b) { md[b] = make_pair(x, b); }
void syori() { syori2(n); }
void syori2(int k) {
sort(md, md + k);
llint mae = LLONG_MIN;
s = 0;
REP(i, k) {
if (md[i].first != mae) {
d[s] = md[i].first;
s++;
mae = md[i].first;
}
t[md[i].second] = s - 1;
}
}
llint iti(int b) { return t[b]; }
int size() { return s; }
int first(llint x) { return lbound(d, x, 0, s); }
int end(llint x) {
return ubound(d, x, 0, s);
;
}
~zpress() {
free(md);
free(d);
free(t);
}
};
class xorshift {
private:
unsigned int a, b, c, d;
public:
xorshift(unsigned int aw = 123456789, unsigned int bw = 362436069,
unsigned int cw = 521288629, unsigned int dw = 88675123,
unsigned int e = 97) {
a = aw;
b = bw;
c = cw;
d = dw;
for (unsigned int i = 0; i < e; i++) {
operator()();
}
}
unsigned int operator()() {
unsigned int w = a ^ (a << 11);
a = b;
b = c;
c = d;
d = (d ^ (d >> 19)) ^ (w ^ (w >> 8));
return d;
}
};
llint gcd(llint a, llint b) {
while (1) {
llint w = a % b;
if (w == 0) {
return b;
}
a = b;
b = w;
}
}
llint sei(llint d, llint p) {
if (d < 0) {
d -= ((d + 1) / p - 1) * p;
} else {
d %= p;
}
return d;
}
llint inv(llint a, llint p) {
llint r0 = sei(a, p);
llint r1 = p;
llint a0 = 1;
llint b0 = 0;
llint a1 = 0;
llint b1 = 1;
while (1) {
llint q = r0 / r1;
llint w = r0 - q * r1;
if (w == 0) {
break;
}
r0 = r1;
r1 = w;
w = a0 - q * a1;
a0 = a1;
a1 = w;
}
return sei(a1, p);
}
template <llint P> class mod {
private:
llint d;
public:
mod(llint a = 0) {
d = a;
d = sei(d, P);
}
mod operator-() const { return mod(P - d); }
mod operator+=(const mod &a) {
d = d + a.d;
d = sei(d, P);
return *this;
}
mod operator-=(const mod &a) { return operator+=(-a); }
mod operator*=(const mod &a) {
d = d * a.d;
d = sei(d, P);
return *this;
}
mod operator/=(const mod &a) { return a * mod(inv(d, P)); }
mod operator+(const mod &a) const {
mod w = *this;
return (w += a);
}
mod operator-(const mod &a) const {
mod w = *this;
return (w -= a);
}
mod operator*(const mod &a) const {
mod w = *this;
return (w *= a);
}
mod operator/(const mod &a) const {
mod w = *this;
return (w /= a);
}
bool operator==(const mod &a) const { return (d == a.d); }
bool operator!=(const mod &a) const { return (d != a.d); }
operator llint() const { return d; }
};
/////////////////////////////////////////////////////
FILE *fi;
FILE *fo;
void err(char *a) {
printf("%s\n", a);
getchar();
}
llint in() {
llint w;
fscanf(fi, "%lld", &w);
return w;
}
void ins(char *a) { fscanf(fi, "%s", a); }
void out(llint a) { fprintf(fo, "%lld\n", a); }
void outs(char *a) { fprintf(fo, "%s\n", a); }
void func();
int main() {
fi = stdin;
fo = stdout;
func();
return 0;
}
int d[100];
pii w[100];
int ju[100];
void func() {
int n = in();
REP(i, n) { d[i] = 0; }
REP(i, n * (n - 1) / 2) {
int a = in();
int b = in();
int c = in();
int dw = in();
if (c == dw) {
d[a - 1]++;
d[b - 1]++;
} else if (c > dw) {
d[a - 1] += 3;
} else {
d[b - 1] += 3;
}
}
REP(i, n) { w[i] = make_pair(d[i], i); }
sort(w, w + n, greater<pii>());
int mae = -1;
int nj;
REP(i, n) {
if (w[i].first != mae) {
mae - w[i].first;
nj = i + 1;
}
ju[w[i].second] = nj;
}
REP(i, n) { out(ju[i]); }
}
|
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#include <windows.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
typedef unsigned int uint;
typedef long long int llint;
typedef pair<llint, llint> pii;
#define mpq(T) priority_queue<T, vector<T>, greater<T>>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) for (int i = 0; i < (a); i++)
class BIT {
private:
int s;
llint *d;
public:
BIT(int size) {
s = size;
d = (llint *)malloc(sizeof(llint) * s);
for (int i = 0; i < s; i++) {
d[i] = 0;
}
}
void add(int b, int x) {
for (int i = b + 1; i <= s; i += (i & -i)) {
d[i - 1] += x;
}
}
llint sum(int b) {
llint all = 0;
for (int i = b + 1; i > 0; i -= (i & -i)) {
all += d[i - 1];
}
return all;
}
~BIT() { free(d); }
};
class unionfind {
private:
int s;
int *o;
int *r;
public:
unionfind(int dsize) {
s = dsize;
o = (int *)malloc(sizeof(int) * s);
r = (int *)malloc(sizeof(int) * s);
for (int i = 0; i < s; i++) {
o[i] = i;
r[i] = 0;
}
}
int mfind(int b) {
if (o[b] != b) {
o[b] = mfind(o[b]);
}
return o[b];
}
void munion(int a, int b) {
int aw = mfind(a);
int bw = mfind(b);
if (aw == bw) {
return;
}
if (r[aw] > r[bw]) {
o[bw] = a;
} else if (r[aw] < r[bw]) {
o[aw] = bw;
} else {
o[bw] = aw;
r[aw]++;
}
s--;
}
int size() { return s; }
~unionfind() {
free(o);
free(r);
}
};
template <class T> int lbound(T *d, T key, int s, int e) {
s--;
while (e - s > 1) {
int m = (s + e) / 2;
if (d[m] < key) {
s = m;
} else {
e = m;
}
}
return e;
}
template <class T> int ubound(T *d, T key, int s, int e) {
s--;
while (e - s > 1) {
int m = (s + e) / 2;
if (key < d[m]) {
e = m;
} else {
s = m;
}
}
return e;
}
class zpress {
private:
int ms;
pii *md;
int n;
int s;
llint *d;
int *t;
public:
zpress(int dsize = 1000000) {
int ms = dsize;
md = (pii *)malloc(sizeof(pii) * ms);
d = (llint *)malloc(sizeof(llint) * ms);
t = (int *)malloc(sizeof(int) * ms);
n = 0;
}
void in(llint x) {
in2(x, n);
n++;
}
void in2(llint x, int b) { md[b] = make_pair(x, b); }
void syori() { syori2(n); }
void syori2(int k) {
sort(md, md + k);
llint mae = LLONG_MIN;
s = 0;
REP(i, k) {
if (md[i].first != mae) {
d[s] = md[i].first;
s++;
mae = md[i].first;
}
t[md[i].second] = s - 1;
}
}
llint iti(int b) { return t[b]; }
int size() { return s; }
int first(llint x) { return lbound(d, x, 0, s); }
int end(llint x) {
return ubound(d, x, 0, s);
;
}
~zpress() {
free(md);
free(d);
free(t);
}
};
class xorshift {
private:
unsigned int a, b, c, d;
public:
xorshift(unsigned int aw = 123456789, unsigned int bw = 362436069,
unsigned int cw = 521288629, unsigned int dw = 88675123,
unsigned int e = 97) {
a = aw;
b = bw;
c = cw;
d = dw;
for (unsigned int i = 0; i < e; i++) {
operator()();
}
}
unsigned int operator()() {
unsigned int w = a ^ (a << 11);
a = b;
b = c;
c = d;
d = (d ^ (d >> 19)) ^ (w ^ (w >> 8));
return d;
}
};
llint gcd(llint a, llint b) {
while (1) {
llint w = a % b;
if (w == 0) {
return b;
}
a = b;
b = w;
}
}
llint sei(llint d, llint p) {
if (d < 0) {
d -= ((d + 1) / p - 1) * p;
} else {
d %= p;
}
return d;
}
llint inv(llint a, llint p) {
llint r0 = sei(a, p);
llint r1 = p;
llint a0 = 1;
llint b0 = 0;
llint a1 = 0;
llint b1 = 1;
while (1) {
llint q = r0 / r1;
llint w = r0 - q * r1;
if (w == 0) {
break;
}
r0 = r1;
r1 = w;
w = a0 - q * a1;
a0 = a1;
a1 = w;
}
return sei(a1, p);
}
template <llint P> class mod {
private:
llint d;
public:
mod(llint a = 0) {
d = a;
d = sei(d, P);
}
mod operator-() const { return mod(P - d); }
mod operator+=(const mod &a) {
d = d + a.d;
d = sei(d, P);
return *this;
}
mod operator-=(const mod &a) { return operator+=(-a); }
mod operator*=(const mod &a) {
d = d * a.d;
d = sei(d, P);
return *this;
}
mod operator/=(const mod &a) { return a * mod(inv(d, P)); }
mod operator+(const mod &a) const {
mod w = *this;
return (w += a);
}
mod operator-(const mod &a) const {
mod w = *this;
return (w -= a);
}
mod operator*(const mod &a) const {
mod w = *this;
return (w *= a);
}
mod operator/(const mod &a) const {
mod w = *this;
return (w /= a);
}
bool operator==(const mod &a) const { return (d == a.d); }
bool operator!=(const mod &a) const { return (d != a.d); }
operator llint() const { return d; }
};
/////////////////////////////////////////////////////
FILE *fi;
FILE *fo;
void err(char *a) {
printf("%s\n", a);
getchar();
}
llint in() {
llint w;
fscanf(fi, "%lld", &w);
return w;
}
void ins(char *a) { fscanf(fi, "%s", a); }
void out(llint a) { fprintf(fo, "%lld\n", a); }
void outs(char *a) { fprintf(fo, "%s\n", a); }
void func();
int main() {
fi = stdin;
fo = stdout;
func();
return 0;
}
int d[100];
pii w[100];
int ju[100];
void func() {
int n = in();
REP(i, n) { d[i] = 0; }
REP(i, n * (n - 1) / 2) {
int a = in();
int b = in();
int c = in();
int dw = in();
if (c == dw) {
d[a - 1]++;
d[b - 1]++;
} else if (c > dw) {
d[a - 1] += 3;
} else {
d[b - 1] += 3;
}
}
REP(i, n) { w[i] = make_pair(d[i], i); }
sort(w, w + n, greater<pii>());
int mae = -1;
int nj;
REP(i, n) {
if (w[i].first != mae) {
mae = w[i].first;
nj = i + 1;
}
ju[w[i].second] = nj;
}
REP(i, n) { out(ju[i]); }
}
|
[["-", 0, 57, 64, 9, 0, 1, 0, 16, 17, 33], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32]]
| 1
| 2,017
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s[100], cop[100], rank[100];
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (c == d) {
s[a - 1]++;
s[b - 1]++;
} else {
(c > d ? s[a - 1] : s[b - 1]) += 3;
}
}
for (int i = 0; i < n; i++)
cop[i] = s[i];
sort(cop, cop + n);
reverse(cop, cop + n);
for (int i = n - 1; i >= 0; i--) {
rank[cop[i]] = i + 1;
}
for (int i = 0; i < n; i++) {
cout << rank[s[i]] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s[101], cop[101], rank[10101];
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (c == d) {
s[a - 1]++;
s[b - 1]++;
} else {
(c > d ? s[a - 1] : s[b - 1]) += 3;
}
}
for (int i = 0; i < n; i++)
cop[i] = s[i];
sort(cop, cop + n);
reverse(cop, cop + n);
for (int i = n - 1; i >= 0; i--) {
rank[cop[i]] = i + 1;
}
for (int i = 0; i < n; i++) {
cout << rank[s[i]] << endl;
}
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13]]
| 1
| 226
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c, d;
int score[101];
int pnt[101];
fill_n(pnt, 101, 0);
vector<pair<int, int>> v;
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
cin >> a >> b >> c >> d;
if (c < d)
pnt[b]++;
else if (c > d)
pnt[a]++;
else
pnt[b]++, pnt[a]++;
}
for (int i = 1; i <= n; i++) {
v.push_back(make_pair(pnt[i], i));
}
sort(v.begin(), v.end());
int index = 0;
int before = 0;
int same = 0;
for (int i = v.size() - 1; i >= 0; i--) {
if (v[i].first == before) {
same++;
score[v[i].second] = index;
} else {
index += same + 1;
score[v[i].second] = index;
before = v[i].first;
same = 0;
}
}
for (int i = 1; i <= n; i++) {
cout << score[i] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c, d;
int score[101];
int pnt[101];
fill_n(pnt, 101, 0);
vector<pair<int, int>> v;
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
cin >> a >> b >> c >> d;
if (c < d)
pnt[b] += 3;
else if (c > d)
pnt[a] += 3;
else
pnt[b]++, pnt[a]++;
}
for (int i = 1; i <= n; i++) {
v.push_back(make_pair(pnt[i], i));
}
sort(v.begin(), v.end());
int index = 0;
int before = 0;
int same = 0;
for (int i = v.size() - 1; i >= 0; i--) {
if (v[i].first == before) {
same++;
score[v[i].second] = index;
} else {
index += same + 1;
score[v[i].second] = index;
before = v[i].first;
same = 0;
}
}
for (int i = 1; i <= n; i++) {
cout << score[i] << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 57, 64, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 17, 107], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["-", 75, 76, 0, 57, 64, 1, 0, 27, 17, 29], ["+", 75, 76, 0, 57, 64, 1, 0, 11, 17, 107], ["+", 75, 76, 0, 57, 64, 1, 0, 11, 12, 13]]
| 1
| 304
|
#include <stdio.h>
int n, team[100];
int main() {
scanf("%d", &n);
for (int i = 0; i < n * (n - 1) / 2; i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if (c == d) {
team[a]++;
team[b]++;
}
if (c > d) {
team[a] += 3;
}
if (c < d) {
team[b] += 3;
}
}
for (int i = 1; i <= n; i++) {
int res = 0;
for (int j = 1; j <= n; j++) {
if (team[j] > team[i])
res++;
}
printf("%d\n", res);
}
return 0;
}
|
#include <stdio.h>
int n, team[100];
int main() {
scanf("%d", &n);
for (int i = 0; i < n * (n - 1) / 2; i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if (c == d) {
team[a]++;
team[b]++;
}
if (c > d) {
team[a] += 3;
}
if (c < d) {
team[b] += 3;
}
}
for (int i = 1; i <= n; i++) {
int res = 0;
for (int j = 1; j <= n; j++) {
if (team[j] > team[i])
res++;
}
printf("%d\n", res + 1);
}
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 193
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int toppingN = sc.nextInt();
int valBase = sc.nextInt();
int valTopping = sc.nextInt();
int calBase = sc.nextInt();
int[] calTopping = new int[toppingN];
for (int i = 0; i < toppingN; i++)
calTopping[i] = sc.nextInt();
java.util.Arrays.sort(calTopping);
int currCal = calBase;
int currVal = valBase;
for (int i = toppingN - 1; i >= 0; i--) {
int nextCal = currCal + calTopping[i];
int nextVal = currVal + valTopping;
if (((double)nextCal / nextVal) > ((double)currCal / currVal)) {
currCal = nextCal;
currVal = nextVal;
} else
break;
}
System.out.println((double)currCal / currVal);
}
}
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int toppingN = sc.nextInt();
int valBase = sc.nextInt();
int valTopping = sc.nextInt();
int calBase = sc.nextInt();
int[] calTopping = new int[toppingN];
for (int i = 0; i < toppingN; i++)
calTopping[i] = sc.nextInt();
java.util.Arrays.sort(calTopping);
int currCal = calBase;
int currVal = valBase;
for (int i = toppingN - 1; i >= 0; i--) {
int nextCal = currCal + calTopping[i];
int nextVal = currVal + valTopping;
if (((double)nextCal / nextVal) > ((double)currCal / currVal)) {
currCal = nextCal;
currVal = nextVal;
} else
break;
}
System.out.println(currCal / currVal);
}
}
|
[["-", 0, 492, 3, 4, 0, 16, 31, 74, 0, 24], ["-", 3, 4, 0, 16, 31, 74, 39, 511, 0, 512], ["-", 0, 492, 3, 4, 0, 16, 31, 74, 0, 25]]
| 3
| 210
|
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) { return (int *)b - (int *)a; }
int main() {
int n, a, b, c, d[10000], i, cost, calorie;
scanf("%d%d%d%d", &n, &a, &b, &c);
for (i = 0; i < n; i++) {
scanf("%d", &d[i]);
}
qsort(d, n, sizeof(int), cmp);
cost = a;
calorie = c;
for (i = 0; calorie * b < d[i] * cost; i++) {
cost += b;
calorie += d[i];
}
printf("%d\n", calorie / cost);
return 0;
}
|
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) { return *(int *)b - *(int *)a; }
int main() {
int n, a, b, c, d[10000], i, cost, calorie;
scanf("%d%d%d%d", &n, &a, &b, &c);
for (i = 0; i < n; i++) {
scanf("%d", &d[i]);
}
qsort(d, n, sizeof(int), cmp);
cost = a;
calorie = c;
for (i = 0; calorie * b < d[i] * cost; i++) {
cost += b;
calorie += d[i];
}
printf("%d\n", calorie / cost);
return 0;
}
|
[["+", 8, 9, 0, 37, 0, 16, 31, 66, 17, 48], ["+", 8, 9, 0, 37, 0, 16, 12, 66, 17, 48]]
| 0
| 175
|
#include <stdio.h>
int main() {
int n, a, b, c, d[100], y, i, p, j, tmp, max, k;
int price, calorie, cur;
scanf("%d %d %d %d", &n, &a, &b, &c);
for (i = 0; i < n; i++) {
scanf("%d", &d[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < i; j++) {
if (d[j] < d[i]) {
tmp = d[j];
d[j] = d[i];
d[j] = tmp;
}
}
}
price = a, calorie = c;
max = calorie / price;
for (i = 1; i < n; i++) {
price += b;
calorie += d[i];
cur = calorie / price;
if (max > cur) {
break;
}
max = cur;
}
printf("%d\n", max);
return 0;
}
|
#include <stdio.h>
int main() {
int n, a, b, c, d[100], y, i, p, j, tmp, max, k;
int price, calorie, cur;
scanf("%d %d %d %d", &n, &a, &b, &c);
for (i = 0; i < n; i++) {
scanf("%d", &d[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < i; j++) {
if (d[j] < d[i]) {
tmp = d[j];
d[j] = d[i];
d[i] = tmp;
}
}
}
price = a;
calorie = c;
max = calorie / price;
for (i = 0; i < n; i++) {
price += b;
calorie += d[i];
cur = calorie / price;
if (max > cur) {
break;
}
max = cur;
}
printf("%d\n", max);
return 0;
}
|
[["-", 64, 9, 0, 1, 0, 11, 31, 69, 71, 22], ["+", 64, 9, 0, 1, 0, 11, 31, 69, 71, 22], ["-", 0, 14, 8, 9, 0, 1, 0, 34, 0, 21], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13]]
| 0
| 231
|
#include <algorithm>
#include <iostream>
#include <map>
#define MAX 300
using namespace std;
int main(void) {
int n;
int a, b;
int pizza;
//今までの最高値を格納
double ans = 0;
int top[MAX];
cin >> n;
cin >> a >> b >> pizza;
for (int i = 0; i < n; i++) {
cin >> top[i];
}
sort(top, top + n, greater<int>());
//初期値
ans = pizza / a;
//貪欲法
for (int i = 0; i < n; i++) {
a += b;
pizza += top[i];
ans = max(ans, (double)pizza / a);
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#define MAX 300
using namespace std;
int main(void) {
int n;
int a, b;
int pizza;
//今までの最高値を格納
double ans = 0;
int top[MAX];
cin >> n;
cin >> a >> b >> pizza;
for (int i = 0; i < n; i++) {
cin >> top[i];
}
sort(top, top + n, greater<int>());
//初期値
ans = pizza / a;
//貪欲法
for (int i = 0; i < n; i++) {
a += b;
pizza += top[i];
ans = max(ans, (double)pizza / a);
}
cout << (int)ans << endl;
return 0;
}
|
[["+", 0, 1, 0, 16, 31, 16, 12, 74, 0, 24], ["+", 0, 16, 31, 16, 12, 74, 39, 77, 39, 40], ["+", 0, 1, 0, 16, 31, 16, 12, 74, 0, 25]]
| 1
| 152
|
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, x[100], d, p;
main() {
p = 0;
cin >> n;
cin >> a >> b;
cin >> c;
for (int i = 0; i < n; i++) {
cin >> x[i];
}
d = 0;
sort(x, x + n, greater<int>());
p = c / a;
for (int i = 0; i < n; i++) {
c += b;
a += x[i];
p = max(p, c / a);
}
cout << p << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, x[100], d, p;
main() {
p = 0;
cin >> n;
cin >> a >> b;
cin >> c;
for (int i = 0; i < n; i++) {
cin >> x[i];
}
d = 0;
sort(x, x + n, greater<int>());
p = c / a;
for (int i = 0; i < n; i++) {
a += b;
c += x[i];
p = max(p, c / a);
}
cout << p << endl;
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22]]
| 1
| 143
|
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n;
int a, b;
int c;
vector<int> d;
int main() {
scanf("%d %d %d %d", &n, &a, &b, &c);
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
d.push_back(t);
}
sort(d.rbegin(), d.rend());
/* printf("%d %d %d %d\n", n, a, b, c);
for(int i=0; i<n; i++) printf("%d ", d[i]);
printf("\n");
*/
int ans = 0;
for (int i = 0; i < n + 1; i++) {
int cal;
cal = c;
for (int j = 0; j < i; j++)
cal += d[j];
cal /= a + b * i;
if (ans < cal)
ans = cal;
else
break;
}
printf("%d\n", ans);
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n;
int a, b;
int c;
vector<int> d;
int main() {
scanf("%d %d %d %d", &n, &a, &b, &c);
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
d.push_back(t);
}
sort(d.rbegin(), d.rend());
/*
printf("%d %d %d %d\n", n, a, b, c);
for(int i=0; i<n; i++) printf("%d ", d[i]);
printf("\n");
*/
int ans = 0;
for (int i = 0; i < n + 1; i++) {
int cal;
cal = c;
for (int j = 0; j < i; j++)
cal += d[j];
cal /= a + b * i;
if (ans <= cal)
ans = cal;
else
break;
}
printf("%d\n", ans);
return 0;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
| 1
| 189
|
#include <algorithm>
#include <iostream>
using namespace std;
int n;
int A, B;
int pieC;
int C[100];
int main() {
cin >> n;
cin >> A >> B;
cin >> pieC;
for (int i = 0; i < n; i++) {
cin >> C[i];
}
// テゥツ卍催ゥツ??10,9,8,...)テ」ツ?ォテ」ツつステ」ツδシテ」ツδ暗」ツ?凖」ツつ?
sort(C, C + n);
reverse(C, C + n);
int calorie = pieC;
int doll = A;
int ans = 0;
for (int i = 0; i < n; i++) {
doll += B;
calorie += C[i];
if (calorie / doll > ans) {
ans = calorie / doll;
} else {
break;
}
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
int n;
int A, B;
int pieC;
int C[100];
int main() {
cin >> n;
cin >> A >> B;
cin >> pieC;
for (int i = 0; i < n; i++) {
cin >> C[i];
}
// テゥツ卍催ゥツ??10,9,8,...)テ」ツ?ォテ」ツつステ」ツδシテ」ツδ暗」ツ?凖」ツつ?
sort(C, C + n);
reverse(C, C + n);
int calorie = pieC;
int doll = A;
int ans = calorie / doll;
for (int i = 0; i < n; i++) {
doll += B;
calorie += C[i];
if (calorie / doll >= ans) {
ans = calorie / doll;
} else {
break;
}
}
cout << ans << endl;
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 31, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 85], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
| 1
| 159
|
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
int a, b, money, top;
double cal;
cin >> a >> money >> b >> cal;
vector<int> c(a);
for (int i = 0; i < a; i++) {
cin >> c[i];
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < a - i - 1; j++) {
if (c[i] < c[i + 1]) {
swap(c[i], c[i + 1]);
}
}
}
for (int i = 0; i < a; i++) {
if (cal / money < (cal + c[i]) / (money + b)) {
cal += c[i];
money += b;
}
}
int ans = cal / money;
cout << ans << endl;
}
|
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
int a, b, money, top;
double cal;
cin >> a >> money >> b >> cal;
vector<int> c(a);
for (int i = 0; i < a; i++) {
cin >> c[i];
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < a - i - 1; j++) {
if (c[j] < c[j + 1]) {
swap(c[j], c[j + 1]);
}
}
}
for (int i = 0; i < a; i++) {
if (cal / money < (cal + c[i]) / (money + b)) {
cal += c[i];
money += b;
}
}
int ans = cal / money;
cout << ans << endl;
}
|
[["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["-", 51, 16, 12, 69, 341, 342, 0, 16, 31, 22], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 31, 22], ["-", 0, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 0, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22]]
| 1
| 206
|
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int cal[10005];
int main() {
int n, a, b, c;
cin >> n >> a >> b >> c;
for (int i = 0; i < n; i++) {
cin >> cal[i];
}
sort(cal, cal + n);
double sumcal = c, sumdol = a;
double cado = sumcal / sumdol;
for (int i = n - 1; i > -1; i--) {
sumcal += cal[i];
sumdol += b;
double d = sumcal / sumdol;
if (d >= cado) {
cado = d;
} else {
cado = d;
break;
}
}
cado = floor(cado);
int ca = cado;
cout << ca << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int cal[10005];
int main() {
int n, a, b, c;
cin >> n >> a >> b >> c;
for (int i = 0; i < n; i++) {
cin >> cal[i];
}
sort(cal, cal + n);
double sumcal = c, sumdol = a;
double cado = sumcal / sumdol;
for (int i = n - 1; i > -1; i--) {
sumcal += cal[i];
sumdol += b;
double d = sumcal / sumdol;
if (d >= cado) {
cado = d;
} else {
break;
}
}
cado = floor(cado);
int ca = cado;
cout << ca << endl;
return 0;
}
|
[["-", 75, 76, 0, 9, 0, 1, 0, 11, 31, 22], ["-", 75, 76, 0, 9, 0, 1, 0, 11, 17, 32], ["-", 75, 76, 0, 9, 0, 1, 0, 11, 12, 22], ["-", 0, 57, 75, 76, 0, 9, 0, 1, 0, 35]]
| 1
| 167
|
# directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
# map info
visited = []
maps = []
# bfs algorithm
def bfs(h, w, cheese, sx, sy, moves):
que = [(moves, sx, sy)]
for i in range(h):
for j in range(w):
visited[i][j] = (maps[i][j] != 'X')
visited[sy][sx] = False
while len(que) > 0:
c_moves, c_x, c_y = que.pop(0)
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x]):
que.append((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# main function
if __name__ == '__main__':
str = (input()).split()
H = int(str[0])
W = int(str[1])
N = int(str[2])
visited = [[True for i in range(W)] for j in range(H)]
maps = ["" for i in range(H)]
x, y = 0, 0
for i in range(H):
maps[i] = input()
for j in range(W):
if maps[i][j] == 'S':
x, y = j, i
moves = 0
for i in range(1, N+1, 1):
moves, x, y = bfs(H, W, str(i), x, y, moves)
print(str(moves))
|
# directions
x_dir = [0, 1, 0, -1]
y_dir = [1, 0, -1, 0]
# map info
visited = []
maps = []
# bfs algorithm
def bfs(h, w, cheese, sx, sy, moves):
que = [(moves, sx, sy)]
for i in range(h):
for j in range(w):
visited[i][j] = (maps[i][j] != 'X')
visited[sy][sx] = False
while len(que) > 0:
c_moves, c_x, c_y = que.pop(0)
if maps[c_y][c_x] == cheese:
return (c_moves, c_x, c_y)
for i in range(4):
n_x = c_x + x_dir[i]
n_y = c_y + y_dir[i]
if(0 <= n_x < w and 0 <= n_y < h and visited[n_y][n_x]):
que.append((c_moves+1, n_x, n_y))
visited[n_y][n_x] = False
return (-1, -1, -1)
# main function
if __name__ == '__main__':
inputs = (input()).split()
H = int(inputs[0])
W = int(inputs[1])
N = int(inputs[2])
visited = [[True for i in range(W)] for j in range(H)]
maps = ["" for i in range(H)]
x, y = 0, 0
for i in range(H):
maps[i] = input()
for j in range(W):
if maps[i][j] == 'S':
x, y = j, i
moves = 0
for i in range(1, N+1, 1):
moves, x, y = bfs(H, W, str(i), x, y, moves)
print(str(moves))
|
[["-", 0, 57, 64, 196, 0, 1, 0, 662, 31, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 662, 31, 22], ["-", 0, 662, 12, 652, 3, 4, 0, 206, 51, 22], ["+", 0, 662, 12, 652, 3, 4, 0, 206, 51, 22]]
| 5
| 406
|
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)
def calculate_distance(field,locations,current_HP):
is_visit = [[False for _ in range(len(field[0]))] for __ in range(len(field))]
frontier = set()
frontier.add(locations[current_HP-1])
for step in range(10**4):
for cx,cy in frontier:
if (cx,cy)==locations[current_HP]: return step+1
is_visit[cx][cy]=True
next_frontier = set()
for cx,cy in list(frontier):
for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:
nx = cx+dx
ny = cy+dy
if not (0<=nx<len(field)): continue
if not (0<=ny<len(field[nx])): continue
if field[nx][ny]=='X': continue
if is_visit[nx][ny]: continue
next_frontier.add((nx,ny))
if len(next_frontier)==0: return
frontier = next_frontier
return -10*3
def solve(field,locations):
total_step=0
for current_HP in range(1,len(locations)):
total_step += calculate_distance(field,locations,current_HP)
return total_step
def main():
line=input().strip()
H,W,N=list(map(int,line.split(' ')))
field,locations=list(),dict()
for i in range(H):
field.append(list(input().strip()))
for j,x in enumerate(field[-1]):
if x=='S':
locations[0] = (i,j,)
if x.isdigit():
x = int(x)
field[-1][j] = x
locations[x] = (i,j,)
print(solve(field,locations))
if __name__=='__main__':
main()
|
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)
def calculate_distance(field,locations,current_HP):
is_visit = [[False for _ in range(len(field[0]))] for __ in range(len(field))]
frontier = set()
frontier.add(locations[current_HP-1])
for step in range(10**6):
for cx,cy in frontier:
if (cx,cy)==locations[current_HP]: return step
is_visit[cx][cy]=True
next_frontier = set()
for cx,cy in list(frontier):
for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:
nx = cx+dx
ny = cy+dy
if not (0<=nx<len(field)): continue
if not (0<=ny<len(field[nx])): continue
if field[nx][ny]=='X': continue
if is_visit[nx][ny]: continue
next_frontier.add((nx,ny))
if len(next_frontier)==0: return
frontier = next_frontier
return -10*3
def solve(field,locations):
total_step=0
for current_HP in range(1,len(locations)):
total_step += calculate_distance(field,locations,current_HP)
return total_step
def main():
line=input().strip()
H,W,N=list(map(int,line.split(' ')))
field,locations=list(),dict()
for i in range(H):
field.append(list(input().strip()))
for j,x in enumerate(field[-1]):
if x=='S':
locations[0] = (i,j,)
if x.isdigit():
x = int(x)
field[-1][j] = x
locations[x] = (i,j,)
print(solve(field,locations))
if __name__=='__main__':
main()
|
[["-", 0, 7, 12, 652, 3, 4, 0, 657, 12, 612], ["+", 0, 7, 12, 652, 3, 4, 0, 657, 12, 612], ["-", 0, 57, 64, 196, 0, 37, 0, 657, 17, 72], ["-", 0, 57, 64, 196, 0, 37, 0, 657, 12, 612]]
| 5
| 440
|
# -*- coding: utf-8 -*-
def get_shortest_distance(field,locations,current_HP):
is_visit = [[False for _ in range(len(field[0]))] for __ in range(len(field))]
frontier = set()
frontier.add(locations[current_HP-1])
for step in xrange(10**10):
for cx,cy in frontier:
if (cx,cy)==locations[current_HP]: return step
is_visit[cx][cy]=True
next_frontier = set()
for cx,cy in list(frontier):
for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:
nx = cx+dx
ny = cy+dy
if not (0<=nx<len(field)): continue
if not (0<=ny<len(field[nx])): continue
if field[nx][ny]=='X': continue
if is_visit[nx][ny]: continue
next_frontier.add((nx,ny))
# assert len(next_frontier)!=0,(current_HP,is_visit,frontier)
frontier = next_frontier
# assert True,('the goal is not reachable')
def solve(field,locations):
total_step=0
for current_HP in range(1,len(locations)):
total_step += get_shortest_distance(field,locations,current_HP)
return total_step
def main():
line=input().strip()
H,W,N=list(map(int,line.split(' ')))
field,locations=list(),dict()
for i in range(H):
field.append(list(input().strip()))
for j,x in enumerate(field[-1]):
if x=='S':
locations[0] = (i,j,)
if x.isdigit():
x = int(x)
field[-1][j] = x
locations[x] = (i,j,)
print(solve(field,locations))
if __name__=='__main__':
main()
|
# -*- coding: utf-8 -*-
def get_shortest_distance(field,locations,current_HP):
is_visit = [[False for _ in range(len(field[0]))] for __ in range(len(field))]
frontier = set()
frontier.add(locations[current_HP-1])
for step in range(10**10):
for cx,cy in frontier:
if (cx,cy)==locations[current_HP]: return step
is_visit[cx][cy]=True
next_frontier = set()
for cx,cy in list(frontier):
for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:
nx = cx+dx
ny = cy+dy
if not (0<=nx<len(field)): continue
if not (0<=ny<len(field[nx])): continue
if field[nx][ny]=='X': continue
if is_visit[nx][ny]: continue
next_frontier.add((nx,ny))
# assert len(next_frontier)!=0,(current_HP,is_visit,frontier)
frontier = next_frontier
# assert True,('the goal is not reachable')
def solve(field,locations):
total_step=0
for current_HP in range(1,len(locations)):
total_step += get_shortest_distance(field,locations,current_HP)
return total_step
def main():
line=input().strip()
H,W,N=list(map(int,line.split(' ')))
field,locations=list(),dict()
for i in range(H):
field.append(list(input().strip()))
for j,x in enumerate(field[-1]):
if x=='S':
locations[0] = (i,j,)
if x.isdigit():
x = int(x)
field[-1][j] = x
locations[x] = (i,j,)
print(solve(field,locations))
if __name__=='__main__':
main()
|
[["-", 0, 14, 8, 196, 0, 7, 12, 652, 63, 22], ["+", 0, 14, 8, 196, 0, 7, 12, 652, 63, 22]]
| 5
| 416
|
def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = input().strip()
H,W,N = map(int,first.split())
for i in range(H):
row = list(input().strip())
for j in range(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in range(len(field)):
for x in range(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,k+1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in range(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print(distance)
if __name__=='__main__':
main()
|
def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction:
nx = current[0] + d[0]
ny = current[1] + d[1]
if 0 <= nx < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
min_path[ny][nx] = min_path[current[1]][current[0]] + 1
if field[ny][nx] == tmp_N:
return nx,ny,min_path[ny][nx]
else:
que.append([nx,ny])
def getField():
matrix = []
first = input().strip()
H,W,N = map(int,first.split())
for i in range(H):
row = list(input().strip())
for j in range(W):
if row[j].isdigit():
row[j] = int(row[j])
matrix.append(row)
return matrix,H,W,N
def getStart(field):
for y in range(len(field)):
for x in range(len(field[0])):
if field[y][x] == "S":
return x,y
def main():
matrix,H,W,N = getField()
sx,sy = getStart(matrix)
distance = 0
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
for k in range(N-1):
tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
distance += tmp_dist
tmps = [tmp_x,tmp_y]
print(distance)
if __name__=='__main__':
main()
|
[["-", 0, 662, 12, 652, 3, 4, 0, 657, 31, 22], ["-", 0, 662, 12, 652, 3, 4, 0, 657, 17, 72]]
| 5
| 490
|
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
ny = y + [1,-1,0,0][i]
nx = x + [0,0,1,-1][i]
if ny >= 0 and ny < h and nx >= 0 and nx < w:
if bfs_map[ny][nx] == -1 and stage[ny][nx] != "x":
bfs_map[ny][nx] = bfs_map[y][x]+1
data_y.append(ny)
data_x.append(nx)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
print(sum)
|
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
ny = y + [1,-1,0,0][i]
nx = x + [0,0,1,-1][i]
if ny >= 0 and ny < h and nx >= 0 and nx < w:
if bfs_map[ny][nx] == -1 and stage[ny][nx] != "X":
bfs_map[ny][nx] = bfs_map[y][x]+1
data_y.append(ny)
data_x.append(nx)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
print(sum)
|
[["-", 0, 57, 15, 679, 12, 666, 0, 557, 0, 6], ["+", 0, 57, 15, 679, 12, 666, 0, 557, 0, 6]]
| 5
| 464
|
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
move_y = [1,-1,0,0]
move_x = [0,0,1,-1]
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
y += move_y[i]
x += move_x[i]
if y >= 0 and y < h and x >= 0 and x < w:
if bfs_map[y][x] == -1 and stage[y][x] != "x":
bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
data_y.append(y)
data_x.append(x)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
y -= move_y[i]
x -= move_x[i]
print(sum)
|
h,w,n = map(int, input().split())
stage = [input() for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i+1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
for x in range(w):
if stage[y][x] in starts:
starts_y[starts.index(stage[y][x])] = y
starts_x[starts.index(stage[y][x])] = x
if stage[y][x] in goals:
goals_y[goals.index(stage[y][x])] = y
goals_x[goals.index(stage[y][x])] = x
move_y = [1,-1,0,0]
move_x = [0,0,1,-1]
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
bfs_map = [[-1 for j in range(w)] for i in range(h)]
data_y = [start_y]
data_x = [start_x]
bfs_map[start_y][start_x] = 0
goal = False
while len(data_y) != 0 and not goal:
y = data_y.pop(0)
x = data_x.pop(0)
goal = False
for i in range(4):
y += move_y[i]
x += move_x[i]
if y >= 0 and y < h and x >= 0 and x < w:
if bfs_map[y][x] == -1 and stage[y][x] != "X":
bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1
data_y.append(y)
data_x.append(x)
if bfs_map[goal_y][goal_x] != -1:
sum += bfs_map[goal_y][goal_x]
goal = True
break
y -= move_y[i]
x -= move_x[i]
print(sum)
|
[["-", 0, 57, 15, 679, 12, 666, 0, 557, 0, 6], ["+", 0, 57, 15, 679, 12, 666, 0, 557, 0, 6]]
| 5
| 488
|
#import time, sys#
from collections import deque
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''
#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]
#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
for j in range(W):
if field[i][j] in nodes:
node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#
cost = 0
#count = 1#
for i in range(len(node_posi)-1):
#s2 = time.time()#
searched = [[-1]*W for i in range(H)]
start, goal = node_posi[i], node_posi[i+1]
Q = deque([start])
searched[start[1]][start[0]] = 0
goal_flag = False
while Q and not goal_flag:
x, y = Q.popleft()
goal = False
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if nx >= 0 and nx < W and ny >= 0 and ny < H:
if searched[ny][nx] == -1 and field[ny][nx] != 'X':
Q.append([nx, ny])
searched[ny][nx] = searched[y][x] + 1
if ([nx, ny] == goal):
cost += searched[ny][nx]
goal_flag = True
break
#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
#sys.stdout.write("\r{}".format(s))#
#sys.stdout.flush()#
#e2 = time.time()#
#print("{}:{}".format(count, e2-s2))#
#count += 1#
print(cost)
|
#import time, sys#
from collections import deque
H, W, N = map(int, input().split())
field = [list(input()) for i in range(H)]
'''
20 30 9
.......X..8.XXXX.XX..XX.X.XX..
X.XX....X.XX..X.X.X....XXXX..X
..X.X..XX..X.....XX.X..X.XX...
....X.X.X.XXX.X..X..X.X..XX.X.
..XXX.XX...X.X....X.X..X..X...
..X...........X.X...X.1.XX...X
.X.X....X..XX..XX......9.X...4
..X....X........X......X......
....XXX.X...XX.X.....X.X..X.X.
..S..XXX..X....X.X.......X..X.
.5...XX..X.X..X....X...XXX....
..X...X.X....X....X..X.2X.....
XXXXXXXX.X.XX..XX..XX.......X.
.....X...........3..XX.XXX.X.X
..X.....X..X..XX.XX..X.X...XX.
...X..X.XXX.XX......X...X..XXX
.X.XX.X.........X.X...X...X..X
..X...X..X..XX..XXX..X.XX...X.
X....X.X....X...X.X..XX.XX..X.
...X6X..X........X.X.....7.X..
'''
#input = input().split('\n')#
#H, W, N = map(int, input.pop(0).split())#
#field = [line for line in input]#
node_posi = [0 for i in range(N+1)]
#s1 = time.time()#
nodes = 'S123456789'
for i in range(H):
for j in range(W):
if field[i][j] in nodes:
node_posi[nodes.index(field[i][j])] = [j, i]
#e1 = time.time()#
#print("b:{}".format(e1-s1))#
cost = 0
#count = 1#
for i in range(len(node_posi)-1):
#s2 = time.time()#
searched = [[-1]*W for i in range(H)]
start, goal = node_posi[i], node_posi[i+1]
Q = deque([start])
searched[start[1]][start[0]] = 0
goal_flag = False
while Q and not goal_flag:
x, y = Q.popleft()
goal_flag = False
for q in range(4):
nx = x + [1,0,-1,0][q]
ny = y + [0,1,0,-1][q]
if nx >= 0 and nx < W and ny >= 0 and ny < H:
if searched[ny][nx] == -1 and field[ny][nx] != 'X':
Q.append([nx, ny])
searched[ny][nx] = searched[y][x] + 1
if ([nx, ny] == goal):
cost += searched[ny][nx]
goal_flag = True
break
#s = "\n".join(["".join(map(lambda x: str(x)[:1], j)) for j in searched])#
#sys.stdout.write("\r{}".format(s))#
#sys.stdout.flush()#
#e2 = time.time()#
#print("{}:{}".format(count, e2-s2))#
#count += 1#
print(cost)
|
[["-", 0, 52, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 662, 31, 22]]
| 5
| 354
|
#include <iostream>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
#define mod 100000
using namespace std;
int H, W, dp[2][1 << 19][2][2];
string M[20];
int main() {
scanf("%d", &H);
scanf("%d", &W);
for (int i = 0; i < H; i++)
cin >> M[i];
if (M[0][0] == 'J' || M[0][0] == '?')
dp[0][0][0][0] = 1;
if (M[0][0] == 'O' || M[0][0] == '?')
dp[0][0][1][0] = 1;
if (M[0][0] == 'I' || M[0][0] == '?')
dp[0][0][1][0] = 1;
for (int i = 0; i < H * W - 1; i++) {
for (int j = 0; j < (1 << (W - 1)); j++) {
dp[(i + 1) % 2][j][0][0] = 0;
dp[(i + 1) % 2][j][0][1] = 0;
dp[(i + 1) % 2][j][1][0] = 0;
dp[(i + 1) % 2][j][1][1] = 0;
}
for (int j = 0; j < (1 << (W - 1)); j++) {
char c = M[(i + 1) / W][(i + 1) % W];
int nxt = (j << 1) % (1 << (W - 1));
int sum0 = (dp[i % 2][j][0][0] + dp[i % 2][j][1][0]) % mod;
int sum1 = (dp[i % 2][j][0][1] + dp[i % 2][j][1][1]) % mod;
if (c == 'J' || c == '?') {
dp[(i + 1) % 2][nxt][0][0] += sum0;
dp[(i + 1) % 2][nxt][0][1] += sum1;
dp[(i + 1) % 2][nxt][0][0] %= mod;
dp[(i + 1) % 2][nxt][0][1] %= mod;
}
if (c == 'O' || c == '?') {
if ((M[i / W][i % W] == 'J' || M[i / W][i % W] == '?') &&
i % W != W - 1) {
dp[(i + 1) % 2][nxt + 1][1][0] += dp[i % 2][j][0][0];
dp[(i + 1) % 2][nxt + 1][1][1] += dp[i % 2][j][0][1];
dp[(i + 1) % 2][nxt + 1][1][0] %= mod;
dp[(i + 1) % 2][nxt + 1][1][1] %= mod;
dp[(i + 1) % 2][nxt][1][0] += (sum0 - dp[i % 2][j][0][0] + mod);
dp[(i + 1) % 2][nxt][1][1] += (sum1 - dp[i % 2][j][0][1] + mod);
dp[(i + 1) % 2][nxt][1][0] %= mod;
dp[(i + 1) % 2][nxt][1][1] %= mod;
} else {
dp[(i + 1) % 2][nxt][1][0] += sum0;
dp[(i + 1) % 2][nxt][1][1] += sum1;
dp[(i + 1) % 2][nxt][1][0] %= mod;
dp[(i + 1) % 2][nxt][1][1] %= mod;
}
}
if (c == 'I' || c == '?') {
if (j & (1 << (W - 2))) {
dp[(i + 1) % 2][nxt][1][1] += sum0;
} else {
dp[(i + 1) % 2][nxt][1][0] += sum0;
}
dp[(i + 1) % 2][nxt][1][1] += sum1;
dp[(i + 1) % 2][nxt][1][0] %= mod;
dp[(i + 1) % 2][nxt][1][1] %= mod;
}
}
}
int ret = 0;
for (int i = 0; i < (1 << (W - 1)); i++) {
ret += dp[(H * W - 1) % 2][i][0][1];
ret += dp[(H * W - 1) % 2][i][1][1];
ret %= mod;
}
printf("%d\n", ret);
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
#define mod 100000
using namespace std;
int H, W, dp[2][1 << 19][2][2];
string M[20];
int main() {
scanf("%d", &H);
scanf("%d", &W);
for (int i = 0; i < H; i++)
cin >> M[i];
if (M[0][0] == 'J' || M[0][0] == '?')
dp[0][0][0][0] += 1;
if (M[0][0] == 'O' || M[0][0] == '?')
dp[0][0][1][0] += 1;
if (M[0][0] == 'I' || M[0][0] == '?')
dp[0][0][1][0] += 1;
for (int i = 0; i < H * W - 1; i++) {
for (int j = 0; j < (1 << (W - 1)); j++) {
dp[(i + 1) % 2][j][0][0] = 0;
dp[(i + 1) % 2][j][0][1] = 0;
dp[(i + 1) % 2][j][1][0] = 0;
dp[(i + 1) % 2][j][1][1] = 0;
}
for (int j = 0; j < (1 << (W - 1)); j++) {
char c = M[(i + 1) / W][(i + 1) % W];
int nxt = (j << 1) % (1 << (W - 1));
int sum0 = (dp[i % 2][j][0][0] + dp[i % 2][j][1][0]) % mod;
int sum1 = (dp[i % 2][j][0][1] + dp[i % 2][j][1][1]) % mod;
if (c == 'J' || c == '?') {
dp[(i + 1) % 2][nxt][0][0] += sum0;
dp[(i + 1) % 2][nxt][0][1] += sum1;
dp[(i + 1) % 2][nxt][0][0] %= mod;
dp[(i + 1) % 2][nxt][0][1] %= mod;
}
if (c == 'O' || c == '?') {
if ((M[i / W][i % W] == 'J' || M[i / W][i % W] == '?') &&
i % W != W - 1) {
dp[(i + 1) % 2][nxt + 1][1][0] += dp[i % 2][j][0][0];
dp[(i + 1) % 2][nxt + 1][1][1] += dp[i % 2][j][0][1];
dp[(i + 1) % 2][nxt + 1][1][0] %= mod;
dp[(i + 1) % 2][nxt + 1][1][1] %= mod;
dp[(i + 1) % 2][nxt][1][0] += (sum0 - dp[i % 2][j][0][0] + mod);
dp[(i + 1) % 2][nxt][1][1] += (sum1 - dp[i % 2][j][0][1] + mod);
dp[(i + 1) % 2][nxt][1][0] %= mod;
dp[(i + 1) % 2][nxt][1][1] %= mod;
} else {
dp[(i + 1) % 2][nxt][1][0] += sum0;
dp[(i + 1) % 2][nxt][1][1] += sum1;
dp[(i + 1) % 2][nxt][1][0] %= mod;
dp[(i + 1) % 2][nxt][1][1] %= mod;
}
}
if (c == 'I' || c == '?') {
if (j & (1 << (W - 2))) {
dp[(i + 1) % 2][nxt][1][1] += sum0;
} else {
dp[(i + 1) % 2][nxt][1][0] += sum0;
}
dp[(i + 1) % 2][nxt][1][1] += sum1;
dp[(i + 1) % 2][nxt][1][0] %= mod;
dp[(i + 1) % 2][nxt][1][1] %= mod;
}
}
}
int ret = 0;
for (int i = 0; i < (1 << (W - 1)); i++) {
ret += dp[(H * W - 1) % 2][i][0][1];
ret += dp[(H * W - 1) % 2][i][1][1];
ret %= mod;
}
printf("%d\n", ret);
return 0;
}
|
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 17, 32], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 17, 107]]
| 1
| 1,239
|
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
#define mod 100000
using namespace std;
int H, W, dp[2][1 << 19][2][2], bits[10946];
string M[20];
int main() {
scanf("%d", &H);
scanf("%d", &W);
for (int i = 0; i < H; i++)
cin >> M[i];
if (M[0][0] == 'J' || M[0][0] == '?')
dp[0][0][0][0] += 1;
if (M[0][0] == 'O' || M[0][0] == '?')
dp[0][0][1][0] += 1;
if (M[0][0] == 'I' || M[0][0] == '?')
dp[0][0][1][0] += 1;
int maxbits = 0;
for (int i = 0; i < (1 << (W - 1)); i++) {
bool ok = true;
for (int j = 0; j < W - 2; j++) {
if ((i & (1 << j)) && (i & (1 << (j + 1)))) {
ok = false;
break;
}
}
if (ok)
bits[maxbits++] = i;
}
for (int i = 0; i < H * W - 1; i++) {
for (int j = 0; j < maxbits; j++) {
dp[(i ^ 1) & 1][bits[j]][0][0] = 0;
dp[(i ^ 1) & 1][bits[j]][0][1] = 0;
dp[(i ^ 1) & 1][bits[j]][1][0] = 0;
dp[(i ^ 1) & 1][bits[j]][1][1] = 0;
}
for (int j = 0; j < maxbits; j++) {
char c = M[(i + 1) / W][(i + 1) % W];
int nxt = (bits[j] << 1) % (1 << (W - 1));
int sum0 = (dp[i & 1][bits[j]][0][0] + dp[i & 1][bits[j]][1][0]) % mod;
int sum1 = (dp[i & 1][bits[j]][0][1] + dp[i & 1][bits[j]][1][1]) % mod;
if (c == 'J' || c == '?') {
dp[(i ^ 1) & 1][nxt][0][0] += sum0;
dp[(i ^ 1) & 1][nxt][0][1] += sum1;
}
if (c == 'O' || c == '?') {
if ((M[i / W][i % W] == 'J' || M[i / W][i % W] == '?') &&
i % W != W - 1) {
dp[(i ^ 1) & 1][nxt + 1][1][0] += dp[i & 1][bits[j]][0][0];
dp[(i ^ 1) & 1][nxt + 1][1][1] += dp[i & 1][bits[j]][0][1];
dp[(i ^ 1) & 1][nxt + 1][1][0] %= mod;
dp[(i ^ 1) & 1][nxt + 1][1][1] %= mod;
dp[(i ^ 1) & 1][nxt][1][0] += (sum0 - dp[i & 1][j][0][0] + mod);
dp[(i ^ 1) & 1][nxt][1][1] += (sum1 - dp[i & 1][j][0][1] + mod);
} else {
dp[(i ^ 1) & 1][nxt][1][0] += sum0;
dp[(i ^ 1) & 1][nxt][1][1] += sum1;
}
}
if (c == 'I' || c == '?') {
if (bits[j] & (1 << (W - 2))) {
dp[(i ^ 1) & 1][nxt][1][1] += sum0;
} else {
dp[(i ^ 1) & 1][nxt][1][0] += sum0;
}
dp[(i ^ 1) & 1][nxt][1][1] += sum1;
}
dp[(i ^ 1) & 1][nxt][1][0] %= mod;
dp[(i ^ 1) & 1][nxt][1][1] %= mod;
}
}
int ret = 0;
for (int i = 0; i < maxbits; i++) {
ret += dp[(H * W - 1) % 2][bits[i]][0][1];
ret += dp[(H * W - 1) % 2][bits[i]][1][1];
ret %= mod;
}
printf("%d\n", ret);
return 0;
}
|
#include <iostream>
#include <string>
#pragma warning(disable : 4996)
#define mod 100000
using namespace std;
int H, W, dp[2][1 << 19][2][2], bits[10946];
string M[20];
int main() {
scanf("%d", &H);
scanf("%d", &W);
for (int i = 0; i < H; i++)
cin >> M[i];
if (M[0][0] == 'J' || M[0][0] == '?')
dp[0][0][0][0] += 1;
if (M[0][0] == 'O' || M[0][0] == '?')
dp[0][0][1][0] += 1;
if (M[0][0] == 'I' || M[0][0] == '?')
dp[0][0][1][0] += 1;
int maxbits = 0;
for (int i = 0; i < (1 << (W - 1)); i++) {
bool ok = true;
for (int j = 0; j < W - 2; j++) {
if ((i & (1 << j)) && (i & (1 << (j + 1)))) {
ok = false;
break;
}
}
if (ok)
bits[maxbits++] = i;
}
for (int i = 0; i < H * W - 1; i++) {
for (int j = 0; j < maxbits; j++) {
dp[(i ^ 1) & 1][bits[j]][0][0] = 0;
dp[(i ^ 1) & 1][bits[j]][0][1] = 0;
dp[(i ^ 1) & 1][bits[j]][1][0] = 0;
dp[(i ^ 1) & 1][bits[j]][1][1] = 0;
}
for (int j = 0; j < maxbits; j++) {
char c = M[(i + 1) / W][(i + 1) % W];
int nxt = (bits[j] << 1) % (1 << (W - 1));
int sum0 = (dp[i & 1][bits[j]][0][0] + dp[i & 1][bits[j]][1][0]) % mod;
int sum1 = (dp[i & 1][bits[j]][0][1] + dp[i & 1][bits[j]][1][1]) % mod;
if (c == 'J' || c == '?') {
dp[(i ^ 1) & 1][nxt][0][0] += sum0;
dp[(i ^ 1) & 1][nxt][0][1] += sum1;
}
if (c == 'O' || c == '?') {
if ((M[i / W][i % W] == 'J' || M[i / W][i % W] == '?') &&
i % W != W - 1) {
dp[(i ^ 1) & 1][nxt + 1][1][0] += dp[i & 1][bits[j]][0][0];
dp[(i ^ 1) & 1][nxt + 1][1][1] += dp[i & 1][bits[j]][0][1];
dp[(i ^ 1) & 1][nxt + 1][1][0] %= mod;
dp[(i ^ 1) & 1][nxt + 1][1][1] %= mod;
dp[(i ^ 1) & 1][nxt][1][0] += (sum0 - dp[i & 1][bits[j]][0][0] + mod);
dp[(i ^ 1) & 1][nxt][1][1] += (sum1 - dp[i & 1][bits[j]][0][1] + mod);
} else {
dp[(i ^ 1) & 1][nxt][1][0] += sum0;
dp[(i ^ 1) & 1][nxt][1][1] += sum1;
}
}
if (c == 'I' || c == '?') {
if (bits[j] & (1 << (W - 2))) {
dp[(i ^ 1) & 1][nxt][1][1] += sum0;
} else {
dp[(i ^ 1) & 1][nxt][1][0] += sum0;
}
dp[(i ^ 1) & 1][nxt][1][1] += sum1;
}
dp[(i ^ 1) & 1][nxt][1][0] %= mod;
dp[(i ^ 1) & 1][nxt][1][1] %= mod;
}
}
int ret = 0;
for (int i = 0; i < maxbits; i++) {
ret += dp[(H * W - 1) % 2][bits[i]][0][1];
ret += dp[(H * W - 1) % 2][bits[i]][1][1];
ret %= mod;
}
printf("%d\n", ret);
return 0;
}
|
[["+", 28, 69, 28, 69, 341, 342, 0, 69, 28, 22], ["+", 28, 69, 341, 342, 0, 69, 341, 342, 0, 70], ["+", 28, 69, 341, 342, 0, 69, 341, 342, 0, 73], ["+", 12, 69, 28, 69, 28, 69, 341, 342, 0, 70]]
| 1
| 1,226
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FI first
#define SE second
#define PB push_back
#define MT make_tuple
using namespace std;
typedef tuple<int, int> TI1;
typedef tuple<int, int, int> TI2;
static const int INF = 1ll << 24;
static const int MBIT = 1 << 20;
static const int mod = 100000;
static const int dx[] = {
1,
-1,
0,
0,
};
static const int dy[] = {0, 0, 1, -1};
short N, M;
short fld[21 * 21];
short dp[2][3][MBIT];
int ans = 0;
int all = 1;
int main() {
cin >> N >> M;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
char c;
cin >> c;
if (c == '?')
fld[i * M + j] = 3, all *= 3;
if (c == 'J')
fld[i * M + j] = 0;
if (c == 'O')
fld[i * M + j] = 1;
if (c == 'I')
fld[i * M + j] = 2;
all %= mod;
}
}
if (fld[0] != 3)
dp[0][fld[0]][0] = 1;
else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
int maxbit = 0;
for (int i = 0; i < M; ++i) {
maxbit += pow(2, i);
}
int bit = 1 << M;
int bit1 = 1 << (M - 1);
int bit2 = 1 << (M - 2);
for (int i = 0; i < M * N - 1; ++i) {
int p1 = (i + 1) & 1;
int p2 = i & 1;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k <= maxbit; ++k) {
int t1 = k << 1;
if (t1 & bit)
t1 -= bit;
int t2 = t1 + 1;
if ((k & bit1) && j == 2 && (i + 1) % M != 0)
continue;
if (fld[i + 1] == 3) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][k]) % mod;
if (!(k & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][k]) % mod;
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][k]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][k]) % mod;
} else if (fld[i + 1] == 0) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][k]) % mod;
} else if (fld[i + 1] == 1) {
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][k]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][k]) % mod;
} else if (fld[i + 1] == 2) {
if (!(k & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][k]) % mod;
}
dp[p2][j][k] = 0;
}
}
}
for (int j = 0; j < 3; ++j) {
for (int k = 0; k <= maxbit; ++k) {
ans += dp[(M * N - 1) & 1][j][k];
ans %= mod;
}
}
cout << (all % mod - ans % mod + mod) % mod << endl;
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FI first
#define SE second
#define PB push_back
#define MT make_tuple
using namespace std;
typedef tuple<int, int> TI1;
typedef tuple<int, int, int> TI2;
static const int INF = 1ll << 24;
static const int MBIT = 1048576;
static const int mod = 100000;
static const int dx[] = {
1,
-1,
0,
0,
};
static const int dy[] = {0, 0, 1, -1};
short N, M;
short fld[21 * 21];
int dp[2][3][MBIT];
int ans = 0;
int all = 1;
int main() {
// int sum = 0;
// for (int i = 0; i < 20; ++i)sum += pow(2, i);
// cout << sum << " " << (int)pow(2, 20) << endl;
cin >> N >> M;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
char c;
cin >> c;
if (c == '?')
fld[i * M + j] = 3, all *= 3;
if (c == 'J')
fld[i * M + j] = 0;
if (c == 'O')
fld[i * M + j] = 1;
if (c == 'I')
fld[i * M + j] = 2;
all %= mod;
}
}
if (fld[0] != 3)
dp[0][fld[0]][0] = 1;
else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
int maxbit = 0;
for (int i = 0; i < M; ++i) {
maxbit += pow(2, i);
}
int bit = 1 << M;
int bit1 = 1 << (M - 1);
int bit2 = 1 << (M - 2);
for (int i = 0; i < M * N - 1; ++i) {
int p1 = (i + 1) & 1;
int p2 = i & 1;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k <= maxbit; ++k) {
int t1 = k << 1;
if (t1 & bit)
t1 -= bit;
int t2 = t1 + 1;
if ((k & bit1) && j == 2 && (i + 1) % M != 0)
continue;
if (fld[i + 1] == 3) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][k]) % mod;
if (!(k & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][k]) % mod;
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][k]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][k]) % mod;
} else if (fld[i + 1] == 0) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][k]) % mod;
} else if (fld[i + 1] == 1) {
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][k]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][k]) % mod;
} else if (fld[i + 1] == 2) {
if (!(k & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][k]) % mod;
}
dp[p2][j][k] = 0;
}
}
}
for (int j = 0; j < 3; ++j) {
for (int k = 0; k <= maxbit; ++k) {
ans += dp[(M * N - 1) & 1][j][k];
ans %= mod;
}
}
cout << (all % mod - ans % mod + mod) % mod << endl;
return 0;
}
|
[["-", 0, 30, 0, 43, 49, 50, 51, 16, 31, 13], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 17, 151], ["-", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 36, 36, 0, 30, 0, 43, 39, 86, 0, 133], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40]]
| 1
| 1,058
|
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int mod = 100000;
int m, n;
char str[25][25];
//メモリの容量上直前の2マスのみ保存
/* 1~x-1番目のbitは, その場所を基準として, JOが続くか否かを表す.
x番目のbitは, そこにJが来るか来ないかを表す.
(JOが作れるか作れないか知るために必要) */
int dp[2][1 << 20][3];
int ans = 0;
char joi[4] = "JOI";
//\bitDPできない勢/
int main(void) {
//入力
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", str[i]);
}
//以降JOIができるパターンを数える
//さいしょ
dp[0][0][0] = 1;
//使いまわすための変数
int cur = 0, next = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//初期化
memset(dp[next], 0, sizeof(dp[next]));
for (int bit = 0; bit < (1 << m); bit++) {
for (int p = 0; p < 2; p++) {
if (dp[cur][bit][p]) {
for (int k = 0; k < 3; k++) {
//条件に全く当てはまらないのは無視
if (str[i][j] != '?' && str[i][j] != joi[k])
continue;
if ((bit & 1 << j) && k == 2)
continue;
// nbit= 現在のビット AND 1<<jの反転
// np= kが0であれば1、そうでなければ0
int nbit = bit & ~(1 << j), np = k == 0;
if (p && k == 1)
nbit |= (1 << j) - 1;
dp[next][nbit][np] += dp[cur][bit][p];
// modで割る
if (dp[next][nbit][np] >= mod)
dp[next][nbit][np] -= mod;
}
}
}
}
//メモリを使い回すためにスワップ
swap(cur, next);
}
for (int bit = 0; bit < (1 << m); bit++) {
//条件を満たしたものを足す
dp[cur][bit][0] += dp[cur][bit][1];
// modで割る
if (dp[cur][bit][0] >= mod)
dp[cur][bit][0] -= mod;
dp[cur][bit][1] = 0;
}
}
int ans = 1;
//パターンが何通りあるか数える(?1つにつき×3、modで割る)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (str[i][j] == '?')
ans = (ans * 3) % mod;
}
}
//全パターンからJOIができるパターンを引く
for (int i = 0; i < (1 << m); i++)
ans = (ans + mod - dp[cur][i][0]) % mod;
cout << ans << endl;
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int mod = 100000;
int m, n;
char str[25][25];
//メモリの容量上直前の2マスのみ保存
/* 1~x-1番目のbitは, その場所を基準として, JOが続くか否かを表す.
x番目のbitは, そこにJが来るか来ないかを表す.
(JOが作れるか作れないか知るために必要) */
int dp[2][1 << 20][3];
int ans = 0;
char joi[4] = "JOI";
//\bitDPできない勢/
int main(void) {
//入力
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", str[i]);
}
//以降JOIができるパターンを数える
//さいしょ
dp[0][0][0] = 1;
//使いまわすための変数
int cur = 0, next = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//初期化
memset(dp[next], 0, sizeof(dp[next]));
for (int bit = 0; bit < (1 << m); bit++) {
for (int p = 0; p < 2; p++) {
if (dp[cur][bit][p]) {
for (int k = 0; k < 3; k++) {
//条件に全く当てはまらないのは無視
if (str[i][j] != '?' && str[i][j] != joi[k])
continue;
if ((bit & 1 << j) && k == 2)
continue;
// nbit= 現在のビット AND 1<<jの反転
// np= kが0であれば1、そうでなければ0
int nbit = bit & ~(1 << j), np = k == 0;
if (p && k == 1)
nbit |= 1 << (j - 1);
dp[next][nbit][np] += dp[cur][bit][p];
// modで割る
if (dp[next][nbit][np] >= mod)
dp[next][nbit][np] -= mod;
}
}
}
}
//メモリを使い回すためにスワップ
swap(cur, next);
}
for (int bit = 0; bit < (1 << m); bit++) {
//条件を満たしたものを足す
dp[cur][bit][0] += dp[cur][bit][1];
// modで割る
if (dp[cur][bit][0] >= mod)
dp[cur][bit][0] -= mod;
dp[cur][bit][1] = 0;
}
}
int ans = 1;
//パターンが何通りあるか数える(?1つにつき×3、modで割る)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (str[i][j] == '?')
ans = (ans * 3) % mod;
}
}
//全パターンからJOIができるパターンを引く
for (int i = 0; i < (1 << m); i++)
ans = (ans + mod - dp[cur][i][0]) % mod;
cout << ans << endl;
return 0;
}
|
[["-", 64, 1, 0, 11, 12, 16, 31, 23, 0, 24], ["+", 64, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["-", 64, 1, 0, 11, 12, 16, 31, 23, 0, 25], ["+", 64, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
| 1
| 593
|
#include <stdio.h>
#include <string.h>
char field[1001][1002];
int J[1001][1001], O[1001][1001], I[1001][1001];
int main(void) {
int M, N, K, i, j, a, b, c, d;
scanf("%d%d", &M, &N);
scanf("%d", &K);
for (i = 0; i <= M; i++) {
for (j = 0; j <= N; j++) {
J[i][j] = O[i][j] = I[i][j] = 0;
}
}
for (i = 1; i <= M; i++) {
scanf("%s\n", &(field[i][1]));
for (j = 1; j <= N; j++) {
J[i][j] = J[i][j - 1] + J[i - 1][j] - J[i - 1][j - 1];
O[i][j] = O[i][j - 1] + O[i - 1][j] - O[i - 1][j - 1];
I[i][j] = I[i][j - 1] + I[i - 1][j] - I[i - 1][j - 1];
switch (field[i][j]) {
case 'J':
J[i][j]++;
break;
case 'O':
O[i][j]++;
break;
case 'I':
I[i][j]++;
break;
default:
break;
}
}
}
for (i = 0; i < M; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("%d %d %d\n", J[c][d] - J[c][b - 1] - J[a - 1][d] + J[a - 1][b - 1],
O[c][d] - O[c][b - 1] - O[a - 1][d] + O[a - 1][b - 1],
I[c][d] - I[c][b - 1] - I[a - 1][d] + I[a - 1][b - 1]);
}
return 0;
}
|
#include <stdio.h>
#include <string.h>
char field[1001][1002];
int J[1001][1001], O[1001][1001], I[1001][1001];
int main(void) {
int M, N, K, i, j, a, b, c, d;
scanf("%d%d", &M, &N);
scanf("%d", &K);
for (i = 0; i <= M; i++) {
for (j = 0; j <= N; j++) {
J[i][j] = O[i][j] = I[i][j] = 0;
}
}
for (i = 1; i <= M; i++) {
scanf("%s\n", &(field[i][1]));
for (j = 1; j <= N; j++) {
J[i][j] = J[i][j - 1] + J[i - 1][j] - J[i - 1][j - 1];
O[i][j] = O[i][j - 1] + O[i - 1][j] - O[i - 1][j - 1];
I[i][j] = I[i][j - 1] + I[i - 1][j] - I[i - 1][j - 1];
switch (field[i][j]) {
case 'J':
J[i][j]++;
break;
case 'O':
O[i][j]++;
break;
case 'I':
I[i][j]++;
break;
default:
break;
}
}
}
for (i = 0; i < K; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("%d %d %d\n", J[c][d] - J[c][b - 1] - J[a - 1][d] + J[a - 1][b - 1],
O[c][d] - O[c][b - 1] - O[a - 1][d] + O[a - 1][b - 1],
I[c][d] - I[c][b - 1] - I[a - 1][d] + I[a - 1][b - 1]);
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
| 0
| 541
|
#include <bits/stdc++.h>
using namespace std;
template <typename T> class BIT {
private:
int n, m;
vector<vector<T>> bit;
public:
void add(int i, int j, T val) {
for (int i_ = i + 1; i_ < n; i_ += i_ & -i_)
for (int j_ = j + 1; j_ < m; j_ += j_ & -j_)
bit[i_][j_] += val;
}
T sum(int i, int j) {
T s = 0;
for (int i_ = i + 1; i_ > 0; i_ -= i_ & -i_)
for (int j_ = j + 1; j_ > 0; j_ -= j_ & -j_)
s += bit[i_][j_];
return s;
}
// [lx, rx)×[ly, ry)の範囲の和を求める
T sum(int lx, int rx, int ly, int ry) {
return sum(rx - 1, ry - 1) - sum(lx - 1, ry - 1) - sum(rx - 1, ly - 1) +
sum(lx - 1, ly - 1);
}
BIT(int sz1, int sz2) {
n = sz1 + 1, m = sz2 + 1;
bit.resize(n, vector<T>(m, 0));
}
BIT(vector<vector<T>> &v) {
n = (int)v.size() + 1, m = (int)v[0].size() + 1;
bit.resize(n, vector<T>(m, 0));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
add(i, j, v[i][j]);
}
void print_sum() {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cout << sum(i - 1, j - 1) << " ";
cout << "\n";
}
};
int main() {
int m, n, K;
cin >> m >> n >> K;
vector<BIT<int>> bt(3, BIT<int>(m, n));
vector<string> s(m);
for (int i = 0; i < m; i++) {
cin >> s[i];
for (int j = 0; j < n; j++) {
if (s[i][j] == 'J') {
bt[0].add(i, j, 1);
} else if (s[i][j] == 'O') {
bt[1].add(i, j, 1);
} else {
bt[2].add(i, j, 1);
}
}
}
// bt[0].print();
for (int i = 0; i < K; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a, --b;
cout << bt[0].sum(a, b, c, d) << " " << bt[1].sum(a, b, c, d) << " "
<< bt[2].sum(a, b, c, d) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T> class BIT {
private:
int n, m;
vector<vector<T>> bit;
public:
void add(int i, int j, T val) {
for (int i_ = i + 1; i_ < n; i_ += i_ & -i_)
for (int j_ = j + 1; j_ < m; j_ += j_ & -j_)
bit[i_][j_] += val;
}
T sum(int i, int j) {
T s = 0;
for (int i_ = i + 1; i_ > 0; i_ -= i_ & -i_)
for (int j_ = j + 1; j_ > 0; j_ -= j_ & -j_)
s += bit[i_][j_];
return s;
}
// [lx, rx)×[ly, ry)の範囲の和を求める
T sum(int lx, int ly, int rx, int ry) {
return sum(rx - 1, ry - 1) - sum(lx - 1, ry - 1) - sum(rx - 1, ly - 1) +
sum(lx - 1, ly - 1);
}
BIT(int sz1, int sz2) {
n = sz1 + 1, m = sz2 + 1;
bit.resize(n, vector<T>(m, 0));
}
BIT(vector<vector<T>> &v) {
n = (int)v.size() + 1, m = (int)v[0].size() + 1;
bit.resize(n, vector<T>(m, 0));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
add(i, j, v[i][j]);
}
void print_sum() {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cout << sum(i - 1, j - 1) << " ";
cout << "\n";
}
};
int main() {
int m, n, K;
cin >> m >> n >> K;
vector<BIT<int>> bt(3, BIT<int>(m, n));
vector<string> s(m);
for (int i = 0; i < m; i++) {
cin >> s[i];
for (int j = 0; j < n; j++) {
if (s[i][j] == 'J') {
bt[0].add(i, j, 1);
} else if (s[i][j] == 'O') {
bt[1].add(i, j, 1);
} else {
bt[2].add(i, j, 1);
}
}
}
// bt[0].print();
for (int i = 0; i < K; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a, --b;
cout << bt[0].sum(a, b, c, d) << " " << bt[1].sum(a, b, c, d) << " "
<< bt[2].sum(a, b, c, d) << "\n";
}
return 0;
}
|
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 49, 22], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 49, 22]]
| 1
| 705
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, x) for (int i = 0; i < x; ++i)
#define FOR(i, x, y) for (int i = x; i < y; ++i)
#define ALL(a) a.begin(), a.end()
using ll = long long;
int m, n, k;
inline int hash_(char c) { return ((c == 'J') ? 0 : ((c == 'O') ? 1 : 2)); }
int imos[3][1234][1234];
int main() {
cin >> m >> n;
cin >> k;
REP(i, m) {
char str[1234];
cin >> str;
REP(j, n) {
int id = hash_(str[j]);
imos[id][i + 1][j + 1] = 1;
}
}
REP(id, 3) {
REP(j, n + 1) {
REP(i, m + 1) { imos[id][i + 1][j] += imos[id][i][j]; }
}
}
REP(id, 3) {
REP(j, n + 1) {
REP(i, m + 1) { imos[id][i][j + 1] += imos[id][i][j]; }
}
}
REP(i, k) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a;
--b;
REP(id, 3) {
int ans =
imos[id][c][d] + imos[id][a][b] - imos[id][a][d] - imos[id][b][c];
cout << ans << " \n"[id == 2];
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, x) for (int i = 0; i < x; ++i)
#define FOR(i, x, y) for (int i = x; i < y; ++i)
#define ALL(a) a.begin(), a.end()
using ll = long long;
int m, n, k;
inline int hash_(char c) { return ((c == 'J') ? 0 : ((c == 'O') ? 1 : 2)); }
int imos[3][1234][1234];
int main() {
cin >> m >> n;
cin >> k;
REP(i, m) {
char str[1234];
cin >> str;
REP(j, n) {
int id = hash_(str[j]);
imos[id][i + 1][j + 1] = 1;
}
}
REP(id, 3) {
REP(j, n + 1) {
REP(i, m + 1) { imos[id][i + 1][j] += imos[id][i][j]; }
}
}
REP(id, 3) {
REP(j, n + 1) {
REP(i, m + 1) { imos[id][i][j + 1] += imos[id][i][j]; }
}
}
REP(i, k) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a;
--b;
REP(id, 3) {
int ans =
imos[id][c][d] + imos[id][a][b] - imos[id][a][d] - imos[id][c][b];
cout << ans << " \n"[id == 2];
}
}
return 0;
}
|
[["-", 51, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["-", 49, 50, 51, 16, 12, 69, 341, 342, 0, 22], ["+", 49, 50, 51, 16, 12, 69, 341, 342, 0, 22]]
| 1
| 377
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#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 toString(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 EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);
typedef vector<vvi> vvvi;
char c[] = {'J', 'O', 'I'};
int main() {
int m, n;
cin >> m >> n;
int k;
cin >> k;
vs field(m);
REP(i, m) { cin >> field[i]; }
vvvi joi(3, vvi(m + 1, vi(n + 1)));
REP(i, 3) {
FOR(y, 1, m + 1) {
FOR(x, 1, n + 1) {
joi[i][y][x] = joi[i][y][x - 1] + (field[y - 1][x - 1] == c[i]);
}
}
}
REP(i, 3) {
FOR(y, 1, m + 1) {
FOR(x, 1, n + 1) { joi[i][y][x] += joi[i][y - 1][x]; }
}
}
REP(i, k) {
int a, b, c, d;
cin >> a >> b >> c >> d;
REP(i, 3) {
cout << joi[i][c][d] - joi[i][c][b - 1] - joi[i][a - 1][d] +
joi[i][a - 1][b - 1]
<< (i == k - 1 ? "" : " ");
}
cout << endl;
}
}
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#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 toString(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 EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);
typedef vector<vvi> vvvi;
char c[] = {'J', 'O', 'I'};
int main() {
int m, n;
cin >> m >> n;
int k;
cin >> k;
vs field(m);
REP(i, m) { cin >> field[i]; }
vvvi joi(3, vvi(m + 1, vi(n + 1)));
REP(i, 3) {
FOR(y, 1, m + 1) {
FOR(x, 1, n + 1) {
joi[i][y][x] = joi[i][y][x - 1] + (field[y - 1][x - 1] == c[i]);
}
}
}
REP(i, 3) {
FOR(y, 1, m + 1) {
FOR(x, 1, n + 1) { joi[i][y][x] += joi[i][y - 1][x]; }
}
}
REP(j, k) {
int a, b, c, d;
cin >> a >> b >> c >> d;
REP(i, 3) {
cout << joi[i][c][d] - joi[i][c][b - 1] - joi[i][a - 1][d] +
joi[i][a - 1][b - 1]
<< (i == 2 ? "" : " ");
}
cout << endl;
}
}
|
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["-", 12, 23, 0, 41, 15, 16, 12, 16, 31, 22], ["-", 12, 23, 0, 41, 15, 16, 12, 16, 17, 33], ["-", 12, 23, 0, 41, 15, 16, 12, 16, 12, 13], ["+", 0, 16, 12, 23, 0, 41, 15, 16, 12, 13]]
| 1
| 534
|
#include <cstdio>
int joi2num(char c) {
if (c == 'J')
return 0;
if (c == 'O')
return 1;
return 2;
}
int main() {
int m, n, k;
char fld[1024][1024];
int sum[1024][1024][3];
scanf("%d%d%d", &m, &n, &k);
for (int x = 0; x < m; ++x)
scanf("%s", fld[x]);
for (int i = 0; i < 3; ++i)
sum[0][0][i] = (i == joi2num(fld[0][0]));
for (int x = 1; x < m; ++x) {
for (int i = 0; i < 3; ++i)
sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
}
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i)
sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
}
for (int x = 1; x < m; ++x) {
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i) {
int temp = 0;
temp += sum[x - 1][y][i];
temp += sum[x][y - 1][i];
temp -= sum[x - 1][y - 1][i];
sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
}
}
}
for (int i = 0; i < k; ++i) {
int ax, ay, bx, by;
scanf("%d%d%d%d", &ax, &ay, &bx, &by);
ax -= 1, ay -= 1, bx -= 1, by -= 1;
int ans[3] = {0};
for (int j = 0; j < 3; ++j) {
ans[j] += sum[bx][by][j];
ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
}
printf("%d %d %d\n", ans[0], ans[1], ans[2]);
}
return 0;
}
|
#include <cstdio>
int joi2num(char c) {
if (c == 'J')
return 0;
if (c == 'O')
return 1;
return 2;
}
int main() {
int m, n, k;
static char fld[1024][1024];
static int sum[1024][1024][3] = {0};
scanf("%d%d%d", &m, &n, &k);
for (int x = 0; x < m; ++x)
scanf("%s", fld[x]);
for (int i = 0; i < 3; ++i)
sum[0][0][i] = (i == joi2num(fld[0][0]));
for (int x = 1; x < m; ++x) {
for (int i = 0; i < 3; ++i)
sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
}
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i)
sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
}
for (int x = 1; x < m; ++x) {
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i) {
int temp = 0;
temp += sum[x - 1][y][i];
temp += sum[x][y - 1][i];
temp -= sum[x - 1][y - 1][i];
sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
}
}
}
for (int i = 0; i < k; ++i) {
int ax, ay, bx, by;
scanf("%d%d%d%d", &ax, &ay, &bx, &by);
ax -= 1, ay -= 1, bx -= 1, by -= 1;
int ans[3] = {0};
for (int j = 0; j < 3; ++j) {
ans[j] += sum[bx][by][j];
ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
}
printf("%d %d %d\n", ans[0], ans[1], ans[2]);
}
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 43, 0, 114, 0, 115], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 45], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 46]]
| 1
| 623
|
#include <stdio.h>
int main() {
int m, n, i, j, k;
long int K;
int a, b, c, d;
char tmp;
int J[1002][1002] = {0}, O[1002][1002] = {0}; // I[1002][1002]={0};
int tmp1, tmp2, tmp3;
scanf("%d %d\n%ld\n", &m, &n, &K);
// for(i=0;i<m;i++){
// gets(map[i]);
// printf("%s\n",&map[i]);
//}
// printf("%c\n",map[0][3]);
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
scanf("%c", &tmp);
if (tmp == '\n')
scanf("%c", &tmp);
if (i == 1 && j == 1) {
} else if (i == 1) {
J[i][j] = J[i][j - 1];
O[i][j] = O[i][j - 1];
// I[i][j]=I[i][j-1];
} else if (j == 1) {
J[i][j] = J[i - 1][j];
O[i][j] = O[i - 1][1];
// I[i][j]=I[i-1][1];
} else {
J[i][j] = J[i - 1][j] + J[i][j - 1] - J[i - 1][j - 1];
O[i][j] = O[i - 1][j] + O[i][j - 1] - O[i - 1][j - 1];
// I[i][j]=I[i-1][j]+I[i][j-1]-I[i-1][j-1];
}
if (tmp == 'J') {
// printf("j\n");
J[i][j] += 1;
// printf("%d:%d J%d\n",i,j,J[i][j]);
} else if (tmp == 'O') {
O[i][j] += 1;
// printf("%d:%d O%d\n",i,j,O[i][j]);
} else if (tmp == 'I') {
// I[i][j]+=1;
// printf("%d:%d I%d\n",i,j,I[i][j]);
}
}
}
scanf("%c", &tmp);
for (i = 0; i < K; i++) {
scanf("%d %d %d %d\n", &a, &b, &c, &d);
tmp1 = J[c][d] - J[c][b - 1] - J[a - 1][d] + J[a - 1][b - 1];
printf("%d ", tmp1);
tmp2 = O[c][d] - O[c][b - 1] - O[a - 1][d] + O[a - 1][b - 1];
printf("%d ", tmp2);
tmp3 = (c - a + 1) * (b - d + 1) - tmp1 - tmp2;
printf("%d\n", tmp3);
}
return 0;
}
|
#include <stdio.h>
int main() {
int m, n, i, j, k;
long int K;
int a, b, c, d;
char tmp;
int J[1002][1002] = {0}, O[1002][1002] = {0}; // I[1002][1002]={0};
int tmp1, tmp2, tmp3;
scanf("%d %d\n%ld\n", &m, &n, &K);
// for(i=0;i<m;i++){
// gets(map[i]);
// printf("%s\n",&map[i]);
//}
// printf("%c\n",map[0][3]);
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
scanf("%c", &tmp);
if (tmp == '\n')
scanf("%c", &tmp);
if (i == 1 && j == 1) {
} else if (i == 1) {
J[i][j] = J[i][j - 1];
O[i][j] = O[i][j - 1];
// I[i][j]=I[i][j-1];
} else if (j == 1) {
J[i][j] = J[i - 1][j];
O[i][j] = O[i - 1][1];
// I[i][j]=I[i-1][1];
} else {
J[i][j] = J[i - 1][j] + J[i][j - 1] - J[i - 1][j - 1];
O[i][j] = O[i - 1][j] + O[i][j - 1] - O[i - 1][j - 1];
// I[i][j]=I[i-1][j]+I[i][j-1]-I[i-1][j-1];
}
if (tmp == 'J') {
// printf("j\n");
J[i][j] += 1;
// printf("%d:%d J%d\n",i,j,J[i][j]);
} else if (tmp == 'O') {
O[i][j] += 1;
// printf("%d:%d O%d\n",i,j,O[i][j]);
} else if (tmp == 'I') {
// I[i][j]+=1;
// printf("%d:%d I%d\n",i,j,I[i][j]);
}
}
}
scanf("%c", &tmp);
for (i = 0; i < K; i++) {
scanf("%d %d %d %d\n", &a, &b, &c, &d);
tmp1 = J[c][d] - J[c][b - 1] - J[a - 1][d] + J[a - 1][b - 1];
printf("%d ", tmp1);
tmp2 = O[c][d] - O[c][b - 1] - O[a - 1][d] + O[a - 1][b - 1];
printf("%d ", tmp2);
tmp3 = (c - a + 1) * (d - b + 1) - tmp1 - tmp2;
printf("%d\n", tmp3);
}
return 0;
}
|
[["-", 31, 16, 12, 23, 0, 16, 31, 16, 31, 22], ["-", 31, 16, 12, 23, 0, 16, 31, 16, 17, 33], ["+", 31, 16, 12, 23, 0, 16, 31, 16, 17, 33], ["+", 31, 16, 12, 23, 0, 16, 31, 16, 12, 22]]
| 1
| 577
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int M, N, K;
const int MAX_M = 1010;
const int MAX_N = 1010;
char board[MAX_M][MAX_N];
char c;
int a[1010][1010]; //長方形内のJの数
int b[1010][1010]; //長方形内のOの数
char s[1010];
int main() {
scanf("%d%d%d", &M, &N, &K);
for (int i = 1; i <= M; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= N; j++) {
if (s[j] == 'J') {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + 1;
b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
} else if (board[i][j] == 'O') {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + 1;
} else {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
}
}
}
int s, t, u, v;
for (int l = 0; l < K; l++) {
int ansJ, ansO, ansI;
scanf("%d%d%d%d", &s, &t, &u, &v);
s--;
t--;
ansJ = a[u][v] - a[s][v] - a[u][t] + a[s][t];
ansO = b[u][v] - b[s][v] - b[u][t] + b[s][t];
ansI = (u - s) * (v - t) - (ansJ + ansO);
printf("%d %d %d\n", ansJ, ansO, ansI);
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int M, N, K;
const int MAX_M = 1010;
const int MAX_N = 1010;
char board[MAX_M][MAX_N];
char c;
int a[1010][1010]; //長方形内のJの数
int b[1010][1010]; //長方形内のOの数
char s[1010];
int main() {
scanf("%d%d%d", &M, &N, &K);
for (int i = 1; i <= M; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= N; j++) {
if (s[j] == 'J') {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + 1;
b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
} else if (s[j] == 'O') {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + 1;
} else {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
}
}
}
int s, t, u, v;
for (int l = 0; l < K; l++) {
int ansJ, ansO, ansI;
scanf("%d%d%d%d", &s, &t, &u, &v);
s--;
t--;
ansJ = a[u][v] - a[s][v] - a[u][t] + a[s][t];
ansO = b[u][v] - b[s][v] - b[u][t] + b[s][t];
ansI = (u - s) * (v - t) - (ansJ + ansO);
printf("%d %d %d\n", ansJ, ansO, ansI);
}
return 0;
}
|
[["-", 15, 339, 51, 16, 31, 69, 28, 69, 28, 22], ["-", 51, 16, 31, 69, 28, 69, 341, 342, 0, 70], ["-", 51, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 51, 16, 31, 69, 28, 69, 341, 342, 0, 73], ["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22]]
| 1
| 719
|
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
using I = int;
using L = long;
using F = float;
using D = double;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;
const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;
template <class T> void PrintVector(vector<T> &v, unsigned n) {
rep(i, 0, n - 1) { cout << v[i] << " "; }
cout << endl;
}
int main(int argc, char const *argv[]) {
L n, a, b, c;
VL v;
cin >> n;
cin >> a >> b;
cin >> c;
rep(i, 0, n - 1) {
L ci;
cin >> ci;
v.pb(ci);
}
sort(v.begin(), v.end(), greater<>());
D initp = double(c) / a;
rep(i, 0, b - 1) {
c += v[i];
a += b;
if (double(c) / a > initp) {
initp = double(c) / a;
} else {
break;
}
}
cout << int(initp) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
using I = int;
using L = long;
using F = float;
using D = double;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;
const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;
template <class T> void PrintVector(vector<T> &v, unsigned n) {
rep(i, 0, n - 1) { cout << v[i] << " "; }
cout << endl;
}
int main(int argc, char const *argv[]) {
L n, a, b, c;
VL v;
cin >> n;
cin >> a >> b;
cin >> c;
rep(i, 0, n - 1) {
L ci;
cin >> ci;
v.pb(ci);
}
sort(v.begin(), v.end(), greater<>());
D initp = double(c) / a;
rep(i, 0, n - 1) {
c += v[i];
a += b;
if (double(c) / a > initp) {
initp = double(c) / a;
}
}
cout << int(initp) << endl;
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["-", 8, 9, 0, 9, 0, 57, 64, 9, 0, 46], ["-", 8, 9, 0, 9, 0, 57, 75, 76, 0, 95], ["-", 0, 9, 0, 57, 75, 76, 0, 9, 0, 45], ["-", 0, 57, 75, 76, 0, 9, 0, 93, 0, 94]]
| 1
| 486
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <vector>
using namespace std;
int n, a, b, c, p;
int main() {
scanf("%d", &n);
scanf("%d %d", &a, &b);
scanf("%d", &c);
std::vector<int> cal(n);
for (int j = 0; j < n; j++) {
scanf("%d", &cal[j]);
}
sort(cal.begin(), cal.end(), greater<int>());
int max = c / a; //値段当たりのカロリー
for (int k = 0; k < n; k++) {
int t = 0;
for (int l = 0; l < k; l++)
t = +cal[l];
p = (c + t) / (a + k * b);
if (p > max)
max = p;
}
int bp = int(max);
printf("%d", bp);
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <vector>
using namespace std;
int n, a, b, c, p;
int main() {
scanf("%d", &n);
scanf("%d %d", &a, &b);
scanf("%d", &c);
std::vector<int> cal(n);
for (int j = 0; j < n; j++) {
scanf("%d", &cal[j]);
}
sort(cal.begin(), cal.end(), greater<int>());
float max = c / a; //値段当たりのカロリー
for (int k = 0; k < n; k++) {
int t = 0;
for (int l = 0; l < k; l++)
t = t + cal[l];
p = (c + t) / (a + k * b);
if (p > max)
max = p;
}
int bp = int(max);
printf("%d\n", bp);
return 0;
}
|
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 7, 8, 1, 0, 11, 12, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
| 1
| 223
|
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int n;
double A, B, C, D[100];
cin >> n >> A >> B >> C;
for (int i = 0; i < n; i++) {
cin >> D[i];
}
sort(D, D + n, greater<int>());
double ret = 0;
double dp[100]; // memo topping
for (int i = 0; i < n - 1; i++) {
D[i + 1] = D[i] + D[i + 1];
dp[i + 1] = (C + D[i + 1]) / (A + (i + 2) * B);
dp[i] = (C + D[i]) / (A + (i + 1) * B);
ret = max(dp[i + 1], max(dp[i], C / A));
}
if (ret == 0)
cout << (int)(C / A) << endl;
else
cout << (int)ret << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int n;
double A, B, C, D[100];
cin >> n >> A >> B >> C;
for (int i = 0; i < n; i++) {
cin >> D[i];
}
sort(D, D + n, greater<int>());
double ret = 0;
double dp[100]; // memo topping
for (int i = 0; i < n - 1; i++) {
D[i + 1] = D[i] + D[i + 1];
dp[i + 1] = (C + D[i + 1]) / (A + (i + 2) * B);
dp[i] = (C + D[i]) / (A + (i + 1) * B);
ret = max(dp[i + 1], max(dp[i], max(ret, C / A)));
}
if (ret == 0)
cout << (int)(C / A) << endl;
else
cout << (int)ret << endl;
return 0;
}
|
[["+", 3, 4, 0, 2, 3, 4, 0, 2, 63, 22], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 24], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 22], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 21], ["+", 0, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
| 1
| 241
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
int rec(int, int, int);
using namespace std;
int n;
int k_n;
int k_prise, t_prise;
vector<int> topi;
vector<vector<int>> dp;
int main() {
cin >> n;
cin >> k_prise >> t_prise;
cin >> k_n;
dp.resize(n + 1);
int p = k_prise + t_prise * n;
topi.resize(n + 1);
for (int i = 1; i < n + 1; i++) {
int tmp;
dp[i - 1].resize(p + 1);
cin >> tmp;
topi[i] = tmp;
;
}
dp[n].resize(p + 1);
for (int i = k_prise; i <= p; i++) {
dp[0][i] = k_n;
}
for (int i = 1; i <= n; i++) {
for (int j = k_prise; j <= p; j++) {
dp[i][j] = dp[i - 1][j];
if (k_prise + t_prise < p)
dp[i][j] = max(dp[i][j], dp[i - 1][j - t_prise] + topi[i]);
}
}
// method
// int ans = rec(0,k_n,k_prise);
int ans = 0;
for (int i = k_prise; i <= p; i++)
ans = max(ans, dp[n][i] / i);
cout << ans << endl;
return 0;
}
int rec(int i, int cal, int prise) {
if (dp[i][prise] > -1) {
return dp[i][prise];
}
int res;
if (i == n) {
res = cal / prise;
dp[i][prise] = res;
} else {
int n1 = rec(i + 1, cal, prise);
int n2 = rec(i + 1, cal + topi[i], prise + t_prise);
res = max(n1, n2);
if (res == n2)
dp[i][prise + t_prise] = res;
else
dp[i][prise] = res;
}
return res;
}
|
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
int rec(int, int, int);
using namespace std;
int n;
int k_n;
int k_prise, t_prise;
vector<int> topi;
vector<vector<int>> dp;
int main() {
cin >> n;
cin >> k_prise >> t_prise;
cin >> k_n;
dp.resize(n + 1);
int p = k_prise + t_prise * n;
topi.resize(n + 1);
for (int i = 1; i < n + 1; i++) {
int tmp;
dp[i - 1].resize(p + 1);
cin >> tmp;
topi[i] = tmp;
;
}
dp[n].resize(p + 1);
for (int i = k_prise; i <= p; i++) {
dp[0][i] = k_n;
}
for (int i = 1; i <= n; i++) {
for (int j = k_prise; j <= p; j++) {
dp[i][j] = dp[i - 1][j];
if (k_prise + t_prise <= j)
dp[i][j] = max(dp[i][j], dp[i - 1][j - t_prise] + topi[i]);
}
}
// method
// int ans = rec(0,k_n,k_prise);
int ans = 0;
for (int i = k_prise; i <= p; i++)
ans = max(ans, dp[n][i] / i);
cout << ans << endl;
return 0;
}
int rec(int i, int cal, int prise) {
if (dp[i][prise] > -1) {
return dp[i][prise];
}
int res;
if (i == n) {
res = cal / prise;
dp[i][prise] = res;
} else {
int n1 = rec(i + 1, cal, prise);
int n2 = rec(i + 1, cal + topi[i], prise + t_prise);
res = max(n1, n2);
if (res == n2)
dp[i][prise + t_prise] = res;
else
dp[i][prise] = res;
}
return res;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
| 1
| 464
|
#include <stdio.h>
#include <string.h>
#define N 100
#define M 10000
#define PCHK(X) \
((((p >> 4) & 3) == ((p >> 2) & 3) && ((p >> 2) & 3) == (p & 3)))
static int d[N] = {0};
static int n;
static int k_operafan[N][64];
int dpFSA(int s, int p) {
int i;
if (s >= n)
return !(PCHK(p));
if (s >= 3 && PCHK(p))
return (0);
else if (~k_operafan[s][p])
;
else if (d[s] != 0) {
k_operafan[s][p] = dpFSA(s + 1, ((p << 2) | d[s]) & 63);
} else {
k_operafan[s][p] = 0;
for (i = 1; i <= 3; ++i) {
k_operafan[s][p] += dpFSA(s + 1, ((p << 2) | i) & 63);
k_operafan[s][p] %= M;
}
}
return (k_operafan[s][p]);
}
/** Application main entry point. */
int main(int argc, char *argv[]) {
int k;
scanf("%d%d", &n, &k);
while (k--) {
int a, b;
scanf("%d%d", &a, &b);
d[a] = b;
}
memset(k_operafan, -1, sizeof(k_operafan));
printf("%d\n", dpFSA(0, 0));
return (0);
}
|
#include <stdio.h>
#include <string.h>
#define N 100
#define M 10000
#define PCHK(X) \
((((p >> 4) & 3) == ((p >> 2) & 3) && ((p >> 2) & 3) == (p & 3)))
static int d[N] = {0};
static int n;
static int k_operafan[N][64];
int dpFSA(int s, int p) {
int i;
if (s >= n)
return !(PCHK(p));
else if (s >= 3 && PCHK(p))
return (0);
else if (~k_operafan[s][p])
;
else if (d[s] != 0) {
k_operafan[s][p] = dpFSA(s + 1, ((p << 2) | d[s]) & 63);
} else {
k_operafan[s][p] = 0;
for (i = 1; i <= 3; ++i) {
k_operafan[s][p] += dpFSA(s + 1, ((p << 2) | i) & 63);
k_operafan[s][p] %= M;
}
}
return (k_operafan[s][p]);
}
/** Application main entry point. */
int main(int argc, char *argv[]) {
int k;
scanf("%d%d", &n, &k);
while (k--) {
int a, b;
scanf("%d%d", &a, &b);
d[a - 1] = b;
}
memset(k_operafan, -1, sizeof(k_operafan));
printf("%d\n", dpFSA(0, 0));
return (0);
}
|
[["+", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 0, 1, 0, 11, 31, 69, 71, 16, 17, 33], ["+", 0, 1, 0, 11, 31, 69, 71, 16, 12, 13]]
| 0
| 313
|
#include <stdio.h>
int pasta[100], dp[101][4][4];
int main() {
int n, i, j, k, l;
scanf("%d%d", &n, &k);
for (i = 0; i < k; i++) {
int ai, bi;
scanf("%d%d", &ai, &bi);
pasta[ai] = bi;
}
dp[0][0][0] = 1;
for (i = 1; i <= n; i++) {
if (pasta[i] == 0) {
for (j = 1; j <= 3; j++) {
for (k = 0; k <= 3; k++) {
for (l = 0; l <= 3; l++) {
if (j == k && l == k)
continue;
dp[i][j][k] += dp[i - 1][k][l];
dp[i][j][k] %= 10000;
}
}
}
} else {
for (k = 1; k <= 3; k++) {
for (l = 1; l <= 3; l++) {
if (pasta[i] == k && l == k)
continue;
dp[i][pasta[i]][k] += dp[i - 1][k][l];
dp[i][pasta[i]][k] %= 10000;
}
}
}
}
int ans = 0;
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 3; j++) {
ans += dp[n][i][j];
ans %= 10000;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <stdio.h>
int pasta[100], dp[101][4][4];
int main() {
int n, i, j, k, l;
scanf("%d%d", &n, &k);
for (i = 0; i < k; i++) {
int ai, bi;
scanf("%d%d", &ai, &bi);
pasta[ai] = bi;
}
dp[0][0][0] = 1;
for (i = 1; i <= n; i++) {
if (pasta[i] == 0) {
for (j = 1; j <= 3; j++) {
for (k = 0; k <= 3; k++) {
for (l = 0; l <= 3; l++) {
if (j == k && l == k)
continue;
dp[i][j][k] += dp[i - 1][k][l];
dp[i][j][k] %= 10000;
}
}
}
} else {
for (k = 0; k <= 3; k++) {
for (l = 0; l <= 3; l++) {
if (pasta[i] == k && l == k)
continue;
dp[i][pasta[i]][k] += dp[i - 1][k][l];
dp[i][pasta[i]][k] %= 10000;
}
}
}
}
int ans = 0;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
ans += dp[n][i][j];
ans %= 10000;
}
}
printf("%d\n", ans);
return 0;
}
|
[["-", 75, 76, 0, 9, 0, 7, 10, 11, 12, 13], ["+", 75, 76, 0, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13]]
| 0
| 378
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second
using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct { int to, cost; };
const int MOD = 1e4;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<int> meal;
int dp[110][4][2];
int N, K;
int dfs(int i, int j, bool k) {
if (dp[i][j][k] != -1)
return dp[i][j][k];
if (i == N + 1)
return 1;
int res = 0;
if (meal[i] != 0) {
if (j && k == meal[i])
return 0;
res = dfs(i + 1, meal[i], meal[i] == j);
return dp[i][j][k] = res % MOD;
}
FOR(n, 1, 4) {
if (k && j == n)
return 0;
res += dfs(i + 1, n, n == j);
}
return dp[i][j][k] = res % MOD;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> K;
meal.resize(N + 1, 0);
REP(i, K) {
int a, b;
cin >> a >> b;
meal[a] = b;
}
memset(dp, -1, sizeof(dp));
Cout(dfs(1, 0, 0));
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second
using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct { int to, cost; };
const int MOD = 1e4;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<int> meal;
int dp[110][4][2];
int N, K;
int dfs(int i, int j, bool k) {
if (dp[i][j][k] != -1)
return dp[i][j][k];
if (i == N + 1)
return 1;
int res = 0;
if (meal[i] != 0) {
if (k && j == meal[i])
return 0;
res = dfs(i + 1, meal[i], meal[i] == j);
return dp[i][j][k] = res % MOD;
}
FOR(n, 1, 4) {
if (k && j == n)
continue;
res += dfs(i + 1, n, n == j);
}
return dp[i][j][k] = res % MOD;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> K;
meal.resize(N + 1, 0);
REP(i, K) {
int a, b;
cin >> a >> b;
meal[a] = b;
}
memset(dp, -1, sizeof(dp));
Cout(dfs(1, 0, 0));
return 0;
}
|
[["-", 64, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["-", 64, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["-", 8, 9, 0, 9, 0, 57, 64, 37, 0, 38], ["-", 8, 9, 0, 9, 0, 57, 64, 37, 0, 13], ["+", 8, 9, 0, 9, 0, 57, 64, 116, 0, 117]]
| 1
| 462
|
#include <cstdio>
using namespace std;
int d[100], n, k, dp[100][6];
// 0:aa, 1:bb, 2:cc, 3:a, 4:b, 5:c
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < n; i++)
d[i] = 0;
for (int i = 0; i < k; i++)
scanf("%d%d", &a, &b), d[a - 1] = b;
dp[0][0] = dp[0][1] = dp[0][2] = dp[0][3] = dp[0][4] = dp[0][5] = 0;
if (d[0] == 0)
dp[0][3] = dp[0][4] = dp[0][5] = 1;
if (d[0] == 1)
dp[0][3] = 1;
if (d[0] == 2)
dp[0][4] = 1;
if (d[0] == 3)
dp[0][5] = 1;
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][3];
dp[i][1] = dp[i - 1][4];
dp[i][2] = dp[i - 1][5];
dp[i][3] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5];
dp[i][4] = dp[i - 1][0] + dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][5];
dp[i][5] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][4];
for (int j = 0; j < 6; j++)
dp[i][j] = dp[i][j] % 10000;
if (d[i] == 0)
continue;
if (d[i] == 1)
dp[i][1] = dp[i][2] = dp[i][4] = dp[i][5] = 0;
if (d[i] == 2)
dp[i][0] = dp[i][2] = dp[i][3] = dp[i][5] = 0;
if (d[i] == 3)
dp[i][0] = dp[i][1] = dp[i][3] = dp[i][4] = 0;
}
int ans = 0;
for (int j = 0; j < 6; j++)
ans += dp[n - 1][j];
printf("%d\n", ans % 10000);
}
|
#include <cstdio>
using namespace std;
int d[100], n, k, dp[100][6];
// 0:aa, 1:bb, 2:cc, 3:a, 4:b, 5:c
int main() {
int a, b;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
d[i] = 0;
for (int i = 0; i < k; i++)
scanf("%d%d", &a, &b), d[a - 1] = b;
dp[0][0] = dp[0][1] = dp[0][2] = dp[0][3] = dp[0][4] = dp[0][5] = 0;
if (d[0] == 0)
dp[0][3] = dp[0][4] = dp[0][5] = 1;
if (d[0] == 1)
dp[0][3] = 1;
if (d[0] == 2)
dp[0][4] = 1;
if (d[0] == 3)
dp[0][5] = 1;
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][3];
dp[i][1] = dp[i - 1][4];
dp[i][2] = dp[i - 1][5];
dp[i][3] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5];
dp[i][4] = dp[i - 1][0] + dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][5];
dp[i][5] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][4];
for (int j = 0; j < 6; j++)
dp[i][j] = dp[i][j] % 10000;
if (d[i] == 0)
continue;
if (d[i] == 1)
dp[i][1] = dp[i][2] = dp[i][4] = dp[i][5] = 0;
if (d[i] == 2)
dp[i][0] = dp[i][2] = dp[i][3] = dp[i][5] = 0;
if (d[i] == 3)
dp[i][0] = dp[i][1] = dp[i][3] = dp[i][4] = 0;
}
int ans = 0;
for (int j = 0; j < 6; j++)
ans += dp[n - 1][j];
printf("%d\n", ans % 10000);
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 66, 28, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 66, 28, 22]]
| 1
| 677
|
#include <bits/stdc++.h>
using namespace std;
int dp[100][4][4];
int p[100], n, k;
int solve(int day, int bday, int bbday) {
int &tmp = dp[day][bday][bbday];
if (day == n)
return 1;
if (tmp != -1)
return tmp;
if (p[day] != 0) {
return (bday == bbday && bday == p[day]
? 0
: solve(day + 1, p[day], bday) % 10000);
}
int ret = 0;
for (int i = 1; i <= 3; i++) {
if (bbday == bday && day == i)
continue;
ret += solve(day + 1, i, bday) % 10000;
}
return tmp = ret % 10000;
}
int main() {
cin >> n >> k;
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
p[a - 1] = b;
}
memset(dp, -1, sizeof(dp));
int ans = solve(0, 0, 0);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int dp[100][4][4];
int p[100], n, k;
int solve(int day, int bday, int bbday) {
int &tmp = dp[day][bday][bbday];
if (day == n)
return 1;
if (tmp != -1)
return tmp;
if (p[day] != 0) {
return (bday == bbday && bday == p[day]
? 0
: solve(day + 1, p[day], bday) % 10000);
}
int ret = 0;
for (int i = 1; i <= 3; i++) {
if (bbday == bday && bday == i)
continue;
ret += solve(day + 1, i, bday) % 10000;
}
return tmp = ret % 10000;
}
int main() {
cin >> n >> k;
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
p[a - 1] = b;
}
memset(dp, -1, sizeof(dp));
int ans = solve(0, 0, 0);
cout << ans << endl;
}
|
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22]]
| 1
| 251
|
#include <iostream>
using namespace std;
int main() {
int n, k, a, b, ans = 0, pasta[101] = {}, dp[101][4][4] = {};
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> a >> b;
pasta[a] = b;
}
for (int i = 1; i <= 3; i++) {
if (pasta[1] > 0 && i != pasta[i]) {
continue;
}
dp[1][1][i] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= 3; j++) {
for (int l = 1; l <= 3; l++) {
if (pasta[i] > 0 && l != pasta[i]) {
continue;
}
dp[i][j][l] = dp[i - 1][1][j] + dp[i - 1][2][j] + dp[i - 1][3][j];
if (i > 2 && j == l && dp[i][j][l] > 0 && dp[i - 1][j][l] > 0) {
dp[i][j][l] -= dp[i - 1][j][l];
}
dp[i][j][l] %= 10000;
}
}
}
for (int j = 1; j <= 3; j++) {
for (int l = 1; l <= 3; l++) {
ans += dp[n][j][l];
}
}
cout << ans % 10000 << endl;
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int n, k, a, b, ans = 0, pasta[101] = {}, dp[101][4][4] = {};
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> a >> b;
pasta[a] = b;
}
for (int i = 1; i <= 3; i++) {
if (pasta[1] > 0 && i != pasta[1]) {
continue;
}
dp[1][1][i] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= 3; j++) {
for (int l = 1; l <= 3; l++) {
if (pasta[i] > 0 && l != pasta[i]) {
continue;
}
dp[i][j][l] = dp[i - 1][1][j] + dp[i - 1][2][j] + dp[i - 1][3][j];
if (i > 2 && j == l && dp[i][j][l] > 0 && dp[i - 1][j][l] > 0) {
dp[i][j][l] -= dp[i - 1][j][l];
}
dp[i][j][l] %= 10000;
}
}
}
for (int j = 1; j <= 3; j++) {
for (int l = 1; l <= 3; l++) {
ans += dp[n][j][l];
}
}
cout << ans % 10000 << endl;
return 0;
}
|
[["-", 51, 16, 12, 16, 12, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 16, 12, 69, 341, 342, 0, 13]]
| 1
| 382
|
#include <iostream>
#include <vector>
using namespace std;
#define MOD 10000
int main() {
int N, K, A, B;
cin >> N >> K;
vector<int> P(N, 0);
for (int i = 0; i < K; i++) {
cin >> A >> B;
P[A] = B;
}
vector<vector<vector<int>>> dp(N, vector<vector<int>>(3, vector<int>(2, 0)));
if (P[0] != 0) {
dp[0][P[0] - 1][0] = 1;
} else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
for (int i = 1; i < N; ++i) {
if (P[i] == 0) {
for (int j = 0; j < 3; ++j) {
int j1, j2;
if (j == 0) {
j1 = 1;
j2 = 2;
}
if (j == 1) {
j1 = 0;
j2 = 2;
}
if (j == 2) {
j1 = 0;
j2 = 1;
}
dp[i][j][0] = (dp[i - 1][j1][0] + dp[i - 1][j1][1] + dp[i - 1][j2][0] +
dp[i - 1][j2][1]) %
MOD;
dp[i][j][1] = dp[i - 1][j][0];
}
} else {
int j = P[i] - 1;
int j1, j2;
if (j == 0) {
j1 = 1;
j2 = 2;
}
if (j == 1) {
j1 = 0;
j2 = 2;
}
if (j == 2) {
j1 = 0;
j2 = 1;
}
dp[i][j][0] = (dp[i - 1][j1][0] + dp[i - 1][j1][1] + dp[i - 1][j2][0] +
dp[i - 1][j2][1]) %
MOD;
dp[i][j][1] = dp[i - 1][j][0];
}
}
int Ans = (dp[N - 1][0][0] + dp[N - 1][0][1] + dp[N - 1][1][0] +
dp[N - 1][1][1] + dp[N - 1][2][0] + dp[N - 1][2][1]) %
MOD;
cout << Ans << endl;
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
#define MOD 10000
int main() {
int N, K, A, B;
cin >> N >> K;
vector<int> P(N, 0);
for (int i = 0; i < K; i++) {
cin >> A >> B;
P[A - 1] = B;
}
vector<vector<vector<int>>> dp(N, vector<vector<int>>(3, vector<int>(2, 0)));
if (P[0] != 0) {
dp[0][P[0] - 1][0] = 1;
} else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
for (int i = 1; i < N; ++i) {
if (P[i] == 0) {
for (int j = 0; j < 3; ++j) {
int j1, j2;
if (j == 0) {
j1 = 1;
j2 = 2;
}
if (j == 1) {
j1 = 0;
j2 = 2;
}
if (j == 2) {
j1 = 0;
j2 = 1;
}
dp[i][j][0] = (dp[i - 1][j1][0] + dp[i - 1][j1][1] + dp[i - 1][j2][0] +
dp[i - 1][j2][1]) %
MOD;
dp[i][j][1] = dp[i - 1][j][0];
}
} else {
int j = P[i] - 1;
int j1, j2;
if (j == 0) {
j1 = 1;
j2 = 2;
}
if (j == 1) {
j1 = 0;
j2 = 2;
}
if (j == 2) {
j1 = 0;
j2 = 1;
}
dp[i][j][0] = (dp[i - 1][j1][0] + dp[i - 1][j1][1] + dp[i - 1][j2][0] +
dp[i - 1][j2][1]) %
MOD;
dp[i][j][1] = dp[i - 1][j][0];
}
}
int Ans = (dp[N - 1][0][0] + dp[N - 1][0][1] + dp[N - 1][1][0] +
dp[N - 1][1][1] + dp[N - 1][2][0] + dp[N - 1][2][1]) %
MOD;
cout << Ans << endl;
return 0;
}
|
[["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 617
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai \
ios::sync_with_stdio(false); \
cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
os << "(" << p.fst << ", " << p.scd << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
rep(i, v.size()) {
if (i)
os << ", ";
os << v[i];
}
return os;
}
template <typename T> T &max(T &a, T &b) {
if (a >= b)
return a;
return b;
}
template <typename T> T &min(T &a, T &b) {
if (a < b)
return a;
return b;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;
int N, K;
int dp[105][3]
[3]; // dp[?????\??????][?????\???????????\???][?????\???????????\???]
int menu[105];
int main() {
cin >> N >> K;
memset(menu, -1, sizeof menu);
rep(i, K) {
int a, b;
cin >> a >> b;
menu[a] = b - 1;
}
if (menu[0] != -1) {
dp[1][menu[0]][0] = 1;
} else {
rep(i, 3) { dp[1][i][0] = 1; }
}
if (menu[1] != -1) {
rep(i, 3) { dp[2][menu[1]][i] += dp[1][i][0]; }
} else {
rep(i, 3) rep(j, 3) rep(k, 3) {
dp[2][i][j] += dp[1][j][k];
dp[2][i][j] %= 10000;
}
}
for (int i = 2; i < N; ++i) {
if (menu[i] != -1) {
rep(j, 3) {
rep(k, 3) {
if (menu[i] == j && j == k)
continue;
dp[i + 1][menu[i]][j] += dp[i][j][k];
dp[i + 1][menu[i]][j] %= 10000;
}
}
} else {
rep(j, 3) {
rep(k, 3) {
rep(l, 3) {
if (j == k && k == l)
continue;
dp[i + 1][j][k] += dp[i][k][l];
dp[i + 1][j][k] %= 10000;
}
}
}
}
}
int ans = 0;
rep(i, 3) rep(j, 3) {
ans += dp[N][i][j];
ans %= 10000;
}
cout << ans << endl;
}
|
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai \
ios::sync_with_stdio(false); \
cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
os << "(" << p.fst << ", " << p.scd << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
rep(i, v.size()) {
if (i)
os << ", ";
os << v[i];
}
return os;
}
template <typename T> T &max(T &a, T &b) {
if (a >= b)
return a;
return b;
}
template <typename T> T &min(T &a, T &b) {
if (a < b)
return a;
return b;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;
int N, K;
int dp[105][3]
[3]; // dp[?????\??????][?????\???????????\???][?????\???????????\???]
int menu[105];
int main() {
cin >> N >> K;
memset(menu, -1, sizeof menu);
rep(i, K) {
int a, b;
cin >> a >> b;
menu[a - 1] = b - 1;
}
if (menu[0] != -1) {
dp[1][menu[0]][0] = 1;
} else {
rep(i, 3) { dp[1][i][0] = 1; }
}
if (menu[1] != -1) {
rep(i, 3) { dp[2][menu[1]][i] += dp[1][i][0]; }
} else {
rep(i, 3) rep(j, 3) rep(k, 3) {
dp[2][i][j] += dp[1][j][k];
dp[2][i][j] %= 10000;
}
}
for (int i = 2; i < N; ++i) {
if (menu[i] != -1) {
rep(j, 3) {
rep(k, 3) {
if (menu[i] == j && j == k)
continue;
dp[i + 1][menu[i]][j] += dp[i][j][k];
dp[i + 1][menu[i]][j] %= 10000;
}
}
} else {
rep(j, 3) {
rep(k, 3) {
rep(l, 3) {
if (j == k && k == l)
continue;
dp[i + 1][j][k] += dp[i][k][l];
dp[i + 1][j][k] %= 10000;
}
}
}
}
}
int ans = 0;
rep(i, 3) rep(j, 3) {
ans += dp[N][i][j];
ans %= 10000;
}
cout << ans << endl;
}
|
[["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 839
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#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 pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
int n, m;
int MOD = 10000;
cin >> n >> m;
int dp[110][3][3] = {0};
vi in(n);
while (m--) {
int a, b;
cin >> a >> b;
a--;
in[a] = b;
}
rep(i, 3) rep(j, 3) {
dp[1][(in[1] ? in[0] - 1 : i)][(in[0] ? in[1] - 1 : j)] = 1;
}
loop(i, 2, n) {
if (in[i]) {
rep(j, 3) rep(k, 3) if (!(in[i] - 1 == j && j == k)) dp[i][in[i] - 1][j] =
(dp[i][in[i] - 1][j] + dp[i - 1][j][k]) % MOD;
} else {
rep(q, 3) rep(j, 3) rep(k, 3) if (!(q == j && j == k)) dp[i][q][j] =
(dp[i][q][j] + dp[i - 1][j][k]) % MOD;
}
}
int co = 0;
rep(i, 3) rep(j, 3) co = (co + dp[n - 1][i][j]) % MOD;
cout << co << endl;
// rep(i,3){rep(j,3)cout<<" "<<dp[2][i][j];cout<<endl;}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#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 pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
int n, m;
int MOD = 10000;
cin >> n >> m;
int dp[110][3][3] = {0};
vi in(n);
while (m--) {
int a, b;
cin >> a >> b;
a--;
in[a] = b;
}
rep(i, 3) rep(j, 3) {
dp[1][(in[1] ? in[1] - 1 : i)][(in[0] ? in[0] - 1 : j)] = 1;
}
loop(i, 2, n) {
if (in[i]) {
rep(j, 3) rep(k, 3) if (!(in[i] - 1 == j && j == k)) dp[i][in[i] - 1][j] =
(dp[i][in[i] - 1][j] + dp[i - 1][j][k]) % MOD;
} else {
rep(q, 3) rep(j, 3) rep(k, 3) if (!(q == j && j == k)) dp[i][q][j] =
(dp[i][q][j] + dp[i - 1][j][k]) % MOD;
}
}
int co = 0;
rep(i, 3) rep(j, 3) co = (co + dp[n - 1][i][j]) % MOD;
cout << co << endl;
// rep(i,3){rep(j,3)cout<<" "<<dp[1][i][j];cout<<endl;}
}
|
[["-", 0, 41, 64, 16, 31, 69, 341, 342, 0, 13], ["+", 0, 41, 64, 16, 31, 69, 341, 342, 0, 13]]
| 1
| 495
|
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 9000000000000000000
#define INF 2000000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
class Union_find {
private:
vector<int> par;
vector<int> rank;
int n;
public:
Union_find(int a) {
n = a;
for (int i = 0; i < n; i++) {
par.push_back(i);
rank.push_back(0);
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
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]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
int main(void) {
int N, K;
map<int, int> d;
int dp[101][3][3] = {0};
cin >> N >> K;
REP(i, K) {
int a, b;
cin >> a >> b;
d[a] = b;
}
REP(i, 3) {
REP(j, 3) { dp[2][i][j] = 1; }
}
if (d.find(0) != d.end()) {
REP(i, 3) {
if (i != d[0] - 1) {
REP(j, 3) { dp[2][j][i] = 0; }
}
}
}
if (d.find(1) != d.end()) {
REP(i, 3) {
if (i != d[1] - 1) {
REP(j, 3) { dp[2][i][j] = 0; }
}
}
}
if (d.find(2) != d.end()) {
dp[2][d[2] - 1][d[2] - 1] = 0;
}
for (int i = 2; i < N; i++) {
REP(j, 3) { // 2??????
REP(k, 3) { // 1??????
if (d.find(i) != d.end()) {
if (!(j == k && k == d[i] - 1)) {
dp[i + 1][d[i] - 1][k] += dp[i][k][j];
dp[i + 1][d[i] - 1][k] %= 10000;
}
} else {
REP(s, 3) {
if (!(j == k && k == s)) {
dp[i + 1][s][k] += dp[i][k][j];
dp[i + 1][s][k] %= 10000;
}
}
}
}
}
}
int res = 0;
REP(i, 3) {
REP(j, 3) { res = (res + dp[N][i][j]) % 10000; }
}
cout << res << endl;
}
|
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 9000000000000000000
#define INF 2000000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
class Union_find {
private:
vector<int> par;
vector<int> rank;
int n;
public:
Union_find(int a) {
n = a;
for (int i = 0; i < n; i++) {
par.push_back(i);
rank.push_back(0);
}
}
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
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]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
};
int main(void) {
int N, K;
map<int, int> d;
int dp[101][3][3] = {0};
cin >> N >> K;
REP(i, K) {
int a, b;
cin >> a >> b;
d[a - 1] = b;
}
REP(i, 3) {
REP(j, 3) { dp[2][i][j] = 1; }
}
if (d.find(0) != d.end()) {
REP(i, 3) {
if (i != d[0] - 1) {
REP(j, 3) { dp[2][j][i] = 0; }
}
}
}
if (d.find(1) != d.end()) {
REP(i, 3) {
if (i != d[1] - 1) {
REP(j, 3) { dp[2][i][j] = 0; }
}
}
}
if (d.find(2) != d.end()) {
dp[2][d[2] - 1][d[2] - 1] = 0;
}
for (int i = 2; i < N; i++) {
REP(j, 3) { // 2??????
REP(k, 3) { // 1??????
if (d.find(i) != d.end()) {
if (!(j == k && k == d[i] - 1)) {
dp[i + 1][d[i] - 1][k] += dp[i][k][j];
dp[i + 1][d[i] - 1][k] %= 10000;
}
} else {
REP(s, 3) {
if (!(j == k && k == s)) {
dp[i + 1][s][k] += dp[i][k][j];
dp[i + 1][s][k] %= 10000;
}
}
}
}
}
}
int res = 0;
REP(i, 3) {
REP(j, 3) { res = (res + dp[N][i][j]) % 10000; }
}
cout << res << endl;
}
|
[["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 760
|
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
int a[101], dp[101][3][3];
int main() {
int n, k, t1, t2;
scanf("%d%d", &n, &k);
rep(i, k) {
scanf("%d%d", &t1, &t2);
a[t1] = t2;
}
if (a[1] == 0 || a[1] == 1)
dp[1][0][1] = 1;
if (a[1] == 0 || a[1] == 2)
dp[1][1][2] = 1;
if (a[1] == 0 || a[1] == 3)
dp[1][2][0] = 1;
for (int i = 1; i <= n; i++) {
if (a[i] == 0 || a[i] == 1) {
dp[i][0][0] = dp[i - 1][0][1] + dp[i - 1][0][2];
rep(j, 3) dp[i][0][1] += dp[i - 1][1][j];
rep(j, 3) dp[i][0][2] += dp[i - 1][2][j];
}
if (a[i] == 0 || a[i] == 2) {
dp[i][1][1] = dp[i - 1][1][0] + dp[i - 1][1][2];
rep(j, 3) dp[i][1][0] += dp[i - 1][0][j];
rep(j, 3) dp[i][1][2] += dp[i - 1][2][j];
}
if (a[i] == 0 || a[i] == 3) {
dp[i][2][2] = dp[i - 1][2][0] + dp[i - 1][2][1];
rep(j, 3) dp[i][2][0] += dp[i - 1][0][j];
rep(j, 3) dp[i][2][1] += dp[i - 1][1][j];
}
rep(j, 3) rep(k, 3) dp[i][j][k] = dp[i][j][k] % 10000;
}
int ans = 0;
rep(i, 3) rep(j, 3) ans += dp[n][i][j];
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
int a[101], dp[101][3][3];
int main() {
int n, k, t1, t2;
scanf("%d%d", &n, &k);
rep(i, k) {
scanf("%d%d", &t1, &t2);
a[t1] = t2;
}
if (a[1] == 0 || a[1] == 1)
dp[1][0][1] = 1;
if (a[1] == 0 || a[1] == 2)
dp[1][1][2] = 1;
if (a[1] == 0 || a[1] == 3)
dp[1][2][0] = 1;
for (int i = 1; i <= n; i++) {
if (a[i] == 0 || a[i] == 1) {
dp[i][0][0] = dp[i - 1][0][1] + dp[i - 1][0][2];
rep(j, 3) dp[i][0][1] += dp[i - 1][1][j];
rep(j, 3) dp[i][0][2] += dp[i - 1][2][j];
}
if (a[i] == 0 || a[i] == 2) {
dp[i][1][1] = dp[i - 1][1][0] + dp[i - 1][1][2];
rep(j, 3) dp[i][1][0] += dp[i - 1][0][j];
rep(j, 3) dp[i][1][2] += dp[i - 1][2][j];
}
if (a[i] == 0 || a[i] == 3) {
dp[i][2][2] = dp[i - 1][2][0] + dp[i - 1][2][1];
rep(j, 3) dp[i][2][0] += dp[i - 1][0][j];
rep(j, 3) dp[i][2][1] += dp[i - 1][1][j];
}
rep(j, 3) rep(k, 3) dp[i][j][k] = dp[i][j][k] % 10000;
}
int ans = 0;
rep(i, 3) rep(j, 3) ans += dp[n][i][j];
printf("%d\n", ans % 10000);
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 109], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 654
|
#include <bits/stdc++.h>
#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<LL>
#define VVL vector<vector<LL>>
#define VVVL vector<vector<vector<LL>>>
#define VB vector<bool>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14
template <class T> T ceil(T a, T b) { return (a + b - 1) / b; }
template <class T> void print(T x) { std::cout << x << std::endl; }
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, -1, 0, 1},
dx = {1, 0, -1, 0}; // 4近傍(右, 下, 左, 上)
using namespace std;
const int MOD = 10000;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
map<int, int> m;
FOR(i, 0, K) {
int a, b;
cin >> a >> b;
m[a] = b;
}
// dp[i][j][k] = i日目に2日前がパスタjで1日前がパスタkのときの場合の数
VVVL dp(N + 1, VVL(4, VL(4, 0)));
if (m[1]) {
dp[1][0][m[1]] = 1;
} else {
dp[1][0][1] = 1;
dp[1][0][2] = 1;
dp[1][0][3] = 1;
}
FOR(i, 1, N) {
FOR(j, 1, 4) {
FOR(k, 1, 4) {
// i + 1日目のパスタが指定されてる
if (m[i + 1]) {
if (j == k and k == m[i + 1]) {
} else {
(dp[i + 1][k][m[i + 1]] += dp[i][j][k]) %= MOD;
}
continue;
}
// 2日連続で同じパスタ
if (j == k) {
if (j == 1) {
(dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
} else if (j == 2) {
(dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
} else if (j == 3) {
(dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
}
} else {
(dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
}
}
}
}
LL ans = 0;
FOR(j, 1, 4) {
FOR(k, 1, 4) { (ans += dp[N][j][k]) %= MOD; }
}
print(ans % MOD);
}
|
#include <bits/stdc++.h>
#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<LL>
#define VVL vector<vector<LL>>
#define VVVL vector<vector<vector<LL>>>
#define VB vector<bool>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14
template <class T> T ceil(T a, T b) { return (a + b - 1) / b; }
template <class T> void print(T x) { std::cout << x << std::endl; }
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, -1, 0, 1},
dx = {1, 0, -1, 0}; // 4近傍(右, 下, 左, 上)
using namespace std;
const int MOD = 10000;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
map<int, int> m;
FOR(i, 0, K) {
int a, b;
cin >> a >> b;
m[a] = b;
}
// dp[i][j][k] = i日目に2日前がパスタjで1日前がパスタkのときの場合の数
VVVL dp(N + 1, VVL(4, VL(4, 0)));
if (m[1]) {
dp[1][0][m[1]] = 1;
} else {
dp[1][0][1] = 1;
dp[1][0][2] = 1;
dp[1][0][3] = 1;
}
FOR(i, 1, N) {
FOR(j, 0, 4) {
FOR(k, 0, 4) {
// i + 1日目のパスタが指定されてる
if (m[i + 1]) {
if (j == k and k == m[i + 1]) {
} else {
(dp[i + 1][k][m[i + 1]] += dp[i][j][k]) %= MOD;
}
continue;
}
// 2日連続で同じパスタ
if (j == k) {
if (j == 1) {
(dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
} else if (j == 2) {
(dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
} else if (j == 3) {
(dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
}
} else {
(dp[i + 1][k][1] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][2] += dp[i][j][k]) %= MOD;
(dp[i + 1][k][3] += dp[i][j][k]) %= MOD;
}
}
}
}
LL ans = 0;
FOR(j, 0, 4) {
FOR(k, 0, 4) { (ans += dp[N][j][k]) %= MOD; }
}
print(ans % MOD);
}
|
[["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 0, 11, 31, 2, 63, 2, 3, 4, 0, 13], ["+", 0, 11, 31, 2, 63, 2, 3, 4, 0, 13]]
| 1
| 827
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
int W, H, N;
int xs[2][100 * 1000];
int ys[2][100 * 1000];
int main() {
scanf("%d%d%d", &W, &H, &N);
for (int i = 0; i < N; i++) {
scanf("%d%d", xs[0] + i, ys[0] + i);
xs[1][i] = xs[0][i];
ys[1][i] = ys[0][i];
}
LL ret = 1LL << 60;
int rx = 0, ry = 0;
for (int i = 0; i <= (N + 1) % 2; i++) {
for (int j = 0; j <= (N + 1) % 2; j++) {
nth_element(xs[1], xs[1] + (N + i - 1) / 2, xs[1] + N);
int x = xs[1][(N - i) / 2];
nth_element(ys[1], ys[1] + (N + j - 1) / 2, ys[1] + N);
int y = ys[1][(N - j) / 2];
LL m = 0;
LL sum = 0;
for (int i = 0; i < N; i++) {
LL d = abs(xs[0][i] - x);
d += abs(ys[0][i] - y);
m = max(m, d);
sum += 2 * d;
}
sum -= m;
if (sum < ret) {
ret = sum;
rx = x;
ry = y;
}
}
}
printf("%lld\n%d %d\n", ret, rx, ry);
}
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
int W, H, N;
int xs[2][100 * 1000];
int ys[2][100 * 1000];
int main() {
scanf("%d%d%d", &W, &H, &N);
for (int i = 0; i < N; i++) {
scanf("%d%d", xs[0] + i, ys[0] + i);
xs[1][i] = xs[0][i];
ys[1][i] = ys[0][i];
}
LL ret = 1LL << 60;
int rx = 0, ry = 0;
for (int i = 0; i <= (N + 1) % 2; i++) {
for (int j = 0; j <= (N + 1) % 2; j++) {
nth_element(xs[1], xs[1] + (N + i - 1) / 2, xs[1] + N);
int x = xs[1][(N + i - 1) / 2];
nth_element(ys[1], ys[1] + (N + j - 1) / 2, ys[1] + N);
int y = ys[1][(N + j - 1) / 2];
LL m = 0;
LL sum = 0;
for (int i = 0; i < N; i++) {
LL d = abs(xs[0][i] - x);
d += abs(ys[0][i] - y);
m = max(m, d);
sum += 2 * d;
}
sum -= m;
if (sum < ret) {
ret = sum;
rx = x;
ry = y;
}
}
}
printf("%lld\n%d %d\n", ret, rx, ry);
}
|
[["-", 341, 342, 0, 16, 31, 23, 0, 16, 17, 33], ["+", 0, 16, 31, 23, 0, 16, 31, 16, 17, 72], ["+", 341, 342, 0, 16, 31, 23, 0, 16, 17, 33], ["+", 341, 342, 0, 16, 31, 23, 0, 16, 12, 13]]
| 1
| 448
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 1001001001001001;
int main() {
int w, h, n;
scanf("%d %d %d", &w, &h, &n);
vector<int> X, Y, tX, tY;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
X.push_back(x);
tX.push_back(x);
Y.push_back(y);
tY.push_back(y);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
LL resd = INF;
int resx, resy;
for (int i = n / 2 - !(n % 2); i <= n / 2; i++) {
for (int j = n / 2 - !(n % 2); j <= n / 2; j++) {
LL sum = 0;
LL maxi = 0;
for (int k = 0; k < n; k++) {
LL d = abs(tX[k] - X[i]) + abs(tY[k] - Y[j]);
sum += d * 2;
maxi = max(maxi, d);
}
sum -= maxi;
if (resd > sum) {
resd = sum;
resx = X[i];
resy = Y[i];
}
}
}
printf("%lld\n%d %d\n", resd, resx, resy);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 1001001001001001LL;
int main() {
int w, h, n;
scanf("%d %d %d", &w, &h, &n);
vector<int> X, Y, tX, tY;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
X.push_back(x);
tX.push_back(x);
Y.push_back(y);
tY.push_back(y);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
LL resd = INF;
int resx, resy;
for (int i = n / 2 - !(n % 2); i <= n / 2; i++) {
for (int j = n / 2 - !(n % 2); j <= n / 2; j++) {
LL sum = 0;
LL maxi = 0;
for (int k = 0; k < n; k++) {
LL d = abs(tX[k] - X[i]) + abs(tY[k] - Y[j]);
sum += d * 2;
maxi = max(maxi, d);
}
sum -= maxi;
if (resd > sum) {
resd = sum;
resx = X[i];
resy = Y[j];
}
}
}
printf("%lld\n%d %d\n", resd, resx, resy);
return 0;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22]]
| 1
| 333
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long int ll;
int W, H;
int N;
int X[100000];
int Y[100000];
int XC[100000];
int YC[100000];
ll ans;
int ansx, ansy;
int main() {
scanf("%d%d", &W, &H);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d%d", &X[i], &Y[i]);
XC[i] = X[i];
YC[i] = Y[i];
}
sort(XC, XC + N);
sort(YC, YC + N);
if (N & 1) {
ll ma = 0;
ansx = XC[N / 2];
ansy = YC[N / 2];
for (int i = 0; i < N; i++) {
ans += (ll)(abs(X[i] - ansx));
ans += (ll)(abs(Y[i] - ansy));
ma = max(ma, (ll)(abs(X[i] - ansx) + abs(Y[i] - ansy)));
}
printf("%lld\n%d %d\n", 2 * ans - ma, ansx, ansy);
} else {
int ansx1 = XC[N / 2 - 1];
int ansy1 = YC[N / 2 - 1];
ll ans1 = 0;
ll ma = 0;
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
ansx = ansx1;
ansy = ansy1;
ans = 2 * ans1 - ma;
ma = 0;
ansx1 = XC[N / 2 - 1];
ansy1 = YC[N / 2];
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
if (2 * ans1 - ma < ans) {
ans = 2 * ans1 - ma;
ansx = ansx1;
ansy = ansy1;
}
ma = 0;
ans1 = 0;
ansx1 = XC[N / 2];
ansy1 = YC[N / 2 - 1];
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
if (2 * ans1 - ma < ans) {
ans = 2 * ans1 - ma;
ansx = ansx1;
ansy = ansy1;
}
ma = 0;
ans1 = 0;
ansx1 = XC[N / 2];
ansy1 = YC[N / 2];
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
if (2 * ans1 - ma < ans) {
ans = 2 * ans1 - ma;
ansx = ansx1;
ansy = ansy1;
}
printf("%lld\n%d %d\n", ans, ansx, ansy);
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long int ll;
int W, H;
int N;
int X[100000];
int Y[100000];
int XC[100000];
int YC[100000];
ll ans;
int ansx, ansy;
int main() {
scanf("%d%d", &W, &H);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d%d", &X[i], &Y[i]);
XC[i] = X[i];
YC[i] = Y[i];
}
sort(XC, XC + N);
sort(YC, YC + N);
if (N & 1) {
ll ma = 0;
ansx = XC[N / 2];
ansy = YC[N / 2];
for (int i = 0; i < N; i++) {
ans += (ll)(abs(X[i] - ansx));
ans += (ll)(abs(Y[i] - ansy));
ma = max(ma, (ll)(abs(X[i] - ansx) + abs(Y[i] - ansy)));
}
printf("%lld\n%d %d\n", 2 * ans - ma, ansx, ansy);
} else {
int ansx1 = XC[N / 2 - 1];
int ansy1 = YC[N / 2 - 1];
ll ans1 = 0;
ll ma = 0;
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
ansx = ansx1;
ansy = ansy1;
ans = 2 * ans1 - ma;
ma = 0;
ans1 = 0;
ansx1 = XC[N / 2 - 1];
ansy1 = YC[N / 2];
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
if (2 * ans1 - ma < ans) {
ans = 2 * ans1 - ma;
ansx = ansx1;
ansy = ansy1;
}
ma = 0;
ans1 = 0;
ansx1 = XC[N / 2];
ansy1 = YC[N / 2 - 1];
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
if (2 * ans1 - ma < ans) {
ans = 2 * ans1 - ma;
ansx = ansx1;
ansy = ansy1;
}
ma = 0;
ans1 = 0;
ansx1 = XC[N / 2];
ansy1 = YC[N / 2];
for (int i = 0; i < N; i++) {
ans1 += (ll)(abs(X[i] - ansx1));
ans1 += (ll)(abs(Y[i] - ansy1));
ma = max(ma, (ll)(abs(X[i] - ansx1) + abs(Y[i] - ansy1)));
}
if (2 * ans1 - ma < ans) {
ans = 2 * ans1 - ma;
ansx = ansx1;
ansy = ansy1;
}
printf("%lld\n%d %d\n", ans, ansx, ansy);
}
return 0;
}
|
[["+", 75, 76, 0, 9, 0, 1, 0, 11, 31, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 17, 32], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 57, 75, 76, 0, 9, 0, 1, 0, 35]]
| 1
| 855
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define MAX_N 111111
typedef long long int ll;
int W, H;
int N;
ll A[MAX_N];
ll B[MAX_N];
int main() {
scanf("%d%d%d", &W, &H, &N);
for (int i = 0; i < N; i++) {
scanf("%lld%lld", &A[i], &B[i]);
}
if (N & 1) {
int n = N / 2;
ll x, y;
vector<ll> X, Y;
for (int i = 0; i < N; i++) {
X.pb(A[i]);
Y.pb(B[i]);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
x = X[n];
y = Y[n];
ll ans = 0;
ll M = 0;
for (int i = 0; i < N; i++) {
M = max(M, abs(x - A[i] + abs(y - B[i])));
ans += abs(x - A[i]);
ans += abs(y - B[i]);
}
printf("%lld\n%lld %lld\n", 2 * ans - M, x, y);
} else {
int n = (N - 1) / 2;
ll x[2], y[2];
vector<ll> X, Y;
for (int i = 0; i < N; i++) {
X.pb(A[i]);
Y.pb(B[i]);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
for (int i = 0; i < 2; i++) {
x[i] = X[n + i];
y[i] = Y[n + i];
}
ll ans[4] = {0};
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
ll M = 0;
for (int i = 0; i < N; i++) {
M = max(abs(x[j] - A[i]) + abs(y[k] - B[i]), M);
ans[j * 2 + k] += abs(x[j] - A[i]);
ans[j * 2 + k] += abs(y[k] - B[i]);
}
ans[j * 2 + k] *= 2;
ans[j * 2 + k] -= M;
}
}
ll C = min(min(ans[0], ans[1]), min(ans[2], ans[3]));
printf("%lld\n", C);
for (int i = 0; i < 4; i++) {
// printf("ans[%d]=%lld\n",i,ans[i]);
if (C == ans[i]) {
printf("%lld %lld\n", x[i / 2], y[i % 2]);
break;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define MAX_N 111111
typedef long long int ll;
int W, H;
int N;
ll A[MAX_N];
ll B[MAX_N];
int main() {
scanf("%d%d%d", &W, &H, &N);
for (int i = 0; i < N; i++) {
scanf("%lld%lld", &A[i], &B[i]);
}
if (N & 1) {
int n = N / 2;
ll x, y;
vector<ll> X, Y;
for (int i = 0; i < N; i++) {
X.pb(A[i]);
Y.pb(B[i]);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
x = X[n];
y = Y[n];
ll ans = 0;
ll M = 0;
for (int i = 0; i < N; i++) {
M = max(M, abs(x - A[i]) + abs(y - B[i]));
ans += abs(x - A[i]);
ans += abs(y - B[i]);
}
printf("%lld\n%lld %lld\n", 2 * ans - M, x, y);
} else {
int n = (N - 1) / 2;
ll x[2], y[2];
vector<ll> X, Y;
for (int i = 0; i < N; i++) {
X.pb(A[i]);
Y.pb(B[i]);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
for (int i = 0; i < 2; i++) {
x[i] = X[n + i];
y[i] = Y[n + i];
}
ll ans[4] = {0};
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
ll M = 0;
for (int i = 0; i < N; i++) {
M = max(abs(x[j] - A[i]) + abs(y[k] - B[i]), M);
ans[j * 2 + k] += abs(x[j] - A[i]);
ans[j * 2 + k] += abs(y[k] - B[i]);
}
ans[j * 2 + k] *= 2;
ans[j * 2 + k] -= M;
}
}
ll C = min(min(ans[0], ans[1]), min(ans[2], ans[3]));
printf("%lld\n", C);
for (int i = 0; i < 4; i++) {
// printf("ans[%d]=%lld\n",i,ans[i]);
if (C == ans[i]) {
printf("%lld %lld\n", x[i / 2], y[i % 2]);
break;
}
}
}
return 0;
}
|
[["+", 3, 4, 0, 16, 31, 2, 3, 4, 0, 25], ["-", 3, 4, 0, 16, 12, 2, 3, 4, 0, 25]]
| 1
| 698
|
#include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(x) __builtin_ctz(x)
// #define DEBUG
#ifdef DEBUG
#define dump(...) fprintf(stderr, __VA_ARGS__)
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const int nmax = 200010;
ll x[nmax], y[nmax];
ll sx[nmax], sy[nmax];
int main(void) {
int w, h, n;
cin >> w >> h >> n;
rep(i, n) cin >> x[i] >> y[i];
rep(i, n) x[n + i] = x[i], y[n + i] = y[i];
rep(i, 2 * n) sx[i] = x[i], sy[i] = y[i];
sort(sx, sx + 2 * n), sort(sy, sy + 2 * n);
ll ax[2] = {sx[n - 1], sx[n]};
ll ay[2] = {sy[n - 1], sy[n]};
ll all[2][2];
rep(xi, 2) rep(yi, 2) {
all[xi][yi] = 0LL;
rep(i, n) {
ll cur = abs(x[i] - ax[xi]) + abs(y[i] - ay[yi]);
all[xi][yi] += 2LL * cur;
}
}
ll cmax[2][2] = {{0, 0}, {0, 0}};
rep(i, n) {
int xi = lower_bound(sx, sx + 2 * n, x[i]) - sx;
int yi = lower_bound(sy, sy + 2 * n, y[i]) - sy;
xi = (xi < n), yi = (yi < n);
ll cur = abs(x[i] - ax[xi]) + abs(y[i] - ay[yi]);
cmax[xi][yi] = max(cmax[xi][yi], cur);
}
ll ans = all[0][0] - cmax[0][0];
rep(i, 2) rep(j, 2) ans = min(ans, all[i][j] - cmax[i][j]);
cout << ans << endl;
rep(j, 2) rep(i, 2) {
if (all[i][j] - cmax[i][j] == ans) {
cout << ax[i] << " " << ay[j] << endl;
return 0;
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(x) __builtin_ctz(x)
// #define DEBUG
#ifdef DEBUG
#define dump(...) fprintf(stderr, __VA_ARGS__)
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const int nmax = 200010;
ll x[nmax], y[nmax];
ll sx[nmax], sy[nmax];
int main(void) {
int w, h, n;
cin >> w >> h >> n;
rep(i, n) cin >> x[i] >> y[i];
rep(i, n) x[n + i] = x[i], y[n + i] = y[i];
rep(i, 2 * n) sx[i] = x[i], sy[i] = y[i];
sort(sx, sx + 2 * n), sort(sy, sy + 2 * n);
ll ax[2] = {sx[n - 1], sx[n]};
ll ay[2] = {sy[n - 1], sy[n]};
ll all[2][2];
rep(xi, 2) rep(yi, 2) {
all[xi][yi] = 0LL;
rep(i, n) {
ll cur = abs(x[i] - ax[xi]) + abs(y[i] - ay[yi]);
all[xi][yi] += 2LL * cur;
}
}
ll cmax[2][2] = {{0, 0}, {0, 0}};
rep(i, n) {
int xi = lower_bound(sx, sx + 2 * n, x[i]) - sx;
int yi = lower_bound(sy, sy + 2 * n, y[i]) - sy;
xi = (xi < n), yi = (yi < n);
ll cur = abs(x[i] - ax[xi]) + abs(y[i] - ay[yi]);
cmax[xi][yi] = max(cmax[xi][yi], cur);
}
ll ans = all[0][0] - cmax[0][0];
rep(i, 2) rep(j, 2) ans = min(ans, all[i][j] - cmax[i][j]);
cout << ans << endl;
rep(i, 2) rep(j, 2) {
if (all[i][j] - cmax[i][j] == ans) {
cout << ax[i] << " " << ay[j] << endl;
return 0;
}
}
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
| 1
| 659
|
#include <bits/stdc++.h>
using namespace std;
int N, x[2][100009], p[100009], z[2][100009];
long long v[2][100009];
int main() {
scanf("%*d%*d%d", &N);
for (int i = 0; i < N; i++)
scanf("%d%d", &x[0][i], &x[1][i]);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < N; j++)
v[i][j] = 1LL << 60, p[j] = j;
sort(p, p + N, [&](int j, int k) { return x[i][j] < x[i][k]; });
for (int j = (N - 1) / 2; j <= N / 2; j++) {
long long sum = 0;
for (int k = 0; k < N; k++)
sum += abs(x[i][p[j]] - x[i][k]) * 2;
for (int k = 0; k < N; k++) {
long long cost = sum - abs(x[i][p[j]] - x[i][k]);
if (v[i][k] > cost)
v[i][k] = cost, z[i][k] = x[i][p[j]];
}
}
}
long long ret = 1LL << 60;
int lp = -1;
for (int i = 0; i < N; i++) {
long long c = v[0][i] + v[1][i];
if (ret > c || (ret == c && (z[0][lp] > z[0][i] ||
(z[0][lp] == z[0][i] && z[1][lp] > z[1][i]))))
ret = c;
lp = i;
}
printf("%lld\n%d %d\n", ret, z[0][lp], z[1][lp]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, x[2][100009], p[100009], z[2][100009];
long long v[2][100009];
int main() {
scanf("%*d%*d%d", &N);
for (int i = 0; i < N; i++)
scanf("%d%d", &x[0][i], &x[1][i]);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < N; j++)
v[i][j] = 1LL << 60, p[j] = j;
sort(p, p + N, [&](int j, int k) { return x[i][j] < x[i][k]; });
for (int j = (N - 1) / 2; j <= N / 2; j++) {
long long sum = 0;
for (int k = 0; k < N; k++)
sum += abs(x[i][p[j]] - x[i][k]) * 2;
for (int k = 0; k < N; k++) {
long long cost = sum - abs(x[i][p[j]] - x[i][k]);
if (v[i][k] > cost)
v[i][k] = cost, z[i][k] = x[i][p[j]];
}
}
}
long long ret = 1LL << 60;
int lp = -1;
for (int i = 0; i < N; i++) {
long long c = v[0][i] + v[1][i];
if (ret > c || (ret == c && (z[0][lp] > z[0][i] ||
(z[0][lp] == z[0][i] && z[1][lp] > z[1][i]))))
ret = c, lp = i;
}
printf("%lld\n%d %d\n", ret, z[0][lp], z[1][lp]);
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 57, 64, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 1, 0, 34, 0, 21]]
| 1
| 490
|
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;
int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];
ll calc(ll X, ll Y) {
ll ma = 0LL;
ll tot = 0LL;
FOR(i, n) {
ll d = labs(X - x[i]) + labs(Y - y[i]);
ma = max(ma, d);
tot += d;
}
return 2 * tot - ma;
}
void solve() {
cin >> h >> w;
cin >> n;
FOR(i, n) cin >> x[i] >> y[i];
FOR(i, n) cx[i] = x[i];
FOR(i, n) cy[i] = y[i];
sort(x, cx + n);
sort(y, cy + n);
ll mx[2], my[2];
mx[0] = cx[(n - 1) >> 1];
mx[1] = cx[n >> 1];
my[0] = cy[(n - 1) >> 1];
my[1] = cy[n >> 1];
ll mi = LONG_MAX / 4, miX, miY;
FOR(i, 2) {
FOR(j, 2) {
ll ret = calc(mx[i], my[j]);
if (ret < mi) {
mi = ret;
miX = mx[i];
miY = my[j];
}
}
}
cout << mi << endl;
cout << miX << " " << miY << endl;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;
int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];
ll calc(ll X, ll Y) {
ll ma = 0LL;
ll tot = 0LL;
FOR(i, n) {
ll d = labs(X - x[i]) + labs(Y - y[i]);
ma = max(ma, d);
tot += d;
}
return 2 * tot - ma;
}
void solve() {
cin >> h >> w;
cin >> n;
FOR(i, n) cin >> x[i] >> y[i];
FOR(i, n) cx[i] = x[i];
FOR(i, n) cy[i] = y[i];
sort(cx, cx + n);
sort(cy, cy + n);
ll mx[2], my[2];
mx[0] = cx[(n - 1) >> 1];
mx[1] = cx[n >> 1];
my[0] = cy[(n - 1) >> 1];
my[1] = cy[n >> 1];
ll mi = LONG_MAX / 4, miX, miY;
FOR(i, 2) {
FOR(j, 2) {
ll ret = calc(mx[i], my[j]);
if (ret < mi) {
mi = ret;
miX = mx[i];
miY = my[j];
}
}
}
cout << mi << endl;
cout << miX << " " << miY << endl;
}
int main() {
solve();
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
| 1
| 427
|
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll x[100000], y[100000], tx[100000], ty[100000], sum1[100001], sum2[100001];
#define dist(x, y) (abs(i - x) + abs(j - y))
int main() {
ll w, h, n;
scanf("%lld%lld%lld", &w, &h, &n);
rep(i, n) {
scanf("%lld%lld", &x[i], &y[i]);
tx[i] = x[i];
ty[i] = y[i];
}
sort(x, x + n);
sort(y, y + n);
rep(i, n) {
sum1[i + 1] = sum1[i] + x[i];
sum2[i + 1] = sum2[i] + y[i];
}
unordered_set<ll> ax, ay;
ll mx = LLONG_MAX, my = LLONG_MAX;
rep(i, n) {
ll a = ((x[i] * i - sum1[i]) + (sum1[n] - sum1[i] - x[i] * (n - i))) * 2;
if (mx > a)
mx = a, ax.clear();
if (mx == a)
ax.insert(x[i]);
a = ((y[i] * i - sum2[i]) + (sum2[n] - sum2[i] - y[i] * (n - i))) * 2;
if (my > a)
my = a, ay.clear();
if (my == a)
ay.insert(y[i]);
}
ll ansx, ansy, ans = LLONG_MAX;
for (ll i : ax)
for (ll j : ay)
rep(k, n) {
ll d = mx + my - dist(tx[k], ty[k]);
if (ans > d) {
ans = d;
ansx = i;
ansy = j;
}
}
printf("%lld\n%lld %lld\n", ans, ansx, ansy);
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll x[100000], y[100000], tx[100000], ty[100000], sum1[100001], sum2[100001];
#define dist(x, y) (abs(i - x) + abs(j - y))
int main() {
ll w, h, n;
scanf("%lld%lld%lld", &w, &h, &n);
rep(i, n) {
scanf("%lld%lld", &x[i], &y[i]);
tx[i] = x[i];
ty[i] = y[i];
}
sort(x, x + n);
sort(y, y + n);
rep(i, n) {
sum1[i + 1] = sum1[i] + x[i];
sum2[i + 1] = sum2[i] + y[i];
}
set<ll> ax, ay;
ll mx = LLONG_MAX, my = LLONG_MAX;
rep(i, n) {
ll a = ((x[i] * i - sum1[i]) + (sum1[n] - sum1[i] - x[i] * (n - i))) * 2;
if (mx > a)
mx = a, ax.clear();
if (mx == a)
ax.insert(x[i]);
a = ((y[i] * i - sum2[i]) + (sum2[n] - sum2[i] - y[i] * (n - i))) * 2;
if (my > a)
my = a, ay.clear();
if (my == a)
ay.insert(y[i]);
}
ll ansx, ansy, ans = LLONG_MAX;
for (ll i : ax)
for (ll j : ay)
rep(k, n) {
ll d = mx + my - dist(tx[k], ty[k]);
if (ans > d) {
ans = d;
ansx = i;
ansy = j;
}
}
printf("%lld\n%lld %lld\n", ans, ansx, ansy);
}
|
[["-", 0, 14, 8, 9, 0, 43, 39, 344, 141, 78], ["+", 0, 14, 8, 9, 0, 43, 39, 344, 141, 78]]
| 1
| 466
|
#include <algorithm>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
signed main() {
int w, h, n, x[100000], y[100000], mx[100000], my[100000];
cin >> w >> h >> n;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
mx[i] = x[i];
my[i] = y[i];
}
sort(x, x + n);
sort(y, y + n);
int sum = 0, ma = 0, xx, yy;
if (n % 2 == 1) {
for (int i = 0; i < n; i++) {
int d = abs(x[n / 2] - mx[i]) + abs(y[n / 2] - my[i]);
sum += d;
ma = max(ma, d);
}
cout << sum * 2 - ma << endl;
cout << x[n / 2] << " " << y[n / 2] << endl;
} else {
int mas = 1e+9;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
sum = 0;
ma = 0;
for (int i = 0; i < n; i++) {
int d = abs(x[n / 2 - j] - mx[i]) + abs(y[n / 2 - k] - my[i]);
sum += d;
ma = max(ma, d);
}
if (mas > sum * 2 - ma) {
mas = min(mas, sum * 2 - ma);
xx = x[n / 2 - j];
yy = y[n / 2 - k];
}
}
}
cout << mas << endl;
cout << xx << " " << yy << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
signed main() {
int w, h, n, x[100000], y[100000], mx[100000], my[100000];
cin >> w >> h >> n;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
mx[i] = x[i];
my[i] = y[i];
}
sort(x, x + n);
sort(y, y + n);
int sum = 0, ma = 0, xx, yy;
if (n % 2 == 1) {
for (int i = 0; i < n; i++) {
int d = abs(x[n / 2] - mx[i]) + abs(y[n / 2] - my[i]);
sum += d;
ma = max(ma, d);
}
cout << sum * 2 - ma << endl;
cout << x[n / 2] << " " << y[n / 2] << endl;
} else {
int mas = 1e+18;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
sum = 0;
ma = 0;
for (int i = 0; i < n; i++) {
int d = abs(x[n / 2 - j] - mx[i]) + abs(y[n / 2 - k] - my[i]);
sum += d;
ma = max(ma, d);
}
if (mas >= sum * 2 - ma) {
mas = min(mas, sum * 2 - ma);
xx = x[n / 2 - j];
yy = y[n / 2 - k];
}
}
}
cout << mas << endl;
cout << xx << " " << yy << endl;
}
return 0;
}
|
[["-", 75, 76, 0, 9, 0, 43, 49, 50, 51, 13], ["+", 75, 76, 0, 9, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
| 1
| 419
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
#define MAX_H 1000000000 // 10^9
#define MAX_W 1000000000 // 10^9
#define MAX_N 100000 // 10^5
#define mp make_pair
#define INF 1001001001
#define LLINF 1001001001001001001
#define llint long long int
#define ppp pair<int, pair<int, int>>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
llint homesx[MAX_N];
llint homesy[MAX_N];
llint homesxs[MAX_N];
llint homesys[MAX_N];
int main(void) {
// Your code here!
llint W, H, N;
scanf("%lld %lld %lld", &W, &H, &N);
rep(i, N) {
llint X, Y;
scanf("%lld %lld", &X, &Y);
X--;
Y--;
homesx[i] = X;
homesy[i] = Y;
homesxs[i] = X;
homesys[i] = Y;
}
// X座標について求める
if (N == 1) {
printf("%lld\n%lld %lld", 0, homesx[0], homesy[0]);
return 0;
}
llint ans = LLINF;
llint zans[2] = {-1, -1};
sort(&homesxs[0], &homesxs[N]);
sort(&homesys[0], &homesys[N]);
llint Center1 = (llint)(N / 2) - 1;
llint Center2 = (llint)(N / 2);
llint sum = 0;
llint maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center1]) + abs(homesy[i] - homesys[Center1]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center1];
zans[1] = homesys[Center1];
}
sum = 0;
maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center1]) + abs(homesy[i] - homesys[Center2]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center1];
zans[1] = homesys[Center2];
}
sum = 0;
maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center2]) + abs(homesy[i] - homesys[Center1]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center2];
zans[1] = homesys[Center1];
}
sum = 0;
maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center2]) + abs(homesy[i] - homesys[Center2]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center2];
zans[1] = homesys[Center2];
}
printf("%lld\n%lld %lld\n", ans, zans[0] + 1, zans[1] + 1);
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
#define MAX_H 1000000000 // 10^9
#define MAX_W 1000000000 // 10^9
#define MAX_N 100000 // 10^5
#define mp make_pair
#define INF 1001001001
#define LLINF 1001001001001001001
#define llint long long int
#define ppp pair<int, pair<int, int>>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
llint homesx[MAX_N];
llint homesy[MAX_N];
llint homesxs[MAX_N];
llint homesys[MAX_N];
int main(void) {
// Your code here!
llint W, H, N;
scanf("%lld %lld %lld", &W, &H, &N);
rep(i, N) {
llint X, Y;
scanf("%lld %lld", &X, &Y);
X--;
Y--;
homesx[i] = X;
homesy[i] = Y;
homesxs[i] = X;
homesys[i] = Y;
}
// X座標について求める
if (N == 1) {
printf("%lld\n%lld %lld\n", 0, homesx[0] + 1, homesy[0] + 1);
return 0;
}
llint ans = LLINF;
llint zans[2] = {-1, -1};
sort(&homesxs[0], &homesxs[N]);
sort(&homesys[0], &homesys[N]);
llint Center1 = (llint)(N / 2) - 1;
llint Center2 = (llint)(N / 2);
llint sum = 0;
llint maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center1]) + abs(homesy[i] - homesys[Center1]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center1];
zans[1] = homesys[Center1];
}
sum = 0;
maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center1]) + abs(homesy[i] - homesys[Center2]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center1];
zans[1] = homesys[Center2];
}
sum = 0;
maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center2]) + abs(homesy[i] - homesys[Center1]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center2];
zans[1] = homesys[Center1];
}
sum = 0;
maxl = 0;
rep(i, N) {
llint leng =
abs(homesx[i] - homesxs[Center2]) + abs(homesy[i] - homesys[Center2]);
sum += 2 * leng;
maxl = max(leng, maxl);
}
if (ans > sum - maxl) {
ans = sum - maxl;
zans[0] = homesxs[Center2];
zans[1] = homesys[Center2];
}
printf("%lld\n%lld %lld\n", ans, zans[0] + 1, zans[1] + 1);
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 683
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
long long int INF = (long long)10000000000 * 100000;
long long int ans = 0;
long long int w, h;
long long int x[100001], y[100001];
vector<long long int> vx;
vector<long long int> vy;
long long rx, ry;
int n;
long long int dfs(long long int sx, long long int sy) {
vector<long long int> v;
for (int i = 0; i < n; i++) {
v.push_back(abs(sx - x[i]) + abs(sy - y[i]));
}
sort(v.begin(), v.end());
long long res = 0;
for (int i = 0; i < n; i++) {
if (i < n - 1)
res += v[i] * 2;
else
res += v[i];
}
return res;
}
int main(void) {
cin >> w >> h;
ans = INF;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
vx.push_back(x[i]);
vy.push_back(y[i]);
}
if (n == 1) {
cout << 0 << endl;
cout << x[0] << " " << y[0] << endl;
} else {
sort(vx.begin(), vx.end());
sort(vy.begin(), vy.end());
for (int j = -1; j <= 1; j += 2) {
for (int i = -1; i <= 1; i += 2) {
long long int res = dfs(vx[(n + i) / 2], vy[(n + j) / 2]);
if (res < ans) {
rx = vx[(long long)((n + i) / 2)];
ry = vy[(long long)((n + j) / 2)];
ans = res;
}
}
}
cout << ans << endl;
cout << rx << " " << ry << endl;
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
long long int INF = (long long)10000000000 * 100000;
long long int ans = 0;
long long int w, h;
long long int x[100001], y[100001];
vector<long long int> vx;
vector<long long int> vy;
long long rx, ry;
int n;
long long int dfs(long long int sx, long long int sy) {
vector<long long int> v;
for (int i = 0; i < n; i++) {
v.push_back(abs(sx - x[i]) + abs(sy - y[i]));
}
sort(v.begin(), v.end());
long long res = 0;
for (int i = 0; i < n; i++) {
if (i < n - 1)
res += v[i] * 2;
else
res += v[i];
}
return res;
}
int main(void) {
cin >> w >> h;
ans = INF;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
vx.push_back(x[i]);
vy.push_back(y[i]);
}
if (n == 1) {
cout << 0 << endl;
cout << x[0] << " " << y[0] << endl;
} else {
sort(vx.begin(), vx.end());
sort(vy.begin(), vy.end());
for (int j = -1; j <= 1; j += 2) {
for (int i = -1; i <= 1; i += 2) {
long long int res = dfs(vx[(n + j) / 2], vy[(n + i) / 2]);
if (res < ans) {
rx = vx[(long long)((n + j) / 2)];
ry = vy[(long long)((n + i) / 2)];
ans = res;
}
}
}
cout << ans << endl;
cout << rx << " " << ry << endl;
}
return 0;
}
|
[["-", 341, 342, 0, 16, 31, 23, 0, 16, 12, 22], ["+", 341, 342, 0, 16, 31, 23, 0, 16, 12, 22], ["-", 51, 23, 0, 16, 31, 23, 0, 16, 12, 22], ["+", 51, 23, 0, 16, 31, 23, 0, 16, 12, 22]]
| 1
| 481
|
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int ox[100000];
int oy[100000];
int x[100000];
int y[100000];
int n;
long long calc(int a, int b) {
int md = 0, i;
long long ans = 0;
for (i = 0; i < n; i++) {
int d = abs(ox[i] - a) + abs(oy[i] - b);
md = max(md, d);
ans += (long long)d * 2;
}
return ans - (long long)md;
}
int max(int a, int b) { return a > b ? a : b; }
int main() {
int w, h, i, j;
scanf("%d %d", &w, &h);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &ox[i], &oy[i]);
x[i] = ox[i];
y[i] = oy[i];
}
sort(x, x + n);
sort(y, y + n);
long long ans = 1000000000000000000LL;
int ax[2];
int ay[2];
ax[0] = x[n / 2];
ay[0] = y[n / 2];
int nx, ny;
if (n % 2 == 0) {
ax[1] = x[n / 2 - 1];
ay[1] = y[n / 2 - 1];
for (i = 1; i >= 0; i++)
for (j = 1; j >= 0; j++) {
if (ans > calc(ax[i], ay[j])) {
ans = calc(ax[i], ay[j]);
nx = ax[i];
ny = ay[j];
}
}
} else {
ans = calc(ax[0], ay[0]);
nx = ax[0];
ny = ay[0];
}
printf("%lld\n", ans);
printf("%d %d\n", nx, ny);
return 0;
}
|
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int ox[100000];
int oy[100000];
int x[100000];
int y[100000];
int n;
long long calc(int a, int b) {
int md = 0, i;
long long ans = 0;
for (i = 0; i < n; i++) {
int d = abs(ox[i] - a) + abs(oy[i] - b);
md = max(md, d);
ans += (long long)d * 2;
}
return ans - (long long)md;
}
int max(int a, int b) { return a > b ? a : b; }
int main() {
int w, h, i, j;
scanf("%d %d", &w, &h);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &ox[i], &oy[i]);
x[i] = ox[i];
y[i] = oy[i];
}
sort(x, x + n);
sort(y, y + n);
long long ans = 1000000000000000000LL;
int ax[2];
int ay[2];
ax[0] = x[n / 2];
ay[0] = y[n / 2];
int nx, ny;
if (n % 2 == 0) {
ax[1] = x[n / 2 - 1];
ay[1] = y[n / 2 - 1];
for (i = 1; i >= 0; i--)
for (j = 1; j >= 0; j--) {
if (ans > calc(ax[i], ay[j])) {
ans = calc(ax[i], ay[j]);
nx = ax[i];
ny = ay[j];
}
}
} else {
ans = calc(ax[0], ay[0]);
nx = ax[0];
ny = ay[0];
}
printf("%lld\n", ans);
printf("%d %d\n", nx, ny);
return 0;
}
|
[["-", 0, 57, 64, 9, 0, 7, 26, 27, 17, 29], ["+", 0, 57, 64, 9, 0, 7, 26, 27, 17, 68], ["-", 64, 9, 0, 7, 8, 7, 26, 27, 17, 29], ["+", 64, 9, 0, 7, 8, 7, 26, 27, 17, 68]]
| 1
| 476
|
// AOJ 0564: Bug Party
// 2018.1.27 bal4u
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a, b;
} T;
T tbl[300002];
long long sa[300002];
int n;
#define MAX 300002
int que[MAX];
int qsize;
#define PARENT(i) ((i) >> 1)
#define LEFT(i) ((i) << 1)
#define RIGHT(i) (((i) << 1) + 1)
void max_heapify(int i) {
int l, r, max;
int qt;
l = LEFT(i), r = RIGHT(i);
if (l < qsize && que[l] > que[i])
max = l;
else
max = i;
if (r < qsize && que[r] > que[max])
max = r;
if (max != i) {
qt = que[i], que[i] = que[max], que[max] = qt;
max_heapify(max);
}
}
int deq() {
int t;
if (qsize == 0)
return 0;
t = que[0];
que[0] = que[--qsize];
max_heapify(0);
return t;
}
void enq(int t) {
int i, max;
int qt;
i = qsize++;
que[i] = t;
while (i > 0 && que[max = PARENT(i)] < que[i]) {
qt = que[i];
que[i] = que[max], que[max] = qt;
i = max;
}
}
//#define getchar_unlocked() getchar()
int in() {
int n = 0;
int c = getchar_unlocked();
do
n = (n << 3) + (n << 1) + (c & 0xf), c = getchar_unlocked();
while (c >= '0');
return n;
}
int cmp(T *a, T *b) { return b->b - a->b; }
int check(int k) {
int i;
long long s;
if (!k)
return 1;
s = 0, qsize = 0;
for (i = 0; i < k - 1; i++)
s += tbl[i].a, enq(tbl[i].a);
for (; i < n; i++) {
if (s + tbl[i].a <= (long long)k * tbl[i].b)
return 1;
s += tbl[i].a, enq(tbl[i].a);
s -= deq();
}
return 0;
}
int main() {
int i, lo, hi, m;
n = in();
for (i = 0; i < n; i++)
tbl[i].a = in(), tbl[i].b = in();
qsort(tbl, n, sizeof(T), cmp);
lo = 0, hi = n;
while (lo + 1 < hi) {
m = (lo + hi) >> 1;
if (check(m))
lo = m;
else
hi = m;
}
printf("%d\n", lo);
return 0;
}
|
// AOJ 0564: Bug Party
// 2018.1.27 bal4u
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a, b;
} T;
T tbl[300002];
long long sa[300002];
int n;
#define MAX 300002
int que[MAX];
int qsize;
#define PARENT(i) ((i) >> 1)
#define LEFT(i) ((i) << 1)
#define RIGHT(i) (((i) << 1) + 1)
void max_heapify(int i) {
int l, r, max;
int qt;
l = LEFT(i), r = RIGHT(i);
if (l < qsize && que[l] > que[i])
max = l;
else
max = i;
if (r < qsize && que[r] > que[max])
max = r;
if (max != i) {
qt = que[i], que[i] = que[max], que[max] = qt;
max_heapify(max);
}
}
int deq() {
int t;
if (qsize == 0)
return 0;
t = que[0];
que[0] = que[--qsize];
max_heapify(0);
return t;
}
void enq(int t) {
int i, max;
int qt;
i = qsize++;
que[i] = t;
while (i > 0 && que[max = PARENT(i)] < que[i]) {
qt = que[i];
que[i] = que[max], que[max] = qt;
i = max;
}
}
//#define getchar_unlocked() getchar()
int in() {
int n = 0;
int c = getchar_unlocked();
do
n = (n << 3) + (n << 1) + (c & 0xf), c = getchar_unlocked();
while (c >= '0');
return n;
}
int cmp(T *a, T *b) { return b->b - a->b; }
int check(int k) {
int i;
long long s;
if (!k)
return 1;
s = 0, qsize = 0;
for (i = 0; i < k - 1; i++)
s += tbl[i].a, enq(tbl[i].a);
for (; i < n; i++) {
if (s + tbl[i].a <= (long long)k * tbl[i].b)
return 1;
s += tbl[i].a, enq(tbl[i].a);
s -= deq();
}
return 0;
}
int main() {
int i, lo, hi, m;
n = in();
for (i = 0; i < n; i++)
tbl[i].a = in(), tbl[i].b = in();
qsort(tbl, n, sizeof(T), cmp);
lo = 0, hi = n + 1;
while (lo + 1 < hi) {
m = (lo + hi) >> 1;
if (check(m))
lo = m;
else
hi = m;
}
printf("%d\n", lo);
return 0;
}
|
[["+", 0, 1, 0, 34, 12, 11, 12, 16, 17, 72], ["+", 0, 1, 0, 34, 12, 11, 12, 16, 12, 13]]
| 0
| 639
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.