buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
#import <cstdio> #define r(a) for (int i = 0; i < a; i++) #define s scanf("%d", &w), w; #define c(a, b) a = a < b ? a : b int w, n, m, d[999], p[999]; main() { n = s m = s r(n) d[i] = s r(n) p[i] = 1e9; r(m) { s r(n) c(p[i], p[i + 1] + w * d[n - i - 1]); } printf("%d\n", *p); }
#import <cstdio> #define r(a) for (int i = 0; i < a; i++) #define s (scanf("%d", &w), w) #define c(a, b) a = a < b ? a : b int w, n, m, d[999], p[999]; main() { n = s; m = s; r(n) d[i] = s; r(n) p[i] = 1e9; r(m) { s; r(n) c(p[i], p[i + 1] + w * d[n - i - 1]); } printf("%d\n", *p); }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 14, 8, 9, 0, 35], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
116
#import <cstdio> #define r(a) for (int i = 0; i < a; i++) #define s(a) scanf("%d", &a); #define c(a, b) a = a < b ? a : b int w, n, m, d[999], *p; main() { s(n) s(m) r(n) s(d[i]) r(n) p[i] = 1e9; r(m) { s(w) r(n) c(p[i], p[i + 1] + w * d[n - i - 1]); } printf("%d\n", *p); }
#import <cstdio> #define r(a) for (int i = 0; i < a; i++) #define s(n) scanf("%d", &n); #define c(a, b) a = a < b ? a : b int w, n, m, d[999], p[999]; main() { s(n) s(m) r(n) s(d[i]) r(n) p[i] = 1e9; r(m) { s(w) r(n) c(p[i], p[i + 1] + w * d[n - i - 1]); } printf("%d\n", *p); }
[["-", 36, 36, 0, 30, 0, 112, 54, 158, 0, 22], ["+", 36, 36, 0, 30, 0, 112, 54, 158, 0, 22], ["-", 36, 36, 36, 36, 0, 30, 0, 112, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 112, 51, 59], ["-", 36, 36, 0, 30, 0, 43, 49, 84, 0, 48], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 0, 70], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 0, 73]]
1
123
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; long long int fie[1001][1001]; int main() { int N, M; cin >> N >> M; int city[1001], weather[1001]; for (int i = 0; i < N; i++) cin >> city[i]; for (int i = 0; i < M; i++) cin >> weather[i]; for (int i = 0; i < 1001; i++) { for (int j = 0; j < 1001; j++) { fie[i][j] = 1000000000000000; } } fie[0][0] = 0; for (int i = 1; i < M; i++) { for (int j = 0; j < N + 1; j++) { if (j != 0) fie[i][j] = min(fie[i][j], min(fie[i - 1][j], fie[i - 1][j - 1] + city[j - 1] * weather[i - 1])); else fie[i][j] = min(fie[i][j], fie[i - 1][j]); } } cout << fie[M - 1][N] << endl; /* for(int i=0;i<M+1;i++){ for(int j=0;j<N+2;j++){ printf("[%lld]",fie[i][j]); } puts(""); } */ return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; long long int fie[1001][1001]; int main() { int N, M; cin >> N >> M; int city[1001], weather[1001]; for (int i = 0; i < N; i++) cin >> city[i]; for (int i = 0; i < M; i++) cin >> weather[i]; for (int i = 0; i < 1001; i++) { for (int j = 0; j < 1001; j++) { fie[i][j] = 1000000000000000; } } fie[0][0] = 0; for (int i = 1; i < M + 1; i++) { for (int j = 0; j < N + 1; j++) { if (j != 0) fie[i][j] = min(fie[i][j], min(fie[i - 1][j], fie[i - 1][j - 1] + city[j - 1] * weather[i - 1])); else fie[i][j] = min(fie[i][j], fie[i - 1][j]); } } cout << fie[M][N] << endl; return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["-", 12, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["-", 12, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
291
#include <algorithm> #include <stdio.h> #include <string.h> using namespace std; const int MAX_N = 1000; const int MAX_M = 1000; const int INF = 1000000000000; int dp[MAX_M][MAX_N]; int dis[MAX_N]; int wea[MAX_M]; int n, m; int rec(int i, int j) { if (j == n) { return 0; } if (i == m) { return INF; } if (dp[i][j] >= 0) { return dp[i][j]; } return dp[i][j] = min(rec(i + 1, j), rec(i + 1, j + 1) + wea[i] * dis[j]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &dis[i]); } for (int i = 0; i < m; i++) { scanf("%d", &wea[i]); } memset(dp, -1, sizeof(dp)); printf("%d\n", rec(0, 0)); return 0; }
#include <algorithm> #include <stdio.h> #include <string.h> using namespace std; const int MAX_N = 1000; const int MAX_M = 1000; const int INF = 1000000000; int dp[MAX_M][MAX_N]; int dis[MAX_N]; int wea[MAX_M]; int n, m; int rec(int i, int j) { if (j == n) { return 0; } if (i == m) { return INF; } if (dp[i][j] >= 0) { return dp[i][j]; } return dp[i][j] = min(rec(i + 1, j), rec(i + 1, j + 1) + wea[i] * dis[j]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &dis[i]); } for (int i = 0; i < m; i++) { scanf("%d", &wea[i]); } memset(dp, -1, sizeof(dp)); printf("%d\n", rec(0, 0)); return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
1
259
#include <stdio.h> long long int dp[1001][1001]; int main(void) { long long int min, a[1001], b[1001], n, m, i, j; min = 9223372036854775806; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (j = 0; j < m; j++) scanf("%d", &b[j]); for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) dp[i][j] = min; } dp[0][0] = 0; for (i = 0; i < m; i++) { for (j = 0; j <= n; j++) { if (dp[i][j] != min) { if (dp[i][j] < dp[i + 1][j]) dp[i + 1][j] = dp[i][j]; if (dp[i + 1][j + 1] > dp[i][j] + a[j] * b[i]) { dp[i + 1][j + 1] = dp[i][j] + a[j] * b[i]; } } } } /* for(i=0;i<=m;i++){ for(j=0;j<=n;j++)printf("%d ",dp[i][j]); printf("\n"); }*/ for (i = 0; i <= m; i++) { if (min > dp[i][n]) min = dp[i][n]; } printf("%d\n", min); return 0; }
#include <stdio.h> long long int dp[1001][1001]; int main(void) { long long int min, a[1001], b[1001], n, m, i, j; min = 9223372036854775806; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%lld", &a[i]); for (j = 0; j < m; j++) scanf("%lld", &b[j]); for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) dp[i][j] = min; } dp[0][0] = 0; for (i = 0; i < m; i++) { for (j = 0; j <= n; j++) { if (dp[i][j] != min) { if (dp[i][j] < dp[i + 1][j]) dp[i + 1][j] = dp[i][j]; if (dp[i + 1][j + 1] > dp[i][j] + a[j] * b[i]) { dp[i + 1][j + 1] = dp[i][j] + a[j] * b[i]; } } } } /* for(i=0;i<=m;i++){ for(j=0;j<=n;j++)printf("%d ",dp[i][j]); printf("\n"); }*/ for (i = 0; i <= m; i++) { if (min > dp[i][n]) min = dp[i][n]; } printf("%lld\n", min); return 0; }
[["-", 8, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 8, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
357
#include <iostream> using namespace std; int main() { int a[1000], b[1000], n, m, i, j; cin >> n >> m; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < m; i++) cin >> b[i]; int small[1000][1000] = {a[0] * b[0]}; for (i = 1; i < n; i++) small[i][i] = small[i - 1][j - 1] + a[i] * b[j]; for (i = 1; i < m; i++) small[0][i] = min(a[0] * b[i], small[0][i - 1]); for (i = 1; i < n; i++) for (j = i + 1; j < m; j++) small[i][j] = min(small[i - 1][j - 1] + a[i] * b[j], small[i][j - 1]); cout << small[n - 1][m - 1] << endl; return 0; }
#include <iostream> using namespace std; int main() { int a[1000], b[1000], n, m, i, j; cin >> n >> m; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < m; i++) cin >> b[i]; int small[1000][1000] = {a[0] * b[0]}; for (i = 1; i < n; i++) small[i][i] = small[i - 1][i - 1] + a[i] * b[i]; for (i = 1; i < m; i++) small[0][i] = min(a[0] * b[i], small[0][i - 1]); for (i = 1; i < n; i++) for (j = i + 1; j < m; j++) small[i][j] = min(small[i - 1][j - 1] + a[i] * b[j], small[i][j - 1]); cout << small[n - 1][m - 1] << endl; return 0; }
[["-", 12, 16, 31, 69, 341, 342, 0, 16, 31, 22], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 31, 22], ["-", 12, 16, 12, 16, 12, 69, 341, 342, 0, 22], ["+", 12, 16, 12, 16, 12, 69, 341, 342, 0, 22]]
1
275
#include <bits/stdc++.h> using namespace std; const int N = 1e3L + 11; int a[N], b[N], f[N][N]; void upd(int &x, int y) { if (x > y) x = y; } int main() { ios ::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) cin >> b[i]; memset(f, 0x3f, sizeof f); f[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { upd(f[i][j], f[i][j - 1]); upd(f[i][j], f[i - 1][j - 1] + a[i] * b[j]); } cout << *min_element(f[n], f[n] + m + 1) << '\n'; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3L + 11; int a[N], b[N], f[N][N]; void upd(int &x, int y) { if (x > y) x = y; } int main() { ios ::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) cin >> b[i]; memset(f, 0x3f, sizeof f); f[0][0] = 0; for (int i = 0; i <= n; i++) for (int j = 1; j <= m; j++) { upd(f[i][j], f[i][j - 1]); if (i) upd(f[i][j], f[i - 1][j - 1] + a[i] * b[j]); } cout << *min_element(f[n], f[n] + m + 1) << '\n'; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 0, 7, 8, 7, 8, 9, 0, 57, 0, 121], ["+", 8, 7, 8, 9, 0, 57, 15, 339, 0, 24], ["+", 8, 7, 8, 9, 0, 57, 15, 339, 51, 22], ["+", 8, 7, 8, 9, 0, 57, 15, 339, 0, 25]]
1
249
#include <bits/stdc++.h> #define INF (int)1e8 using namespace std; int main() { int n, m; cin >> n >> m; vector<int> d(n, 0); vector<int> c(m, 0); for (int i = 0; i < n; i++) { cin >> d[i]; } for (int i = 0; i < m; i++) { cin >> c[i]; } vector<int> prev(m, 0); vector<int> now(m, INF); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { now[j] = INF; } now[i] = d[i] * c[i] + i > 0 ? prev[i - 1] : 0; for (int j = i + 1; j < m; j++) { now[j] = min(prev[j - 1] + d[i] * c[j], now[j - 1]); // cerr << now[j] << " "; } // cerr << endl; swap(now, prev); } cout << prev[m - 1] << endl; return 0; }
#include <bits/stdc++.h> #define INF (int)1e8 using namespace std; int main() { int n, m; cin >> n >> m; vector<int> d(n, 0); vector<int> c(m, 0); for (int i = 0; i < n; i++) { cin >> d[i]; } for (int i = 0; i < m; i++) { cin >> c[i]; } vector<int> prev(m, 0); vector<int> now(m, INF); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { now[j] = INF; } now[i] = d[i] * c[i] + (i > 0 ? prev[i - 1] : 0); for (int j = i + 1; j < m; j++) { now[j] = min(prev[j - 1] + d[i] * c[j], now[j - 1]); // cerr << now[j] << " "; } // cerr << endl; swap(now, prev); } cout << prev[m - 1] << endl; return 0; }
[["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
1
256
n, m = gets.split.map(&:to_i) d = [] h = [] n.times{ d << gets.to_i } m.times{ h << gets.to_i } dp = Array.new(2){ Array.new(m, 1<<31) } table = Array.new(n){ Array.new(m) } n.times do |i| m.times do |j| table[i][j] = d[i] * h[j] end end m.times{ |i| dp[0][i] = table[0][i] } (0...n-1).each do |j| (j+1..m-n+j+1).each do |k| (k+1..m-n+j+1).each do |i| dp[j%2^1][i] = [dp[j%2^1][i], dp[j%2][k] + table[j+1][i]].min end end dp[j%2].fill(1<<31) end p dp[n%2^1][n-1..-1].min
n, m = gets.split.map(&:to_i) d = [] h = [] n.times{ d << gets.to_i } m.times{ h << gets.to_i } dp = Array.new(2){ Array.new(m, 1<<31) } table = Array.new(n){ Array.new(m) } n.times do |i| m.times do |j| table[i][j] = d[i] * h[j] end end m.times{ |i| dp[0][i] = table[0][i] } (0...n-1).each do |j| (j..m-n+j+1).each do |k| (k+1..m-n+j+1).each do |i| dp[j%2^1][i] = [dp[j%2^1][i], dp[j%2][k] + table[j+1][i]].min end end dp[j%2].fill(1<<31) end p dp[n%2^1][n-1..-1].min
[["-", 0, 652, 486, 739, 0, 475, 756, 738, 17, 72], ["-", 0, 652, 486, 739, 0, 475, 756, 738, 12, 612]]
4
266
N = int(input()) C = input() S = list(C) dp = [[0 for i in range(8)] for j in range(N + 1)] dp[0][1] = 1 for i in range(N): if C[i] == "J": dp[i + 1][1] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4]; dp[i + 1][2] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6]; dp[i + 1][3] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][6] + dp[i][7]; dp[i + 1][4] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][5] = 0; dp[i + 1][6] = 0; dp[i + 1][7] = 0; if C[i] == 'O': dp[i + 1][1] = 0; dp[i + 1][2] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6]; dp[i + 1][3] = 0; dp[i + 1][4] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][5] = dp[i][2] + dp[i][4] + dp[i][5] + dp[i][6]; dp[i + 1][6] = dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][7] = 0; if C[i] == "I": dp[i + 1][1] = 0; dp[i + 1][2] = 0; dp[i + 1][3] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][6] + dp[i][7]; dp[i + 1][4] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][5] = 0; dp[i + 1][6] = dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i - 1][7]; dp[i + 1][7] = dp[i][3] + dp[i][4] + dp[i][6] + dp[i][7]; print((dp[N][1] + dp[N][2] + dp[N][3] + dp[N][4] + dp[N][5] + dp[N][6] + dp[N][7]) % 10007)
N = int(input()) C = input() S = list(C) dp = [[0 for i in range(8)] for j in range(N + 1)] dp[0][1] = 1 for i in range(N): if C[i] == "J": dp[i + 1][1] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4]; dp[i + 1][2] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6]; dp[i + 1][3] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][6] + dp[i][7]; dp[i + 1][4] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][5] = 0; dp[i + 1][6] = 0; dp[i + 1][7] = 0; if C[i] == 'O': dp[i + 1][1] = 0; dp[i + 1][2] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6]; dp[i + 1][3] = 0; dp[i + 1][4] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][5] = dp[i][2] + dp[i][4] + dp[i][5] + dp[i][6]; dp[i + 1][6] = dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][7] = 0; if C[i] == 'I': dp[i + 1][1] = 0; dp[i + 1][2] = 0; dp[i + 1][3] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][6] + dp[i][7]; dp[i + 1][4] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][5] = 0; dp[i + 1][6] = dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; dp[i + 1][7] = dp[i][3] + dp[i][4] + dp[i][6] + dp[i][7]; print((dp[N][1] + dp[N][2] + dp[N][3] + dp[N][4] + dp[N][5] + dp[N][6] + dp[N][7]) % 10007)
[["-", 8, 196, 0, 57, 15, 666, 0, 557, 0, 654], ["+", 8, 196, 0, 57, 15, 666, 0, 557, 0, 654], ["-", 8, 196, 0, 57, 15, 666, 0, 557, 0, 655], ["+", 8, 196, 0, 57, 15, 666, 0, 557, 0, 655], ["-", 12, 657, 12, 206, 51, 206, 206, 657, 17, 33], ["-", 12, 657, 12, 206, 51, 206, 206, 657, 12, 612]]
5
933
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; #define F first #define S second const int INF = 1 << 31, MAX_N = 5000; typedef pair<int, int> P; void dijkstra(); void bfs(int s, int dis, int cost); int n, k; vector<int> road[MAX_N + 1]; int taxi[MAX_N + 1][2]; int reach[MAX_N + 1]; int main() { fill(reach + 2, reach + MAX_N + 1, INF); cin >> n >> k; for (int i = 1; i <= n; i++) { scanf("%d,%d", &taxi[i][0], &taxi[i][1]); } for (int i = 0; i < k; i++) { int a, b; scanf("%d,%d", &a, &b); road[a].push_back(b); road[b].push_back(a); } dijkstra(); cout << -reach[n] << endl; return 0; } priority_queue<P> pq; void dijkstra() { bfs(1, taxi[1][1], -taxi[1][0]); while (!pq.empty()) { P p = pq.top(); pq.pop(); if (reach[p.S] < p.F) { reach[p.S] = p.F; bfs(p.S, taxi[p.S][1], p.F - taxi[p.S][0]); } } } void bfs(int s, int dis, int cost) { queue<P> que; bool rea[5001] = {}; que.push(P(s, dis)); while (!que.empty()) { P p = que.front(); que.pop(); if (p.S) { for (int i = 0; i < road[p.F].size(); i++) { if (!rea[road[p.F][i]]) { rea[road[p.F][i]] = 1; que.push(P(road[p.F][i], p.S - 1)); if (reach[road[p.F][i]] < cost) { pq.push(P(cost, road[p.F][i])); } } } } } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; #define F first #define S second const int INF = 1 << 31, MAX_N = 5000; typedef pair<int, int> P; void dijkstra(); void bfs(int s, int dis, int cost); int n, k; vector<int> road[MAX_N + 1]; int taxi[MAX_N + 1][2]; int reach[MAX_N + 1]; int main() { fill(reach + 2, reach + MAX_N + 1, INF); cin >> n >> k; for (int i = 1; i <= n; i++) { scanf("%d%d", &taxi[i][0], &taxi[i][1]); } for (int i = 0; i < k; i++) { int a, b; scanf("%d%d", &a, &b); road[a].push_back(b); road[b].push_back(a); } dijkstra(); cout << -reach[n] << endl; return 0; } priority_queue<P> pq; void dijkstra() { bfs(1, taxi[1][1], -taxi[1][0]); while (!pq.empty()) { P p = pq.top(); pq.pop(); if (reach[p.S] < p.F) { reach[p.S] = p.F; bfs(p.S, taxi[p.S][1], p.F - taxi[p.S][0]); } } } void bfs(int s, int dis, int cost) { queue<P> que; bool rea[5001] = {}; que.push(P(s, dis)); while (!que.empty()) { P p = que.front(); que.pop(); if (p.S) { for (int i = 0; i < road[p.F].size(); i++) { if (!rea[road[p.F][i]]) { rea[road[p.F][i]] = 1; que.push(P(road[p.F][i], p.S - 1)); if (reach[road[p.F][i]] < cost) { pq.push(P(cost, road[p.F][i])); } } } } } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
550
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <queue> #include <string.h> #include <vector> using namespace std; typedef pair<int, int> P; static const int INF = 2e9; struct Edge { int to; int cost; }; vector<Edge> G1[5001]; vector<Edge> G2[5001]; int d[5001]; bool isUsed[5001]; void solve() { int N, K; cin >> N >> K; vector<int> C(N); vector<int> R(N); for (int i = 0; i < N; ++i) { scanf("%d %d", C[i], R[i]); // cin >> C[i] >> R[i]; } for (int i = 0; i < K; ++i) { int from, to; Edge e1, e2; cin >> from >> to; --from; --to; e1.to = to; e1.cost = 1; e2.to = from; e2.cost = 1; G1[from].push_back(e1); G1[to].push_back(e2); } for (int i = 0; i < N; ++i) { memset(isUsed, 0, sizeof(isUsed)); queue<P> que; que.push(P(i, R[i])); isUsed[i] = true; while (!que.empty()) { P p = que.front(); que.pop(); int v = p.first; for (int j = 0; j < G1[v].size(); ++j) { int to = G1[v][j].to; if (!isUsed[to] && p.second >= 1) { isUsed[to] = true; que.push(P(to, p.second - 1)); Edge e; e.to = to; e.cost = C[i]; G2[i].push_back(e); } } } } for (int i = 0; i < N; ++i) { d[i] = INF; } priority_queue<P, vector<P>, greater<P>> pque; pque.push(P(0, 0)); d[0] = 0; while (!pque.empty()) { P p = pque.top(); pque.pop(); int v = p.first; for (int i = 0; i < G2[v].size(); ++i) { Edge e = G2[v][i]; if (d[e.to] > p.second + e.cost) { d[e.to] = p.second + e.cost; pque.push(P(e.to, d[e.to])); } } } cout << d[N - 1] << endl; } int main() { solve(); return (0); }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <queue> #include <string.h> #include <vector> using namespace std; typedef pair<int, int> P; static const int INF = 2e9; struct Edge { int to; int cost; }; vector<Edge> G1[5001]; vector<Edge> G2[5001]; int d[5001]; bool isUsed[5001]; void solve() { int N, K; cin >> N >> K; vector<int> C(N); vector<int> R(N); for (int i = 0; i < N; ++i) { scanf("%d %d", &C[i], &R[i]); // cin >> C[i] >> R[i]; } for (int i = 0; i < K; ++i) { int from, to; Edge e1, e2; cin >> from >> to; --from; --to; e1.to = to; e1.cost = 1; e2.to = from; e2.cost = 1; G1[from].push_back(e1); G1[to].push_back(e2); } for (int i = 0; i < N; ++i) { memset(isUsed, 0, sizeof(isUsed)); queue<P> que; que.push(P(i, R[i])); isUsed[i] = true; while (!que.empty()) { P p = que.front(); que.pop(); int v = p.first; for (int j = 0; j < G1[v].size(); ++j) { int to = G1[v][j].to; if (!isUsed[to] && p.second >= 1) { isUsed[to] = true; que.push(P(to, p.second - 1)); Edge e; e.to = to; e.cost = C[i]; G2[i].push_back(e); } } } } for (int i = 0; i < N; ++i) { d[i] = INF; } priority_queue<P, vector<P>, greater<P>> pque; pque.push(P(0, 0)); d[0] = 0; while (!pque.empty()) { P p = pque.top(); pque.pop(); int v = p.first; for (int i = 0; i < G2[v].size(); ++i) { Edge e = G2[v][i]; if (d[e.to] > p.second + e.cost) { d[e.to] = p.second + e.cost; pque.push(P(e.to, d[e.to])); } } } cout << d[N - 1] << endl; } int main() { solve(); return (0); }
[["+", 0, 1, 0, 2, 3, 4, 0, 66, 17, 67]]
1
622
#include <bits/stdc++.h> using namespace std; #define INF 0x3fffffff int N; int M; int C[114514]; int R[114514]; int D[5145][5145]; int mind[5145]; bool used[5145]; vector<int> es[114514]; void Bfs(int s) { queue<int> q; fill(mind, mind + N, INF); q.push(s); mind[s] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int u : es[v]) { if (mind[u] != INF) continue; mind[u] = mind[v] + 1; q.push(u); } } for (int i = 0; i < N; i++) { if (mind[i] <= R[i]) { D[s][i] = min(D[s][i], C[s]); } } } int Dijkstra(int s, int t) { fill(mind, mind + N, INF); mind[s] = 0; for (int i = 0; i < N; i++) { int v = -1; for (int j = 0; j < N; j++) { if (used[j]) continue; if (v == -1 || mind[v] > mind[j]) v = j; } if (v == -1) break; used[v] = true; for (int u = 0; u < N; u++) { if (mind[u] > mind[v] + D[v][u]) { mind[u] = mind[v] + D[v][u]; } } } return mind[t]; } int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; ++i) { scanf("%d%d", &C[i], &R[i]); for (int j = 0; j < N; j++) { if (i == j) continue; D[i][j] = INF; } } for (int i = 0; i < M; ++i) { int a, b; scanf("%d%d", &a, &b); --a; --b; es[a].emplace_back(b); es[b].emplace_back(a); } for (int i = 0; i < N; i++) { Bfs(i); } printf("%d\n", Dijkstra(0, N - 1)); }
#include <bits/stdc++.h> using namespace std; #define INF 0x3fffffff int N; int M; int C[114514]; int R[114514]; int D[5145][5145]; int mind[5145]; bool used[5145]; vector<int> es[114514]; void Bfs(int s) { queue<int> q; fill(mind, mind + N, INF); q.push(s); mind[s] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int u : es[v]) { if (mind[u] != INF) continue; mind[u] = mind[v] + 1; q.push(u); } } for (int i = 0; i < N; i++) { if (mind[i] <= R[s]) { D[s][i] = min(D[s][i], C[s]); } } } int Dijkstra(int s, int t) { fill(mind, mind + N, INF); mind[s] = 0; for (int i = 0; i < N; i++) { int v = -1; for (int j = 0; j < N; j++) { if (used[j]) continue; if (v == -1 || mind[v] > mind[j]) v = j; } if (v == -1) break; used[v] = true; for (int u = 0; u < N; u++) { if (mind[u] > mind[v] + D[v][u]) { mind[u] = mind[v] + D[v][u]; } } } return mind[t]; } int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; ++i) { scanf("%d%d", &C[i], &R[i]); for (int j = 0; j < N; ++j) { if (i == j) continue; D[i][j] = INF; } } for (int i = 0; i < M; ++i) { int a, b; scanf("%d%d", &a, &b); --a; --b; es[a].emplace_back(b); es[b].emplace_back(a); } for (int i = 0; i < N; i++) { Bfs(i); } printf("%d\n", Dijkstra(0, N - 1)); }
[["-", 15, 339, 51, 16, 12, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 12, 69, 341, 342, 0, 22], ["-", 0, 7, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 7, 8, 9, 0, 7, 26, 27, 28, 22]]
1
576
from collections import deque import heapq def dijkstra(s,g,m): color=[0]*n dis=[float('inf')]*n dis[s]=0 heapq.heappush(pq,[0,s]) while len(pq)!=0: t,u=heapq.heappop(pq) color[u]=2 if dis[u]<t: continue for v in g[u]: if color[v]!=2: if dis[u]+m[u][v]<dis[v]: dis[v]=dis[u]+m[u][v] color[v]=1 heapq.heappush(pq,[dis[v],v]) return dis n,k=list(map(int,input().split())) cr=[list(map(int,input().split())) for _ in range(n)] g=[[]*n for _ in range(n)] for i in range(k): a,b=list(map(int,input().split())) g[a-1].append(b-1) g[b-1].append(a-1) print(g) d=[[0]*n for _ in range(n)] for i in range(n): q=deque([i]) visited=[0]*n visited[i]=1 while len(q)!=0: p=q.popleft() for j in g[p]: if visited[j]==0: visited[j]=1 q.append(j) d[i][j]=d[i][p]+1 cost=[[float('inf')]*n for _ in range(n)] newg=[[]*n for _ in range(n)] for i in range(n): for j in range(n): if d[i][j]<=cr[i][1]: cost[i][j]=cr[i][0] newg[i].append(j) pq=[] dis=dijkstra(0,newg,cost) print((dis[n-1]))
from collections import deque import heapq def dijkstra(s,g,m): color=[0]*n dis=[float('inf')]*n dis[s]=0 heapq.heappush(pq,[0,s]) while len(pq)!=0: t,u=heapq.heappop(pq) color[u]=2 if dis[u]<t: continue for v in g[u]: if color[v]!=2: if dis[u]+m[u][v]<dis[v]: dis[v]=dis[u]+m[u][v] color[v]=1 heapq.heappush(pq,[dis[v],v]) return dis n,k=list(map(int,input().split())) cr=[list(map(int,input().split())) for _ in range(n)] g=[[]*n for _ in range(n)] for i in range(k): a,b=list(map(int,input().split())) g[a-1].append(b-1) g[b-1].append(a-1) d=[[0]*n for _ in range(n)] for i in range(n): q=deque([i]) visited=[0]*n visited[i]=1 while len(q)!=0: p=q.popleft() for j in g[p]: if visited[j]==0: visited[j]=1 q.append(j) d[i][j]=d[i][p]+1 cost=[[float('inf')]*n for _ in range(n)] newg=[[]*n for _ in range(n)] for i in range(n): for j in range(n): if d[i][j]<=cr[i][1]: cost[i][j]=cr[i][0] newg[i].append(j) pq=[] dis=dijkstra(0,newg,cost) print((dis[n-1]))
[["-", 36, 36, 0, 656, 0, 1, 0, 652, 63, 22], ["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 25]]
5
501
from heapq import heappop as pop from heapq import heappush as push INF = 10 ** 20 n, k = map(int, input().split()) clst = [] rlst = [] for i in range(n): c, r = map(int, input().split()) clst.append(c) rlst.append(r) edges = [[0] * n for i in range(n)] for i in range(k): a, b = map(int, input().split()) a -= 1 b -= 1 edges[a][b] = edges[b][a] = 1 costs = [INF for i in range(n)] costs[0] = 0 used = [False for i in range(n)] def make_to_lst(s_num): loop = rlst[s_num] temp = {i for i in range(n) if edges[s_num][i]} ret = {i for i in temp} while loop: new = set() for p in temp: pto = {i for i in range(n) if edges[p][i]} new = new | pto ret = ret | temp temp = new - ret if not temp: break loop -= 1 return ret used[0] = True costs[0] = 0 break_flag = 0 que = [(clst[0], 0)] while que and not break_flag: next_cost, s_num = pop(que) to_lst = make_to_lst(s_num) for num in to_lst: costs[num] = next_cost if num == n - 1: break_flag = 1 break if not used[num]: push(que, (costs[num] + clst[num], num)) used[num] = True print(costs[n - 1]) print(costs)
from heapq import heappop as pop from heapq import heappush as push INF = 10 ** 20 n, k = map(int, input().split()) clst = [] rlst = [] for i in range(n): c, r = map(int, input().split()) clst.append(c) rlst.append(r) edges = [[0] * n for i in range(n)] for i in range(k): a, b = map(int, input().split()) a -= 1 b -= 1 edges[a][b] = edges[b][a] = 1 costs = [INF for i in range(n)] costs[0] = 0 used = [False for i in range(n)] def make_to_lst(s_num): loop = rlst[s_num] temp = {i for i in range(n) if edges[s_num][i]} ret = {i for i in temp} while loop: new = set() for p in temp: pto = {i for i in range(n) if edges[p][i]} new = new | pto ret = ret | temp temp = new - ret if not temp: break loop -= 1 return ret used[0] = True costs[0] = 0 break_flag = 0 que = [(clst[0], 0)] while que and not break_flag: next_cost, s_num = pop(que) to_lst = make_to_lst(s_num) for num in to_lst: costs[num] = next_cost if num == n - 1: break_flag = 1 break if not used[num]: push(que, (costs[num] + clst[num], num)) used[num] = True print(costs[n - 1])
[["-", 36, 36, 0, 656, 0, 1, 0, 652, 63, 22], ["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 0, 656, 0, 1, 0, 652, 3, 4, 0, 25]]
5
382
#include <algorithm> #include <iostream> using namespace std; int n, m; char c[1080][1080]; int cnt[1080][1080]; char ans[2][2]; int anss, res; int main() { cin >> m >> n; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) cin >> c[i][j]; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) cin >> ans[i][j]; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { cnt[i][j] = 1; for (int k = 0; k < 2; k++) for (int l = 0; l < 2; l++) if (c[i + k][j + l] != ans[k][l]) cnt[i][j] = 0; res += cnt[i][j]; } int anss = res; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { int last = c[i][j]; int tres; c[i][j] = 'J'; tres = res; for (int k = i - 1; k <= i; k++) for (int l = j - 1; l <= j; l++) { int tmp = 1; for (int o = 0; o < 2; o++) for (int p = 0; p > 2; p++) if (c[k + o][l + p] != ans[o][p]) tmp = 0; tres += tmp - cnt[k][l]; } anss = max(anss, tres); c[i][j] = 'O'; tres = res; for (int k = i - 1; k <= i; k++) for (int l = j - 1; l <= j; l++) { int tmp = 1; for (int o = 0; o < 2; o++) for (int p = 0; p > 2; p++) if (c[k + o][l + p] != ans[o][p]) tmp = 0; tres += tmp - cnt[k][l]; } anss = max(anss, tres); c[i][j] = 'I'; tres = res; for (int k = i - 1; k <= i; k++) for (int l = j - 1; l <= j; l++) { int tmp = 1; for (int o = 0; o < 2; o++) for (int p = 0; p > 2; p++) if (c[k + o][l + p] != ans[o][p]) tmp = 0; tres += tmp - cnt[k][l]; } anss = max(anss, tres); c[i][j] = last; } } cout << anss << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, m; char c[1080][1080]; int cnt[1080][1080]; char ans[2][2]; int anss, res; int main() { cin >> m >> n; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) cin >> c[i][j]; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) cin >> ans[i][j]; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { cnt[i][j] = 1; for (int k = 0; k < 2; k++) for (int l = 0; l < 2; l++) if (c[i + k][j + l] != ans[k][l]) cnt[i][j] = 0; res += cnt[i][j]; } int anss = res; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { int last = c[i][j]; int tres; c[i][j] = 'J'; tres = res; for (int k = i - 1; k <= i; k++) for (int l = j - 1; l <= j; l++) { int tmp = 1; for (int o = 0; o < 2; o++) for (int p = 0; p < 2; p++) if (c[k + o][l + p] != ans[o][p]) tmp = 0; tres += tmp - cnt[k][l]; } anss = max(anss, tres); c[i][j] = 'O'; tres = res; for (int k = i - 1; k <= i; k++) for (int l = j - 1; l <= j; l++) { int tmp = 1; for (int o = 0; o < 2; o++) for (int p = 0; p < 2; p++) if (c[k + o][l + p] != ans[o][p]) tmp = 0; tres += tmp - cnt[k][l]; } anss = max(anss, tres); c[i][j] = 'I'; tres = res; for (int k = i - 1; k <= i; k++) for (int l = j - 1; l <= j; l++) { int tmp = 1; for (int o = 0; o < 2; o++) for (int p = 0; p < 2; p++) if (c[k + o][l + p] != ans[o][p]) tmp = 0; tres += tmp - cnt[k][l]; } anss = max(anss, tres); c[i][j] = last; } } cout << anss << endl; return 0; }
[["-", 8, 9, 0, 7, 8, 7, 15, 16, 17, 47], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 17, 18]]
1
703
#include <bits/stdc++.h> using namespace std; #define int long long #define PB push_back #define PPB pop_back typedef pair<int, int> pii; static const int INF = 1LL << 61; int grid[1005][1005], m, n, mo[2][2], dx[] = {0, 1, 1, 0}, dy[] = {0, 0, 1, 1}; inline int get(char t) { switch (t) { case 'J': return 1; case 'O': return 2; case 'I': return 3; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> m >> n; for (int y = 1; y <= m; ++y) { string t; cin >> t; for (int x = 1; x <= n; ++x) { grid[x][y] = get(t[x - 1]); } } for (int y = 0; y < 2; ++y) { string t; cin >> t; for (int x = 0; x < 2; ++x) { mo[x][y] = get(t[x]); } } int base = 0, addi = 0; for (int y = 1; y <= m; ++y) { for (int x = 1; x <= n; ++x) { { bool flg = true; for (int j = 0; j < 4; ++j) { flg = flg && grid[x + dx[j]][y + dy[j]] == mo[dx[j]][dy[j]]; if (!flg) break; } if (flg) base++; } int pre = grid[x][y], tmax = 0; for (int i = 1; i <= 3; ++i) { if (pre == i) continue; grid[x][y] = i; int temp = 0; for (int j = 0; j < 4; ++j) { bool flg = true; for (int k = 0; k < 4; ++k) { flg = flg && grid[x + dx[k] - dx[j]][y + dy[k] - dy[j]] == mo[1 + dx[k]][1 + dy[k]]; if (!flg) break; } if (!flg) continue; temp++; } tmax = max(tmax, temp); } grid[x][y] = pre; addi = max(tmax, addi); } } cout << base + addi << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define PB push_back #define PPB pop_back typedef pair<int, int> pii; static const int INF = 1LL << 61; int grid[1005][1005], m, n, mo[2][2], dx[] = {0, 1, 1, 0}, dy[] = {0, 0, 1, 1}; inline int get(char t) { switch (t) { case 'J': return 1; case 'O': return 2; case 'I': return 3; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> m >> n; for (int y = 1; y <= m; ++y) { string t; cin >> t; for (int x = 1; x <= n; ++x) { grid[x][y] = get(t[x - 1]); } } for (int y = 0; y < 2; ++y) { string t; cin >> t; for (int x = 0; x < 2; ++x) { mo[x][y] = get(t[x]); } } int base = 0, addi = 0; for (int y = 1; y <= m; ++y) { for (int x = 1; x <= n; ++x) { { bool flg = true; for (int j = 0; j < 4; ++j) { flg = flg && grid[x + dx[j]][y + dy[j]] == mo[dx[j]][dy[j]]; if (!flg) break; } if (flg) base++; } int pre = grid[x][y], tmax = 0; for (int i = 1; i <= 3; ++i) { if (pre == i) continue; grid[x][y] = i; int temp = 0; for (int j = 0; j < 4; ++j) { bool flg = true; for (int k = 0; k < 4; ++k) { flg = flg && grid[x + dx[k] - dx[j]][y + dy[k] - dy[j]] == mo[dx[k]][dy[k]]; if (!flg) break; } if (!flg) continue; temp++; } tmax = max(tmax, temp); } grid[x][y] = pre; addi = max(tmax, addi); } } cout << base + addi << endl; return 0; }
[["-", 12, 69, 28, 69, 341, 342, 0, 16, 31, 13], ["-", 12, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["-", 12, 16, 12, 69, 341, 342, 0, 16, 31, 13], ["-", 12, 16, 12, 69, 341, 342, 0, 16, 17, 72]]
1
570
#include <bits/stdc++.h> using namespace std; char mp[1000][1000]; char e[2][2]; int dx[4] = {-1, 0, -1, 0}, dy[4] = {-1, -1, 0, 0}; char a[3] = {'J', 'O', 'I'}; int main() { int h, w; scanf("%d %d", &h, &w); for (int i = 0; i < h; ++i) scanf("%s", mp[i]); for (int i = 0; i < 2; ++i) scanf("%s", mp[i]); int sum = 0; for (int i = 0; i < h - 1; ++i) { for (int j = 0; j < w - 1; ++j) { if (mp[i][j] == e[0][0] && mp[i][j + 1] == e[0][1] && mp[i + 1][j] == e[1][0] && mp[i + 1][j + 1] == e[1][1]) { sum++; } } } int ans = sum; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { int sum2 = sum; char tmp = mp[i][j]; int cnt = 0; for (int k = 0; k < 4; ++k) { int y = i + dy[k], x = j + dx[k]; if (y < 0 || x < 0 || y + 1 >= h || x + 1 >= w) continue; if (mp[y][x] == e[0][0] && mp[y][x + 1] == e[0][1] && mp[y + 1][x] == e[1][0] && mp[y + 1][x + 1] == e[1][1]) { cnt++; } } sum2 -= cnt; for (int k = 0; k < 3; ++k) { mp[i][j] = a[k]; int cnt2 = 0; for (int l = 0; l < 4; ++l) { int y = i + dy[l], x = j + dx[l]; if (y < 0 || x < 0 || y + 1 >= h || x + 1 >= w) continue; if (mp[y][x] == e[0][0] && mp[y][x + 1] == e[0][1] && mp[y + 1][x] == e[1][0] && mp[y + 1][x + 1] == e[1][1]) { cnt2++; } } cnt = max(cnt, cnt2); } sum2 += cnt; mp[i][j] = tmp; ans = max(ans, sum2); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; char mp[1000][1000]; char e[2][2]; int dx[4] = {-1, 0, -1, 0}, dy[4] = {-1, -1, 0, 0}; char a[3] = {'J', 'O', 'I'}; int main() { int h, w; scanf("%d %d", &h, &w); for (int i = 0; i < h; ++i) scanf("%s", mp[i]); for (int i = 0; i < 2; ++i) scanf("%s", e[i]); int sum = 0; for (int i = 0; i < h - 1; ++i) { for (int j = 0; j < w - 1; ++j) { if (mp[i][j] == e[0][0] && mp[i][j + 1] == e[0][1] && mp[i + 1][j] == e[1][0] && mp[i + 1][j + 1] == e[1][1]) { sum++; } } } int ans = sum; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { int sum2 = sum; char tmp = mp[i][j]; int cnt = 0; for (int k = 0; k < 4; ++k) { int y = i + dy[k], x = j + dx[k]; if (y < 0 || x < 0 || y + 1 >= h || x + 1 >= w) continue; if (mp[y][x] == e[0][0] && mp[y][x + 1] == e[0][1] && mp[y + 1][x] == e[1][0] && mp[y + 1][x + 1] == e[1][1]) { cnt++; } } sum2 -= cnt; for (int k = 0; k < 3; ++k) { mp[i][j] = a[k]; int cnt2 = 0; for (int l = 0; l < 4; ++l) { int y = i + dy[l], x = j + dx[l]; if (y < 0 || x < 0 || y + 1 >= h || x + 1 >= w) continue; if (mp[y][x] == e[0][0] && mp[y][x + 1] == e[0][1] && mp[y + 1][x] == e[1][0] && mp[y + 1][x + 1] == e[1][1]) { cnt2++; } } cnt = max(cnt, cnt2); } sum2 += cnt; mp[i][j] = tmp; ans = max(ans, sum2); } } cout << ans << endl; }
[["-", 8, 1, 0, 2, 3, 4, 0, 69, 28, 22], ["+", 8, 1, 0, 2, 3, 4, 0, 69, 28, 22]]
1
681
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int H, W; int ans = 0, maxans = 0; string fie[1002], ansfie[2]; cin >> H >> W; for (int i = 0; i < H; i++) cin >> fie[i]; for (int i = 0; i < 2; i++) cin >> ansfie[i]; for (int i = 0; i < H - 1; i++) { for (int j = 0; j < W - 1; j++) { if (fie[i][j] == ansfie[0][0] && fie[i][j + 1] == ansfie[0][1] && fie[i + 1][j] == ansfie[1][0] && fie[i + 1][j + 1] == ansfie[1][1]) ans++; } } cout << ans << endl; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < 3; k++) { int a = 0, b = 0; char p; if (k == 0) p = 'J'; if (k == 1) p = 'O'; if (k == 2) p = 'I'; if (i != H - 1 && j != W - 1) { if (fie[i][j] == ansfie[0][0] && fie[i][j + 1] == ansfie[0][1] && fie[i + 1][j] == ansfie[1][0] && fie[i + 1][j + 1] == ansfie[1][1]) a++; if (p == ansfie[0][0] && fie[i][j + 1] == ansfie[0][1] && fie[i + 1][j] == ansfie[1][0] && fie[i + 1][j + 1] == ansfie[1][1]) b++; } if (i != 0 && j != W - 1) { if (fie[i - 1][j] == ansfie[0][0] && fie[i - 1][j + 1] == ansfie[0][1] && fie[i][j] == ansfie[1][0] && fie[i][j + 1] == ansfie[1][1]) a++; if (fie[i - 1][j] == ansfie[0][0] && fie[i - 1][j + 1] == ansfie[0][1] && p == ansfie[1][0] && fie[i][j + 1] == ansfie[1][1]) b++; } if (i != H - 1 && j != 0) { if (fie[i][j - 1] == ansfie[0][0] && fie[i][j] == ansfie[0][1] && fie[i + 1][j - 1] == ansfie[1][0] && fie[i + 1][j] == ansfie[1][1]) a++; if (fie[i][j - 1] == ansfie[0][0] && p == ansfie[0][1] && fie[i + 1][j - 1] == ansfie[1][0] && fie[i + 1][j] == ansfie[1][1]) b++; } if (i != 0 && j != 0) { if (fie[i - 1][j - 1] == ansfie[0][0] && fie[i - 1][j] == ansfie[0][1] && fie[i][j - 1] == ansfie[1][0] && fie[i][j] == ansfie[1][1]) a++; if (fie[i - 1][j - 1] == ansfie[0][0] && fie[i - 1][j] == ansfie[0][1] && fie[i][j - 1] == ansfie[1][0] && p == ansfie[1][1]) b++; } maxans = max(maxans, ans + b - a); } } } cout << maxans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { int H, W; int ans = 0, maxans = 0; string fie[1002], ansfie[2]; cin >> H >> W; for (int i = 0; i < H; i++) cin >> fie[i]; for (int i = 0; i < 2; i++) cin >> ansfie[i]; for (int i = 0; i < H - 1; i++) { for (int j = 0; j < W - 1; j++) { if (fie[i][j] == ansfie[0][0] && fie[i][j + 1] == ansfie[0][1] && fie[i + 1][j] == ansfie[1][0] && fie[i + 1][j + 1] == ansfie[1][1]) ans++; } } maxans = ans; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < 3; k++) { int a = 0, b = 0; char p; if (k == 0) p = 'J'; if (k == 1) p = 'O'; if (k == 2) p = 'I'; if (i != H - 1 && j != W - 1) { if (fie[i][j] == ansfie[0][0] && fie[i][j + 1] == ansfie[0][1] && fie[i + 1][j] == ansfie[1][0] && fie[i + 1][j + 1] == ansfie[1][1]) a++; if (p == ansfie[0][0] && fie[i][j + 1] == ansfie[0][1] && fie[i + 1][j] == ansfie[1][0] && fie[i + 1][j + 1] == ansfie[1][1]) b++; } if (i != 0 && j != W - 1) { if (fie[i - 1][j] == ansfie[0][0] && fie[i - 1][j + 1] == ansfie[0][1] && fie[i][j] == ansfie[1][0] && fie[i][j + 1] == ansfie[1][1]) a++; if (fie[i - 1][j] == ansfie[0][0] && fie[i - 1][j + 1] == ansfie[0][1] && p == ansfie[1][0] && fie[i][j + 1] == ansfie[1][1]) b++; } if (i != H - 1 && j != 0) { if (fie[i][j - 1] == ansfie[0][0] && fie[i][j] == ansfie[0][1] && fie[i + 1][j - 1] == ansfie[1][0] && fie[i + 1][j] == ansfie[1][1]) a++; if (fie[i][j - 1] == ansfie[0][0] && p == ansfie[0][1] && fie[i + 1][j - 1] == ansfie[1][0] && fie[i + 1][j] == ansfie[1][1]) b++; } if (i != 0 && j != 0) { if (fie[i - 1][j - 1] == ansfie[0][0] && fie[i - 1][j] == ansfie[0][1] && fie[i][j - 1] == ansfie[1][0] && fie[i][j] == ansfie[1][1]) a++; if (fie[i - 1][j - 1] == ansfie[0][0] && fie[i - 1][j] == ansfie[0][1] && fie[i][j - 1] == ansfie[1][0] && p == ansfie[1][1]) b++; } maxans = max(maxans, ans + b - a); } } } cout << maxans << endl; return 0; }
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 31, 22], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 14, 8, 9, 0, 1, 0, 16, 17, 151], ["-", 0, 14, 8, 9, 0, 1, 0, 16, 12, 22]]
1
976
#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 n, m; char flag[1002][1002]; char coa[3][3]; //?´???? Coat of arms int over[1002][1002]; // ????????? bool check(char y0x0, char y0x1, char y1x0, char y1x1) { if (y0x0 == coa[0][0]) { if (y0x1 != coa[0][1]) { return false; } if (y1x0 != coa[1][0]) { return false; } if (y1x1 != coa[1][1]) { return false; } return true; } return false; } void Over(int y, int x) { for (int i = y - 1; i < y + 1; ++i) { for (int i2 = x - 1; i2 < x + 1; ++i2) { ++over[i][i2]; } } } int main() { memset(over, 0, sizeof(over)); memset(flag, '0', sizeof(flag)); cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int i2 = 1; i2 <= m; ++i2) { cin >> flag[i][i2]; } } for (int i = 0; i < 2; ++i) { for (int i2 = 0; i2 < 2; ++i2) { cin >> coa[i][i2]; } } int cnt = 0; //???????????§?????¢?´¢ for (int i = 1; i <= n - 1; ++i) { for (int i2 = 1; i2 <= m - 1; ++i2) { if (check(flag[i][i2], flag[i][i2 + 1], flag[i + 1][i2], flag[i + 1][i2 + 1])) { ++cnt; Over(i, i2); } } } //???????????????J,O,I,????????´???????????? //(i, i2)????????????(i - 1, i2 - 1)???(i + 1, i2 + 1)??????????????°?????? //??????????¢???????????????? ??? ??????(i, //i2)?????????????´??????¨???????????£??????????????????????????????????????°??? int cnt_add = 0; char JOI[] = {"JOI"}; for (int i = 1; i <= n; ++i) { for (int i2 = 1; i2 <= m; ++i2) { for (int j = 0; j < 3; ++j) { int add = 0; if (check(flag[i - 1][i2 - 1], flag[i - 1][i2], flag[i][i2 - 1], JOI[j])) { ++add; } if (check(flag[i - 1][i2], flag[i - 1][i2 + 1], JOI[j], flag[i][i2 + 1])) { ++add; } if (check(flag[i][i2 - 1], JOI[j], flag[i + 1][i2 - 1], flag[i + 1][i2])) { ++add; } if (check(JOI[j], flag[i][i2 + 1], flag[i + 1][i2], flag[i + 1][i2 + 1])) { ++add; } cnt_add = max(cnt_add, add - over[i][i2]); } } } cout << max(cnt, cnt + cnt_add) << endl; return 0; }
#include <iostream> using namespace std; #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <queue> #include <stack> #include <string> #include <vector> #define INF 10 << 10 int n, m; char flag[1002][1002]; char coa[3][3]; //?´???? Coat of arms int over[1002][1002]; // ????????? bool check(char y0x0, char y0x1, char y1x0, char y1x1) { if (y0x0 == coa[0][0]) { if (y0x1 != coa[0][1]) { return false; } if (y1x0 != coa[1][0]) { return false; } if (y1x1 != coa[1][1]) { return false; } return true; } return false; } void Over(int y, int x) { for (int i = y; i <= y + 1; ++i) { for (int i2 = x; i2 <= x + 1; ++i2) { ++over[i][i2]; } } } int main() { memset(over, 0, sizeof(over)); memset(flag, '0', sizeof(flag)); cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int i2 = 1; i2 <= m; ++i2) { cin >> flag[i][i2]; } } for (int i = 0; i < 2; ++i) { for (int i2 = 0; i2 < 2; ++i2) { cin >> coa[i][i2]; } } int cnt = 0; //???????????§?????¢?´¢ for (int i = 1; i <= n - 1; ++i) { for (int i2 = 1; i2 <= m - 1; ++i2) { if (check(flag[i][i2], flag[i][i2 + 1], flag[i + 1][i2], flag[i + 1][i2 + 1])) { ++cnt; Over(i, i2); } } } //???????????????J,O,I,????????´???????????? //(i, i2)????????????(i - 1, i2 - 1)???(i + 1, i2 + 1)??????????????°?????? //??????????¢???????????????? ??? ??????(i, //i2)?????????????´??????¨???????????£??????????????????????????????????????°??? int cnt_add = 0; char JOI[] = {"JOI"}; for (int i = 1; i <= n; ++i) { for (int i2 = 1; i2 <= m; ++i2) { for (int j = 0; j < 3; ++j) { int add = 0; if (check(flag[i - 1][i2 - 1], flag[i - 1][i2], flag[i][i2 - 1], JOI[j])) { ++add; } if (check(flag[i - 1][i2], flag[i - 1][i2 + 1], JOI[j], flag[i][i2 + 1])) { ++add; } if (check(flag[i][i2 - 1], JOI[j], flag[i + 1][i2 - 1], flag[i + 1][i2])) { ++add; } if (check(JOI[j], flag[i][i2 + 1], flag[i + 1][i2], flag[i + 1][i2 + 1])) { ++add; } cnt_add = max(cnt_add, add - over[i][i2]); } } } cout << max(cnt, cnt + cnt_add) << endl; return 0; }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 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, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
725
#include <bits/stdc++.h> using namespace std; const int N = 1e3L + 11; char c[N][N]; char a[2][2]; bool f(int i, int j) { for (int s : {0, 1}) for (int t : {0, 1}) if (c[i + s][j + t] != a[s][t]) return 0; return 1; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1); scanf("%s", a[0]); scanf("%s", a[1]); int all = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) all += f(i, j); int ans = all; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { char old = a[i][j]; for (char k : {'J', 'O', 'I'}) { int tmp = all - f(i - 1, j - 1) - f(i, j - 1) - f(i - 1, j) - f(i, j); c[i][j] = k; ans = max(ans, tmp + f(i - 1, j - 1) + f(i, j - 1) + f(i - 1, j) + f(i, j)); c[i][j] = old; } } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3L + 11; char c[N][N]; char a[2][2]; bool f(int i, int j) { for (int s : {0, 1}) for (int t : {0, 1}) if (c[i + s][j + t] != a[s][t]) return 0; return 1; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1); scanf("%s", a[0]); scanf("%s", a[1]); int all = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) all += f(i, j); int ans = all; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { char old = c[i][j]; for (char k : {'J', 'O', 'I'}) { int tmp = all - f(i - 1, j - 1) - f(i, j - 1) - f(i - 1, j) - f(i, j); c[i][j] = k; ans = max(ans, tmp + f(i - 1, j - 1) + f(i, j - 1) + f(i - 1, j) + f(i, j)); c[i][j] = old; } } cout << ans << '\n'; }
[["-", 0, 43, 49, 50, 51, 69, 28, 69, 28, 22], ["+", 0, 43, 49, 50, 51, 69, 28, 69, 28, 22]]
1
392
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; string s[1001], t[3]; int h, w, sum, ans = 0; int dx[] = {0, 0, -1, -1}; int dy[] = {0, -1, 0, -1}; int gx[] = {0, 0, 1, 1}; int gy[] = {0, 1, 0, 1}; char c[] = {'J', 'O', 'I'}; bool used[1001][1001]; void get_sum() { r(i, h - 1) r(j, w - 1) { if (s[i][j] == t[0][0] && s[i + 1][j] == t[1][0] & s[i][j + 1] == t[0][1] && s[i + 1][j + 1] == t[1][1]) { used[i][j] = 1; sum++; } } } int get_res(int y, int x) { int res = sum; r(i, 4) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy < 0 || xx < 0 || yy >= h || xx >= w) continue; if (used[yy][xx]) ans--; } r(i, 4) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy < 0 || xx < 0 || yy >= h || xx >= w) continue; r(k, 4) { int sy = yy + gy[k]; int sx = xx + gx[k]; if (sy < 0 || sx < 0 || sy >= h || sx >= w) continue; if (s[sy][sx] != t[gy[k]][gx[k]]) break; if (k == 3) res++; } } return res; } int get_ans(int y, int x) { char tmp = s[y][x]; int res = 0; r(i, 3) { if (s[y][x] != c[i]) { s[y][x] = c[i]; res = max(res, get_res(y, x)); s[y][x] = tmp; } } return res; } int main() { cin >> h >> w; r(i, h) cin >> s[i]; r(i, 2) cin >> t[i]; get_sum(); ans = sum; r(i, h) r(j, w) { ans = max(ans, get_ans(i, j)); } cout << ans << endl; }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; string s[1001], t[3]; int h, w, sum, ans = 0; int dx[] = {0, 0, -1, -1}; int dy[] = {0, -1, 0, -1}; int gx[] = {0, 0, 1, 1}; int gy[] = {0, 1, 0, 1}; char c[] = {'J', 'O', 'I'}; bool used[1001][1001]; void get_sum() { r(i, h - 1) r(j, w - 1) { if (s[i][j] == t[0][0] && s[i + 1][j] == t[1][0] & s[i][j + 1] == t[0][1] && s[i + 1][j + 1] == t[1][1]) { used[i][j] = 1; sum++; } } } int get_res(int y, int x) { int res = sum; r(i, 4) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy < 0 || xx < 0 || yy >= h || xx >= w) continue; if (used[yy][xx]) res--; } r(i, 4) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy < 0 || xx < 0 || yy >= h || xx >= w) continue; r(k, 4) { int sy = yy + gy[k]; int sx = xx + gx[k]; if (sy < 0 || sx < 0 || sy >= h || sx >= w) continue; if (s[sy][sx] != t[gy[k]][gx[k]]) break; if (k == 3) res++; } } return res; } int get_ans(int y, int x) { char tmp = s[y][x]; int res = 0; r(i, 3) { if (s[y][x] != c[i]) { s[y][x] = c[i]; res = max(res, get_res(y, x)); s[y][x] = tmp; } } return res; } int main() { cin >> h >> w; r(i, h) cin >> s[i]; r(i, 2) cin >> t[i]; get_sum(); ans = sum; r(i, h) r(j, w) { ans = max(ans, get_ans(i, j)); } cout << ans << endl; }
[["-", 0, 9, 0, 57, 64, 1, 0, 27, 28, 22], ["+", 0, 9, 0, 57, 64, 1, 0, 27, 28, 22]]
1
629
m, n = gets.split.map &:to_i f = (1..m).map { gets.chomp } e = gets.chomp.split("") + gets.chomp.split("") hash = {} count = 0 (0..m-2).each{|i| (0..n-2).each{|j| a = [ f[i][j] == e[0], f[i][j+1] == e[1], f[i+1][j] == e[2], f[i+1][j+1] == e[3], ] case a.count{|b| b} when 4 count += 1 [[i,j], [i,j+1], [i+1,j], [i+1,j+1]].each{|y, x| pos = [y, x] hash[pos] ||= Hash.new(0) (%w(J O I) - [f[i][j]]).each{|c| hash[pos][c] -= 1 } } when 3 k = a.index{|b| b} pos = [i + k/2, j + k%2] hash[pos] ||= Hash.new(0) hash[pos][e[k]] += 1 end } } p count + [hash.values.map(&:values).flatten.max, 0].max
m, n = gets.split.map &:to_i f = (1..m).map { gets.chomp } e = gets.chomp.split("") + gets.chomp.split("") hash = {} count = 0 (0..m-2).each{|i| (0..n-2).each{|j| a = [ f[i][j] == e[0], f[i][j+1] == e[1], f[i+1][j] == e[2], f[i+1][j+1] == e[3], ] case a.count{|b| b} when 4 count += 1 [[i,j], [i,j+1], [i+1,j], [i+1,j+1]].each{|y, x| pos = [y, x] hash[pos] ||= Hash.new(0) (%w(J O I) - [f[i][j]]).each{|c| hash[pos][c] -= 1 } } when 3 k = a.index{|b| !b} pos = [i + k/2, j + k%2] hash[pos] ||= Hash.new(0) hash[pos][e[k]] += 1 end } } p count + [hash.values.map(&:values).flatten.max || 0, 0].max
[["+", 12, 652, 196, 196, 8, 734, 0, 748, 17, 111], ["+", 0, 738, 12, 652, 486, 516, 0, 738, 17, 106], ["+", 0, 738, 12, 652, 486, 516, 0, 738, 12, 612]]
4
327
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; #define WHITE 0LL #define GRAY 1LL #define BLACK 2LL #define MAX_N 500000LL #define INF 1145141145141919810LL long long color[MAX_N], X, n, m, h[MAX_N], s, t, u; vector<pair<long long, long long>> x[MAX_N]; pair<long long, long long> d[MAX_N]; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> Q; long long dijkstra() { for (int i = 0; i < MAX_N; i++) { color[i] = WHITE; d[i] = make_pair(INF, INF); } d[1] = {0, X}; color[1] = GRAY; Q.push(make_pair(0LL, 1LL)); while (!Q.empty()) { long long A = Q.top().first; long long B = Q.top().second; Q.pop(); color[B] = BLACK; if (d[B].first < A * (-1)) continue; for (int v = 0; v < x[B].size(); v++) { long long w = x[B][v].first; if (color[w] == BLACK) continue; long long S = 0LL; long long U = 0LL; if (d[B].second <= x[B][v].second) { U = 0LL; S = 2 * x[B][v].second - d[B].second; } else { U = min(d[B].second - x[B][v].second, h[w]); S = d[B].second - U; } if (d[w].first > d[B].first + S) { d[w].first = d[B].first + S; d[w].second = U; Q.push(make_pair(d[w].first, w)); color[w] = GRAY; } } } if (d[n].second == 0) { return d[n].first + h[n]; } return d[n].first + (h[n] - d[n].second); } int main() { cin >> n >> m >> X; for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 0; i < m; i++) { cin >> s >> t >> u; if (h[s] >= u) { x[s].push_back(make_pair(t, u)); } if (h[t] >= u) { x[t].push_back(make_pair(s, u)); } } long long b = dijkstra(); if (b < INF) { cout << b << endl; } else { cout << "-1" << endl; } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; #define WHITE 0LL #define GRAY 1LL #define BLACK 2LL #define MAX_N 500000LL #define INF 1145141145141919810LL long long color[MAX_N], X, n, m, h[MAX_N], s, t, u; vector<pair<long long, long long>> x[MAX_N]; pair<long long, long long> d[MAX_N]; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> Q; long long dijkstra() { for (int i = 0; i < MAX_N; i++) { color[i] = WHITE; d[i] = make_pair(INF, 0); } d[1] = {0, X}; color[1] = GRAY; Q.push(make_pair(0LL, 1LL)); while (!Q.empty()) { long long A = Q.top().first; long long B = Q.top().second; Q.pop(); color[B] = BLACK; if (d[B].first < A * (-1)) continue; for (int v = 0; v < x[B].size(); v++) { long long w = x[B][v].first; if (color[w] == BLACK) continue; long long S = 0LL; long long U = 0LL; if (d[B].second <= x[B][v].second) { U = 0LL; S = 2 * x[B][v].second - d[B].second; } else { U = min(d[B].second - x[B][v].second, h[w]); S = d[B].second - U; } if (d[w].first > d[B].first + S) { d[w].first = d[B].first + S; d[w].second = U; Q.push(make_pair(d[w].first, w)); color[w] = GRAY; } } } if (d[n].second == 0) { return d[n].first + h[n]; } return d[n].first + (h[n] - d[n].second); } int main() { cin >> n >> m >> X; for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 0; i < m; i++) { cin >> s >> t >> u; if (h[s] >= u) { x[s].push_back(make_pair(t, u)); } if (h[t] >= u) { x[t].push_back(make_pair(s, u)); } } long long b = dijkstra(); if (b < INF) { cout << b << endl; } else { cout << "-1" << endl; } return 0; }
[["-", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 13]]
1
669
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" struct aa { int now; long long int height; long long int time; }; class Compare { public: // aa?????????????????¶ bool operator()(const aa &l, const aa &r) { return l.time - l.height > r.time - r.height; } }; int main() { int N, M, X; cin >> N >> M >> X; vector<long long int> memos(N, 1e17); vector<long long int> hs(N); for (int i = 0; i < N; ++i) { cin >> hs[i]; } vector<vector<pair<int, int>>> edges(N); for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; edges[a].push_back(make_pair(b, c)); edges[b].push_back(make_pair(a, c)); } priority_queue<aa, vector<aa>, Compare> que; que.push(aa{0, X, X}); memos[0] = 0; long long int ans = -1; while (!que.empty()) { aa atop(que.top()); que.pop(); const long long int nowh = atop.height; const long long int nowt = atop.time; const long long int now = atop.now; if (now == N - 1) { ans = nowt + (hs[N - 1] - nowh); break; } for (auto e : edges[now]) { const int next = e.first; if (e.second > hs[now]) continue; else { if (nowh >= e.second) { if (nowh - e.second <= hs[next]) { const long long int nexth = nowh - e.second; const long long int nextt = nowt + e.second; if (memos[next] > nextt - nexth) { memos[next] = nextt - nexth; que.push(aa{next, nexth, nextt}); } } else { const long long int nexth = hs[next]; const long long int nextt = nowt + (nowh - hs[next]); if (memos[next] > nextt - nexth) { memos[next] = nextt - nexth; que.push(aa{next, nexth, nextt}); } } } else { const long long int nexth = 0; const long long int nextt = nowt + 2 * e.second - nowh; if (memos[next] > nextt - nexth) { memos[next] = nextt - nexth; que.push(aa{next, nexth, nextt}); } } } } } if (ans != -1) { cout << ans + X << endl; } else { cout << ans << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" struct aa { int now; long long int height; long long int time; }; class Compare { public: // aa?????????????????¶ bool operator()(const aa &l, const aa &r) { return l.time - l.height > r.time - r.height; } }; int main() { int N, M, X; cin >> N >> M >> X; vector<long long int> memos(N, 1e17); vector<long long int> hs(N); for (int i = 0; i < N; ++i) { cin >> hs[i]; } vector<vector<pair<int, int>>> edges(N); for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; edges[a].push_back(make_pair(b, c)); edges[b].push_back(make_pair(a, c)); } priority_queue<aa, vector<aa>, Compare> que; que.push(aa{0, X, X}); memos[0] = 0; long long int ans = -1; while (!que.empty()) { aa atop(que.top()); que.pop(); const long long int nowh = atop.height; const long long int nowt = atop.time; const long long int now = atop.now; if (now == N - 1) { ans = nowt + (hs[N - 1] - nowh); break; } for (auto e : edges[now]) { const int next = e.first; if (e.second > hs[now]) continue; else { if (nowh >= e.second) { if (nowh - e.second <= hs[next]) { const long long int nexth = nowh - e.second; const long long int nextt = nowt + e.second; if (memos[next] > nextt - nexth) { memos[next] = nextt - nexth; que.push(aa{next, nexth, nextt}); } } else { const long long int nexth = hs[next]; const long long int nextt = nowt + (nowh - hs[next]); if (memos[next] > nextt - nexth) { memos[next] = nextt - nexth; que.push(aa{next, nexth, nextt}); } } } else { const long long int nexth = 0; const long long int nextt = nowt + 2 * e.second - nowh; if (memos[next] > nextt - nexth) { memos[next] = nextt - nexth; que.push(aa{next, nexth, nextt}); } } } } } if (ans != -1) { cout << ans - X << endl; } else { cout << ans << endl; } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 33]]
1
659
#pragma comment(linker, "/STACK:36777216") //#pragma GCC optimize ("O2") #define LOCAL //#include "testlib.h" #include <algorithm> #include <cstdio> #include <iostream> #include <set> #include <vector> //#include <tr1/unordered_set> //#include <tr1/unordered_map> //#include <array> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define DWN(i, b, a) for (int i = b - 1; i >= a; --i) #define REP_1(i, n) for (int i = 1; i <= n; ++i) #define FOR_1(i, a, b) for (int i = a; i <= b; ++i) #define DWN_1(i, b, a) for (int i = b; i >= a; --i) #define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i) #define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i) #define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i) #define REP_N(i, n) for (i = 0; i < n; ++i) #define FOR_N(i, a, b) for (i = a; i < b; ++i) #define DWN_N(i, b, a) for (i = b - 1; i >= a; --i) #define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i) #define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i) #define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i) #define REP_1_N(i, n) for (i = 1; i <= n; ++i) #define FOR_1_N(i, a, b) for (i = a; i <= b; ++i) #define DWN_1_N(i, b, a) for (i = b; i >= a; --i) #define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i) #define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i) #define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i) #define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i) #define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i) #define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i) #define ECH(it, A) \ for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it) #define rECH(it, A) \ for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it) #define REP_S(i, str) for (char *i = str; *i; ++i) #define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i]) #define REP_G(i, u) REP_L(i, hd[u], suc) #define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s) #define DO(n) for (int ____n = n; ____n-- > 0;) #define REP_2(i, j, n, m) REP(i, n) REP(j, m) #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m) #define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l) #define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l) #define REP_4(i, j, k, ii, n, m, l, nn) \ REP(i, n) REP(j, m) REP(k, l) REP(ii, nn) #define REP_4_1(i, j, k, ii, n, m, l, nn) \ REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn) #define ALL(A) A.begin(), A.end() #define LLA(A) A.rbegin(), A.rend() #define CPY(A, B) memcpy(A, B, sizeof(A)) #define INS(A, P, B) A.insert(A.begin() + P, B) #define ERS(A, P) A.erase(A.begin() + P) #define LBD(A, x) (lower_bound(ALL(A), x) - A.begin()) #define UBD(A, x) (upper_bound(ALL(A), x) - A.begin()) #define CTN(T, x) (T.find(x) != T.end()) #define SZ(A) int((A).size()) #define PB push_back #define MP(A, B) make_pair(A, B) #define PTT pair<T, T> #define Ts *this #define rTs return Ts #define fi first #define se second #define re real() #define im imag() #define Rush for (int ____T = RD(); ____T--;) #define Display(A, n, m) \ { \ REP(i, n) { \ REP(j, m - 1) cout << A[i][j] << " "; \ cout << A[i][m - 1] << endl; \ } \ } #define Display_1(A, n, m) \ { \ REP_1(i, n) { \ REP_1(j, m - 1) cout << A[i][j] << " "; \ cout << A[i][m] << endl; \ } \ } typedef long long LL; // typedef long double DB; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; typedef vector<int> VI; template <class T> inline T &RD(T &); template <class T> inline void OT(const T &); // inline int RD(){int x; return RD(x);} inline LL RD() { LL x; return RD(x); } inline DB &RF(DB &); inline DB RF() { DB x; return RF(x); } inline char *RS(char *s); inline char &RC(char &c); inline char RC(); inline char &RC(char &c) { scanf(" %c", &c); return c; } inline char RC() { char c; return RC(c); } // inline char& RC(char &c){c = getchar(); return c;} // inline char RC(){return getchar();} template <class T> inline T &RDD(T &); inline LL RDD() { LL x; return RDD(x); } template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) { RD(x0), RD(x1); return x0; } template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) { RD(x0), RD(x1), RD(x2); return x0; } template <class T0, class T1, class T2, class T3> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { RD(x0), RD(x1), RD(x2), RD(x3); return x0; } template <class T0, class T1, class T2, class T3, class T4> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0; } template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) { OT(x0), OT(x1); } template <class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) { OT(x0), OT(x1), OT(x2); } template <class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) { OT(x0), OT(x1), OT(x2), OT(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); } inline char &RC(char &a, char &b) { RC(a), RC(b); return a; } inline char &RC(char &a, char &b, char &c) { RC(a), RC(b), RC(c); return a; } inline char &RC(char &a, char &b, char &c, char &d) { RC(a), RC(b), RC(c), RC(d); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e) { RC(a), RC(b), RC(c), RC(d), RC(e); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) { RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) { RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a; } inline DB &RF(DB &a, DB &b) { RF(a), RF(b); return a; } inline DB &RF(DB &a, DB &b, DB &c) { RF(a), RF(b), RF(c); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d) { RF(a), RF(b), RF(c), RF(d); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) { RF(a), RF(b), RF(c), RF(d), RF(e); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a; } inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) { RDD(a), RDD(b); return a; } template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) { RDD(a), RDD(b), RDD(c); return a; } template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); } template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); } template <class T> inline void CLR(T &A) { A.clear(); } template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) { RST(A0), RST(A1); } template <class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2) { RST(A0), RST(A1), RST(A2); } template <class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { RST(A0), RST(A1), RST(A2), RST(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); } template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) { FLC(A0, x), FLC(A1, x); } template <class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x); } template <class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x); } template <class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x); } template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) { CLR(A0), CLR(A1); } template <class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2) { CLR(A0), CLR(A1), CLR(A2); } template <class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { CLR(A0), CLR(A1), CLR(A2), CLR(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); } template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); } template <class T> inline bool EPT(T &a) { return a.empty(); } template <class T> inline T &SRT(T &A) { sort(ALL(A)); return A; } template <class T, class C> inline T &SRT(T &A, C B) { sort(ALL(A), B); return A; } template <class T> inline T &RVS(T &A) { reverse(ALL(A)); return A; } template <class T> inline T &UNQQ(T &A) { A.resize(unique(ALL(A)) - A.begin()); return A; } template <class T> inline T &UNQ(T &A) { SRT(A); return UNQQ(A); } //} /** Constant List .. **/ //{ const int MOD = int(1e9) + 7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; //} /** Add On .. **/ //{ // <<= '0. Nichi Joo ., //{ template <class T> inline bool checkMin(T &a, const T b) { return b < a ? a = b, 1 : 0; } template <class T> inline bool checkMax(T &a, const T b) { return a < b ? a = b, 1 : 0; } template <class T, class C> inline bool checkUpd(T &a, const T b, C c) { return c(b, a) ? a = b, 1 : 0; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T min(T a, T b, T c, T d, T e) { return min(min(min(a, b), min(c, d)), e); } template <class T> inline T max(T a, T b, T c, T d, T e) { return max(max(max(a, b), max(c, d)), e); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; } template <class T> T abs(T x) { return x > 0 ? x : -x; } inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; } inline int sgn(DB x, DB y) { return sgn(x - y); } //} //} /** I/O Accelerator Interface .. **/ //{ #define g (c = getchar()) #define d isdigit(g) #define p x = x * 10 + c - '0' #define n x = x * 10 + '0' - c #define pp l /= 10, p #define nn l /= 10, n template <class T> inline T &RD(T &x) { char c; while (!d) ; x = c - '0'; while (d) p; return x; } template <class T> inline T &RDD(T &x) { char c; while (g, c != '-' && !isdigit(c)) ; if (c == '-') { x = '0' - g; while (d) n; } else { x = c - '0'; while (d) p; } return x; } inline DB &RF(DB &x) { // scanf("%lf", &x); char c; while (g, c != '-' && c != '.' && !isdigit(c)) ; if (c == '-') if (g == '.') { x = 0; DB l = 1; while (d) nn; x *= l; } else { x = '0' - c; while (d) n; if (c == '.') { DB l = 1; while (d) nn; x *= l; } } else if (c == '.') { x = 0; DB l = 1; while (d) pp; x *= l; } else { x = c - '0'; while (d) p; if (c == '.') { DB l = 1; while (d) pp; x *= l; } } return x; } #undef nn #undef pp #undef n #undef p #undef d #undef g inline char *RS(char *s) { // gets(s); scanf("%s", s); return s; } LL last_ans; int Case; template <class T> inline void OT(const T &x) { // printf("Case #%d: ", ++Case); // printf("%lld\n", x); // printf("%.9f\n", x); // printf("%d\n", x); cout << x << endl; // last_ans = x; } //}/* //.................................................................................................................................. //*/ const int MAXN = 100020; struct seg_tree { static const int DEPTH = 19; static const int SIZE = 1 << DEPTH; int bit[1 << (DEPTH + 1)]; int renew[1 << (DEPTH + 1)]; seg_tree() {} void init() { for (int i = 0; i < 2 * SIZE; i++) bit[i] = renew[i] = 0; } void add(int p, int v) { p += SIZE; while (p) { bit[p] += v; p >>= 1; } } int query(int l, int r) { l += SIZE; r += SIZE; int ret = 0; while (l < r) { if (l & 1) ret += bit[l++]; if (r & 1) ret += bit[--r]; l >>= 1; r >>= 1; } return ret; } void set_renew(int l, int r) { l += SIZE; r += SIZE; while (l < r) { if (l & 1) renew[l++] = 1; if (r & 1) renew[--r] = 1; l >>= 1; r >>= 1; } } bool is_renew(int p) { p += SIZE; while (p) { if (renew[p]) return true; p >>= 1; } return false; } void unset_renew(int p) { p += SIZE; for (int i = DEPTH - 1; i > 0; i--) { if (renew[p >> i]) { renew[p >> i] = 0; renew[(p >> i) * 2] = renew[(p >> i) * 2 + 1] = 1; } } renew[p] = 0; } }; struct action { int pos, act, left, right; action(int p, int a, int l, int r) { pos = p; act = a; left = l; right = r; } }; inline bool operator<(const action &a, const action &b) { return a.pos < b.pos || (a.pos == b.pos && a.act < b.act); } int W, H, N; int x1[MAXN], y1[MAXN], x2[MAXN], y2[MAXN]; vector<int> uf; seg_tree seg; int target[MAXN * 2]; int root(int p) { // cout << p << " " << uf[p] << endl; return (uf[p] == p) ? p : (uf[p] = root(uf[p])); } bool join(int p, int q) { p = root(p); q = root(q); if (p == q) return false; uf[q] = p; return true; } void adjust(int p) { if (seg.is_renew(p)) { int t = uf.size(); uf.push_back(t); // cout << " " << uf[t] << " " << t << endl; seg.unset_renew(p); target[p] = t; } } int main() { scanf("%d%d%d", &W, &H, &N); // N = 0; for (int i = 0; i < N; i++) { scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i); if (x1[i] > x2[i]) swap(x1[i], x2[i]); if (y1[i] > y2[i]) swap(y1[i], y2[i]); } for (int i = 0; i < 2 * N; i++) target[i] = -1; x1[N] = 0; y1[N] = 0; x2[N] = W; y2[N] = 0; x1[N + 1] = 0; y1[N + 1] = 0; x2[N + 1] = 0; y2[N + 1] = H; x1[N + 2] = W; y1[N + 2] = 0; x2[N + 2] = W; y2[N + 2] = H; x1[N + 3] = 0; y1[N + 3] = H; x2[N + 3] = W; y2[N + 3] = H; N += 4; vector<int> xs; for (int i = 0; i < N; i++) { xs.push_back(x1[i]); xs.push_back(x2[i]); } xs.push_back(-1); sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for (int i = 0; i < N; i++) { x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin(); x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin(); } set<int> S; S.insert(0); target[0] = 0; uf.push_back(0); vector<action> A; for (int i = 0; i < N; i++) { if (x1[i] == x2[i]) { A.push_back(action(y1[i], 0, x1[i], -1)); A.push_back(action(y2[i], 2, x1[i], -1)); } else { A.push_back(action(y1[i], 1, x1[i], x2[i])); } } sort(A.begin(), A.end()); long long ret = 0; for (int i = 0; i < A.size(); i++) { action V = A[i]; if (V.act == 0) { int lf = *--S.lower_bound(V.left); adjust(lf); adjust(V.left); target[V.left] = target[lf]; S.insert(V.left); // cout << "+ " << V.left << endl; seg.add(V.left, 1); } else if (V.act == 1) { int ll = *++S.lower_bound(V.left - 1); int rr = *--S.upper_bound(V.right); if (rr <= ll) continue; int count = seg.query(ll, rr + 1); // cout << " " << V.left << " "<< V.right << " " << count << endl; // cout << " " << ll << " "<< rr << " " << count << endl; // if(count < 2) continue; ret += count - 1; // cout << V.left << " " << V.right << " " << *--S.upper_bound(V.right) << // endl; seg.set_renew(ll, rr); // seg.set_renew(V.left, V.right-1); } else if (V.act == 2) { int lf = *--S.lower_bound(V.left); adjust(lf); adjust(V.left); // cout << " " << target[lf] << " " << target[V.left] << endl; if (join(target[lf], target[V.left])) --ret; S.erase(V.left); // cout << "- " << V.left << endl; seg.add(V.left, -1); } // cout << V.act << " " << ret << endl; } printf("%lld\n", ret); return 0; }
#pragma comment(linker, "/STACK:36777216") //#pragma GCC optimize ("O2") #define LOCAL //#include "testlib.h" #include <algorithm> #include <cstdio> #include <iostream> #include <set> #include <vector> //#include <tr1/unordered_set> //#include <tr1/unordered_map> //#include <array> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define DWN(i, b, a) for (int i = b - 1; i >= a; --i) #define REP_1(i, n) for (int i = 1; i <= n; ++i) #define FOR_1(i, a, b) for (int i = a; i <= b; ++i) #define DWN_1(i, b, a) for (int i = b; i >= a; --i) #define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i) #define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i) #define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i) #define REP_N(i, n) for (i = 0; i < n; ++i) #define FOR_N(i, a, b) for (i = a; i < b; ++i) #define DWN_N(i, b, a) for (i = b - 1; i >= a; --i) #define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i) #define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i) #define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i) #define REP_1_N(i, n) for (i = 1; i <= n; ++i) #define FOR_1_N(i, a, b) for (i = a; i <= b; ++i) #define DWN_1_N(i, b, a) for (i = b; i >= a; --i) #define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i) #define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i) #define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i) #define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i) #define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i) #define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i) #define ECH(it, A) \ for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it) #define rECH(it, A) \ for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it) #define REP_S(i, str) for (char *i = str; *i; ++i) #define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i]) #define REP_G(i, u) REP_L(i, hd[u], suc) #define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s) #define DO(n) for (int ____n = n; ____n-- > 0;) #define REP_2(i, j, n, m) REP(i, n) REP(j, m) #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m) #define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l) #define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l) #define REP_4(i, j, k, ii, n, m, l, nn) \ REP(i, n) REP(j, m) REP(k, l) REP(ii, nn) #define REP_4_1(i, j, k, ii, n, m, l, nn) \ REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn) #define ALL(A) A.begin(), A.end() #define LLA(A) A.rbegin(), A.rend() #define CPY(A, B) memcpy(A, B, sizeof(A)) #define INS(A, P, B) A.insert(A.begin() + P, B) #define ERS(A, P) A.erase(A.begin() + P) #define LBD(A, x) (lower_bound(ALL(A), x) - A.begin()) #define UBD(A, x) (upper_bound(ALL(A), x) - A.begin()) #define CTN(T, x) (T.find(x) != T.end()) #define SZ(A) int((A).size()) #define PB push_back #define MP(A, B) make_pair(A, B) #define PTT pair<T, T> #define Ts *this #define rTs return Ts #define fi first #define se second #define re real() #define im imag() #define Rush for (int ____T = RD(); ____T--;) #define Display(A, n, m) \ { \ REP(i, n) { \ REP(j, m - 1) cout << A[i][j] << " "; \ cout << A[i][m - 1] << endl; \ } \ } #define Display_1(A, n, m) \ { \ REP_1(i, n) { \ REP_1(j, m - 1) cout << A[i][j] << " "; \ cout << A[i][m] << endl; \ } \ } typedef long long LL; // typedef long double DB; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; typedef vector<int> VI; template <class T> inline T &RD(T &); template <class T> inline void OT(const T &); // inline int RD(){int x; return RD(x);} inline LL RD() { LL x; return RD(x); } inline DB &RF(DB &); inline DB RF() { DB x; return RF(x); } inline char *RS(char *s); inline char &RC(char &c); inline char RC(); inline char &RC(char &c) { scanf(" %c", &c); return c; } inline char RC() { char c; return RC(c); } // inline char& RC(char &c){c = getchar(); return c;} // inline char RC(){return getchar();} template <class T> inline T &RDD(T &); inline LL RDD() { LL x; return RDD(x); } template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) { RD(x0), RD(x1); return x0; } template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) { RD(x0), RD(x1), RD(x2); return x0; } template <class T0, class T1, class T2, class T3> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { RD(x0), RD(x1), RD(x2), RD(x3); return x0; } template <class T0, class T1, class T2, class T3, class T4> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0; } template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) { OT(x0), OT(x1); } template <class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) { OT(x0), OT(x1), OT(x2); } template <class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) { OT(x0), OT(x1), OT(x2), OT(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); } inline char &RC(char &a, char &b) { RC(a), RC(b); return a; } inline char &RC(char &a, char &b, char &c) { RC(a), RC(b), RC(c); return a; } inline char &RC(char &a, char &b, char &c, char &d) { RC(a), RC(b), RC(c), RC(d); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e) { RC(a), RC(b), RC(c), RC(d), RC(e); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) { RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a; } inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) { RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a; } inline DB &RF(DB &a, DB &b) { RF(a), RF(b); return a; } inline DB &RF(DB &a, DB &b, DB &c) { RF(a), RF(b), RF(c); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d) { RF(a), RF(b), RF(c), RF(d); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) { RF(a), RF(b), RF(c), RF(d), RF(e); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a; } inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a; } inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) { RDD(a), RDD(b); return a; } template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) { RDD(a), RDD(b), RDD(c); return a; } template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); } template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); } template <class T> inline void CLR(T &A) { A.clear(); } template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) { RST(A0), RST(A1); } template <class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2) { RST(A0), RST(A1), RST(A2); } template <class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { RST(A0), RST(A1), RST(A2), RST(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); } template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) { FLC(A0, x), FLC(A1, x); } template <class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x); } template <class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x); } template <class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x); } template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) { CLR(A0), CLR(A1); } template <class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2) { CLR(A0), CLR(A1), CLR(A2); } template <class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { CLR(A0), CLR(A1), CLR(A2), CLR(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); } template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); } template <class T> inline bool EPT(T &a) { return a.empty(); } template <class T> inline T &SRT(T &A) { sort(ALL(A)); return A; } template <class T, class C> inline T &SRT(T &A, C B) { sort(ALL(A), B); return A; } template <class T> inline T &RVS(T &A) { reverse(ALL(A)); return A; } template <class T> inline T &UNQQ(T &A) { A.resize(unique(ALL(A)) - A.begin()); return A; } template <class T> inline T &UNQ(T &A) { SRT(A); return UNQQ(A); } //} /** Constant List .. **/ //{ const int MOD = int(1e9) + 7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; //} /** Add On .. **/ //{ // <<= '0. Nichi Joo ., //{ template <class T> inline bool checkMin(T &a, const T b) { return b < a ? a = b, 1 : 0; } template <class T> inline bool checkMax(T &a, const T b) { return a < b ? a = b, 1 : 0; } template <class T, class C> inline bool checkUpd(T &a, const T b, C c) { return c(b, a) ? a = b, 1 : 0; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T min(T a, T b, T c, T d, T e) { return min(min(min(a, b), min(c, d)), e); } template <class T> inline T max(T a, T b, T c, T d, T e) { return max(max(max(a, b), max(c, d)), e); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; } template <class T> T abs(T x) { return x > 0 ? x : -x; } inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; } inline int sgn(DB x, DB y) { return sgn(x - y); } //} //} /** I/O Accelerator Interface .. **/ //{ #define g (c = getchar()) #define d isdigit(g) #define p x = x * 10 + c - '0' #define n x = x * 10 + '0' - c #define pp l /= 10, p #define nn l /= 10, n template <class T> inline T &RD(T &x) { char c; while (!d) ; x = c - '0'; while (d) p; return x; } template <class T> inline T &RDD(T &x) { char c; while (g, c != '-' && !isdigit(c)) ; if (c == '-') { x = '0' - g; while (d) n; } else { x = c - '0'; while (d) p; } return x; } inline DB &RF(DB &x) { // scanf("%lf", &x); char c; while (g, c != '-' && c != '.' && !isdigit(c)) ; if (c == '-') if (g == '.') { x = 0; DB l = 1; while (d) nn; x *= l; } else { x = '0' - c; while (d) n; if (c == '.') { DB l = 1; while (d) nn; x *= l; } } else if (c == '.') { x = 0; DB l = 1; while (d) pp; x *= l; } else { x = c - '0'; while (d) p; if (c == '.') { DB l = 1; while (d) pp; x *= l; } } return x; } #undef nn #undef pp #undef n #undef p #undef d #undef g inline char *RS(char *s) { // gets(s); scanf("%s", s); return s; } LL last_ans; int Case; template <class T> inline void OT(const T &x) { // printf("Case #%d: ", ++Case); // printf("%lld\n", x); // printf("%.9f\n", x); // printf("%d\n", x); cout << x << endl; // last_ans = x; } //}/* //.................................................................................................................................. //*/ const int MAXN = 100020; struct seg_tree { static const int DEPTH = 19; static const int SIZE = 1 << DEPTH; int bit[1 << (DEPTH + 1)]; int renew[1 << (DEPTH + 1)]; seg_tree() {} void init() { for (int i = 0; i < 2 * SIZE; i++) bit[i] = renew[i] = 0; } void add(int p, int v) { p += SIZE; while (p) { bit[p] += v; p >>= 1; } } int query(int l, int r) { l += SIZE; r += SIZE; int ret = 0; while (l < r) { if (l & 1) ret += bit[l++]; if (r & 1) ret += bit[--r]; l >>= 1; r >>= 1; } return ret; } void set_renew(int l, int r) { l += SIZE; r += SIZE; while (l < r) { if (l & 1) renew[l++] = 1; if (r & 1) renew[--r] = 1; l >>= 1; r >>= 1; } } bool is_renew(int p) { p += SIZE; while (p) { if (renew[p]) return true; p >>= 1; } return false; } void unset_renew(int p) { p += SIZE; for (int i = DEPTH - 1; i > 0; i--) { if (renew[p >> i]) { renew[p >> i] = 0; renew[(p >> i) * 2] = renew[(p >> i) * 2 + 1] = 1; } } renew[p] = 0; } }; struct action { int pos, act, left, right; action(int p, int a, int l, int r) { pos = p; act = a; left = l; right = r; } }; inline bool operator<(const action &a, const action &b) { return a.pos < b.pos || (a.pos == b.pos && a.act < b.act); } int W, H, N; int x1[MAXN], y1[MAXN], x2[MAXN], y2[MAXN]; vector<int> uf; seg_tree seg; int target[MAXN * 2]; int root(int p) { // cout << p << " " << uf[p] << endl; return (uf[p] == p) ? p : (uf[p] = root(uf[p])); } bool join(int p, int q) { p = root(p); q = root(q); if (p == q) return false; uf[q] = p; return true; } void adjust(int p) { if (seg.is_renew(p)) { int t = uf.size(); uf.push_back(t); // cout << " " << uf[t] << " " << t << endl; seg.unset_renew(p); target[p] = t; } } int main() { scanf("%d%d%d", &W, &H, &N); // N = 0; for (int i = 0; i < N; i++) { scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i); if (x1[i] > x2[i]) swap(x1[i], x2[i]); if (y1[i] > y2[i]) swap(y1[i], y2[i]); } for (int i = 0; i < 2 * N; i++) target[i] = -1; x1[N] = 0; y1[N] = 0; x2[N] = W; y2[N] = 0; x1[N + 1] = 0; y1[N + 1] = 0; x2[N + 1] = 0; y2[N + 1] = H; x1[N + 2] = W; y1[N + 2] = 0; x2[N + 2] = W; y2[N + 2] = H; x1[N + 3] = 0; y1[N + 3] = H; x2[N + 3] = W; y2[N + 3] = H; N += 4; vector<int> xs; for (int i = 0; i < N; i++) { xs.push_back(x1[i]); xs.push_back(x2[i]); } xs.push_back(-1); sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for (int i = 0; i < N; i++) { x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin(); x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin(); } set<int> S; S.insert(0); target[0] = 0; uf.push_back(0); vector<action> A; for (int i = 0; i < N; i++) { if (x1[i] == x2[i]) { A.push_back(action(y1[i], 0, x1[i], -1)); A.push_back(action(y2[i], 2, x1[i], -1)); } else { A.push_back(action(y1[i], 1, x1[i], x2[i])); } } sort(A.begin(), A.end()); long long ret = 0; for (int i = 0; i < A.size(); i++) { action V = A[i]; if (V.act == 0) { int lf = *--S.lower_bound(V.left); adjust(lf); adjust(V.left); target[V.left] = target[lf]; S.insert(V.left); // cout << "+ " << V.left << endl; seg.add(V.left, 1); } else if (V.act == 1) { int ll = *S.lower_bound(V.left); int rr = *--S.upper_bound(V.right); // cout << " " << V.left << " "<< V.right << endl; if (rr <= ll) continue; int count = seg.query(ll, rr + 1); // cout << " " << ll << " "<< rr << " " << count << endl; // if(count < 2) continue; ret += count - 1; // cout << V.left << " " << V.right << " " << *--S.upper_bound(V.right) << // endl; seg.set_renew(ll, rr); // seg.set_renew(V.left, V.right-1); } else if (V.act == 2) { int lf = *--S.lower_bound(V.left); adjust(lf); adjust(V.left); // cout << " " << target[lf] << " " << target[V.left] << endl; if (join(target[lf], target[V.left])) --ret; S.erase(V.left); // cout << "- " << V.left << endl; seg.add(V.left, -1); } // cout << V.act << " " << ret << endl; } printf("%lld\n", ret); return 0; }
[["-", 0, 43, 49, 50, 51, 66, 28, 27, 17, 29], ["-", 28, 27, 28, 2, 3, 4, 0, 16, 17, 33], ["-", 28, 27, 28, 2, 3, 4, 0, 16, 12, 13]]
1
6,216
import math class seg_tree: def __init__(self, n): self.depth = math.ceil(math.log(n, 2)) self.size = 1 << self.depth self.bit = [0] * 2 * self.size self.renew = [0] * 2 * self.size def add(self, p, v): p += self.size; while p: self.bit[p] += v p >>= 1 def query(self, l, r): l += self.size r += self.size ret = 0 while l < r: if l & 1: ret += self.bit[l] l += 1 if r & 1: r -= 1 ret += self.bit[r] l >>= 1 r >>= 1 return ret def set_renew(self, l, r): l += self.size r += self.size while l < r: if l & 1: self.renew[l] = 1 l += 1 if r & 1: r -= 1 self.renew[r] = 1 l >>= 1 r >>= 1 def is_renew(self, p): p += self.size while p: if self.renew[p]: return True p >>= 1 return False def unset_renew(self, p): p += self.size for i in range(self.depth - 1,0,-1): if self.renew[p >> i]: self.renew[p >> i] = 0 self.renew[(p>>i)*2] = self.renew[(p>>i)*2+1] = 1 self.renew[p] = 0 def get_lf(self, r): l = self.size r += self.size while l < r: if r & 1: r -= 1 if self.bit[r]: while r < self.size: r <<= 1 if self.bit[r + 1]: r += 1 return r - self.size if l & 1: l += 1 l >>= 1 r >>= 1 return -1 from collections import UserList class union_find(UserList): def __init__(self): UserList.__init__(self) def root(self, p): if self.data[p] < 0: return p self.data[p] = self.root(self.data[p]) return self.data[p] def join(self, p, q): p, q = self.root(p), self.root(q) if p == q: return False if self.data[p] < self.data[q]: p, q = q, p self.data[p], self.data[q] = self.data[q], p return True def bisect(a,v): l, r = 0, len(a) while l < r: m = (l + r) // 2 if a[m] < v: l = m + 1 else: r = m return l def adjust(seg, uf, target, p): if seg.is_renew(p): uf.append(-1) seg.unset_renew(p) target[p] = len(uf) - 1 from operator import itemgetter @profile def main(f): w,h,n = map(int,f.readline().split()) abcd = [list(map(int,line.split())) for line in f] abcd.extend([[0,0,w,0], [0,0,0,h], [w,0,w,h], [0,h,w,h]]) xs = {x:i for i, x in enumerate(sorted(set([abcdi[0] for abcdi in abcd] +[abcdi[2] for abcdi in abcd] + [-1])))} abcd =[(xs[a],b,xs[c],d) for a,b,c,d in abcd] target = [-1] * n * 2 target[0] = 0 uf = union_find() uf.append(-1) seg = seg_tree(len(xs)) seg.add(0, 1) # a = [] for x1,y1,x2,y2 in abcd: if x1 == x2: a.append((y1, 0, x1, -1)) a.append((y2, 2, x1, -1)) else: a.append((y1, 1, x1, x2)) a.sort(key=itemgetter(0,1)) ret = 0 for _, act, left, right in a: if act == 0: lf = seg.get_lf(left) adjust(seg, uf, target, lf) adjust(seg, uf, target, left) target[left] = target[lf] seg.add(left, 1) elif act == 1: count = seg.query(left, right+1) if count < 2: continue ret += count - 1; seg.set_renew(left, seg.get_lf(right + 1)) elif act == 2: lf = seg.get_lf(left) adjust(seg, uf, target, lf) adjust(seg, uf, target, left) if uf.join(target[lf], target[left]): ret -= 1 seg.add(left, -1) print(ret) import sys f = sys.stdin main(f)
import math class seg_tree: def __init__(self, n): self.depth = math.ceil(math.log(n, 2)) self.size = 1 << self.depth self.bit = [0] * 2 * self.size self.renew = [0] * 2 * self.size def add(self, p, v): p += self.size; while p: self.bit[p] += v p >>= 1 def query(self, l, r): l += self.size r += self.size ret = 0 while l < r: if l & 1: ret += self.bit[l] l += 1 if r & 1: r -= 1 ret += self.bit[r] l >>= 1 r >>= 1 return ret def set_renew(self, l, r): l += self.size r += self.size while l < r: if l & 1: self.renew[l] = 1 l += 1 if r & 1: r -= 1 self.renew[r] = 1 l >>= 1 r >>= 1 def is_renew(self, p): p += self.size while p: if self.renew[p]: return True p >>= 1 return False def unset_renew(self, p): p += self.size for i in range(self.depth - 1,0,-1): if self.renew[p >> i]: self.renew[p >> i] = 0 self.renew[(p>>i)*2] = self.renew[(p>>i)*2+1] = 1 self.renew[p] = 0 def get_lf(self, r): l = self.size r += self.size while l < r: if r & 1: r -= 1 if self.bit[r]: while r < self.size: r <<= 1 if self.bit[r + 1]: r += 1 return r - self.size if l & 1: l += 1 l >>= 1 r >>= 1 return -1 from collections import UserList class union_find(UserList): def __init__(self): UserList.__init__(self) def root(self, p): if self.data[p] < 0: return p self.data[p] = self.root(self.data[p]) return self.data[p] def join(self, p, q): p, q = self.root(p), self.root(q) if p == q: return False if self.data[p] < self.data[q]: p, q = q, p self.data[p], self.data[q] = self.data[q], p return True def bisect(a,v): l, r = 0, len(a) while l < r: m = (l + r) // 2 if a[m] < v: l = m + 1 else: r = m return l def adjust(seg, uf, target, p): if seg.is_renew(p): uf.append(-1) seg.unset_renew(p) target[p] = len(uf) - 1 from operator import itemgetter #@profile def main(f): w,h,n = map(int,f.readline().split()) abcd = [list(map(int,line.split())) for line in f] abcd.extend([[0,0,w,0], [0,0,0,h], [w,0,w,h], [0,h,w,h]]) xs = {x:i for i, x in enumerate(sorted(set([abcdi[0] for abcdi in abcd] +[abcdi[2] for abcdi in abcd] + [-1])))} abcd =[(xs[a],b,xs[c],d) for a,b,c,d in abcd] target = [-1] * n * 2 target[0] = 0 uf = union_find() uf.append(-1) seg = seg_tree(len(xs)) seg.add(0, 1) # a = [] for x1,y1,x2,y2 in abcd: if x1 == x2: a.append((y1, 0, x1, -1)) a.append((y2, 2, x1, -1)) else: a.append((y1, 1, x1, x2)) a.sort(key=itemgetter(0,1)) ret = 0 for _, act, left, right in a: if act == 0: lf = seg.get_lf(left) adjust(seg, uf, target, lf) adjust(seg, uf, target, left) target[left] = target[lf] seg.add(left, 1) elif act == 1: count = seg.query(left, right+1) if count < 2: continue ret += count - 1; seg.set_renew(left, seg.get_lf(right + 1)) elif act == 2: lf = seg.get_lf(left) adjust(seg, uf, target, lf) adjust(seg, uf, target, left) if uf.join(target[lf], target[left]): ret -= 1 seg.add(left, -1) print(ret) import sys f = sys.stdin main(f)
[["-", 36, 36, 0, 656, 0, 692, 0, 693, 0, 549], ["-", 36, 36, 0, 656, 0, 692, 0, 693, 0, 22]]
5
1,156
/*#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> using namespace std; int main(){ int i,j,m,n,ans=0,l; static int r[2001][2001][2]={0}; char s[4001],t[4001]; scanf("%d%d",&m,&n); scanf("%s",s); scanf("%s",t); for(i=0;i<=m;i++){ for(j=0;j<=n;j++){ for(l=0;l<2;l++){ if(l==0){ if(s[i]=='I') r[i+1][j][1]=max(r[i+1][j][1],r[i][j][0]+1); if(t[j]=='I') r[i][j+1][1]=max(r[i][j+1][1],r[i][j][0]+1); } else{ if(s[i]=='O'&&r[i][j][1]!=0) r[i+1][j][0]=max(r[i+1][j][0],r[i][j][1]+1); if(t[j]=='O'&&r[i][j][1]!=0) r[i][j+1][0]=max(r[i][j+1][0],r[i][j][1]+1); } } } } for(i=0;i<=m;i++){ for(j=0;j<=n;j++){ ans=max(ans,r[i][j][1]); } } printf("%d\n",ans); return 0; } */ #include <algorithm> #include <iostream> #include <stdio.h> using namespace std; int main() { int n, i, l = -1, r, ans = 0, u = 0; static int ilm[1000001] = {0}; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &r); if (r != l) { ilm[u]++; l = r; } else { u++; ilm[u]++; l = r; } } if (u < 3) { for (i = 0; i < u; i++) ans += ilm[i]; } for (i = 0; i < u - 1; i++) ans = max(ans, ilm[i] + ilm[i + 1] + ilm[i + 2]); printf("%d\n", ans); return 0; }
/*#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> using namespace std; int main(){ int i,j,m,n,ans=0,l; static int r[2001][2001][2]={0}; char s[4001],t[4001]; scanf("%d%d",&m,&n); scanf("%s",s); scanf("%s",t); for(i=0;i<=m;i++){ for(j=0;j<=n;j++){ for(l=0;l<2;l++){ if(l==0){ if(s[i]=='I') r[i+1][j][1]=max(r[i+1][j][1],r[i][j][0]+1); if(t[j]=='I') r[i][j+1][1]=max(r[i][j+1][1],r[i][j][0]+1); } else{ if(s[i]=='O'&&r[i][j][1]!=0) r[i+1][j][0]=max(r[i+1][j][0],r[i][j][1]+1); if(t[j]=='O'&&r[i][j][1]!=0) r[i][j+1][0]=max(r[i][j+1][0],r[i][j][1]+1); } } } } for(i=0;i<=m;i++){ for(j=0;j<=n;j++){ ans=max(ans,r[i][j][1]); } } printf("%d\n",ans); return 0; } */ #include <algorithm> #include <iostream> #include <stdio.h> using namespace std; int main() { int n, i, l = -1, r, ans = 0, u = 0; static int ilm[1000001] = {0}; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &r); if (r != l) { ilm[u]++; l = r; } else { u++; ilm[u]++; l = r; } } if (u < 3) { for (i = 0; i <= u; i++) ans += ilm[i]; } for (i = 0; i < u - 1; i++) ans = max(ans, ilm[i] + ilm[i + 1] + ilm[i + 2]); printf("%d\n", ans); return 0; }
[["-", 0, 57, 64, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 17, 19]]
1
198
#include <bits/stdc++.h> using namespace std; #define MAX 100005 int n, ans = 1; int t[MAX]; int L[MAX], R[MAX]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &t[i]); L[0] = 1; for (int i = 1; i < n; i++) { L[i] = (t[i] == t[i - 1] ? 1 : L[i - 1] + 1); ans = max(ans, L[i]); } L[n] = L[n - 1]; R[n - 1] = 1; for (int i = n - 2; i >= 0; i--) { R[i] = (t[i] == t[i + 1] ? 1 : R[i + 1] + 1); ans = max(ans, R[i]); } R[n] = 0; for (int i = 1; i + 1 < n; i++) { if (t[i] != t[i - 1]) continue; int target = i + R[i]; ans = max(ans, L[i - 1] + R[i] + R[target]); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 100005 int n, ans = 1; int t[MAX]; int L[MAX], R[MAX]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &t[i]); L[0] = 1; for (int i = 1; i < n; i++) { L[i] = (t[i] == t[i - 1] ? 1 : L[i - 1] + 1); ans = max(ans, L[i]); } L[n] = L[n - 1]; R[n - 1] = 1; for (int i = n - 2; i >= 0; i--) { R[i] = (t[i] == t[i + 1] ? 1 : R[i + 1] + 1); ans = max(ans, R[i]); } R[n] = 0; for (int i = 1; i < n; i++) { if (t[i] == t[i - 1]) { int target = i + R[i]; ans = max(ans, L[i - 1] + R[i] + R[target]); } } printf("%d\n", ans); return 0; }
[["-", 8, 9, 0, 7, 15, 16, 31, 16, 17, 72], ["-", 8, 9, 0, 7, 15, 16, 31, 16, 12, 13], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 79], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["-", 0, 7, 8, 9, 0, 57, 64, 116, 0, 117], ["-", 0, 7, 8, 9, 0, 57, 64, 116, 0, 35], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
310
#include <algorithm> #include <iostream> using namespace std; int n, a[100000], color, chain[100000], cnt, maxn, recently; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } color = a[0]; cnt = 0; chain[0] = 1; for (int i = 1; i < n; i++) { if (color == a[i]) { cnt++; recently = i; } color = a[i]; chain[cnt]++; } cnt++; if (cnt == 0) { maxn = n; } if (cnt == 1) { maxn = n; } for (int i = 0; i < cnt - 2; i++) { maxn = max(maxn, chain[i] + chain[i + 1] + chain[i + 2]); } cout << maxn << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, a[100000], color, chain[100000], cnt, maxn, recently; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } color = a[0]; cnt = 0; chain[0] = 1; for (int i = 1; i < n; i++) { if (color == a[i]) { cnt++; recently = i; } color = a[i]; chain[cnt]++; } cnt++; if (cnt == 1) { maxn = n; } if (cnt == 2) { maxn = n; } for (int i = 0; i < cnt - 2; i++) { maxn = max(maxn, chain[i] + chain[i + 1] + chain[i + 2]); } cout << maxn << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
207
#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 a[MAX]; void solve() { int n; FOR(i, n) cin >> a[i]; vector<int> seg; int l = 0; for (int i = 1; i < n; i++) { if (a[i] != a[i - 1]) continue; seg.pb(i - l); l = i; } seg.pb(n - l); int ma = 0; if (seg.size() == 1) ma = seg[0]; else if (seg.size() == 2) ma = seg[0] + seg[1]; else FOR(i, seg.size() - 2) ma = max(ma, seg[i] + seg[i + 1] + seg[i + 2]); cout << ma << 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 a[MAX]; void solve() { int n; cin >> n; FOR(i, n) cin >> a[i]; vector<int> seg; int l = 0; for (int i = 1; i < n; i++) { if (a[i] != a[i - 1]) continue; seg.pb(i - l); l = i; } seg.pb(n - l); int ma = 0; if (seg.size() == 1) ma = seg[0]; else if (seg.size() == 2) ma = seg[0] + seg[1]; else FOR(i, seg.size() - 2) ma = max(ma, seg[i] + seg[i + 1] + seg[i + 2]); cout << ma << endl; } int main() { solve(); return 0; }
[["+", 0, 14, 8, 9, 0, 1, 0, 16, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 14, 8, 9, 0, 1, 0, 16, 12, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
271
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX_N = 100000; int N, a[MAX_N]; signed main() { cin >> N; int ans = 0; int cnt[MAX_N] = {}; for (int i = 0; i < N; i++) { cin >> a[i]; } cnt[N - 1] = 1; for (int i = N - 2; i >= 0; i--) { if (a[i] != a[i + 1]) cnt[i] = cnt[i + 1] + 1; else cnt[i] = 1; } for (int i = 0; i < N; i++) { int pos = i; for (int j = 0; j < 3 && pos < N; j++) pos += cnt[pos]; ans = max(ans, pos - i + 1); } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX_N = 100000; int N, a[MAX_N]; signed main() { cin >> N; int ans = 0; int cnt[MAX_N] = {}; for (int i = 0; i < N; i++) { cin >> a[i]; } cnt[N - 1] = 1; for (int i = N - 2; i >= 0; i--) { if (a[i] != a[i + 1]) cnt[i] = cnt[i + 1] + 1; else cnt[i] = 1; } for (int i = 0; i < N; i++) { int pos = i; for (int j = 0; j < 3 && pos < N; j++) pos += cnt[pos]; ans = max(ans, pos - i); } printf("%lld\n", ans); return 0; }
[["-", 0, 11, 12, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
1
205
#include <iostream> #include <stdio.h> #define MAX_N 100000 int N; int a[MAX_N]; //??¶??? int p[MAX_N]; //?????????, a[i] = a[i + 1]??????i???????´? int p_tail; // p????????????????´? int main() { scanf("%d", &N); p[0] = -1; p_tail = 0; for (int i = 0; i < N; i++) { scanf("%d", &a[i]); if (i > 0 && a[i] == a[i - 1]) { p[p_tail + 1] = i - 1; p_tail++; } } p[p_tail + 1] = N - 1; p_tail++; // a[i + 3] - a[i]????????§????????§?????????????????¢??? if (p_tail <= 2) { printf("%d\n", N); } else { int ans = 0; for (int i = 0; i <= p_tail - 3; i++) { if (ans < a[i + 3] - a[i]) { ans = a[i + 3] - a[i]; } } printf("%d\n", ans); } return 0; }
#include <iostream> #include <stdio.h> #define MAX_N 100000 int N; int a[MAX_N]; //??¶??? int p[MAX_N]; //?????????, a[i] = a[i + 1]??????i???????´? int p_tail; // p????????????????´? int main() { scanf("%d", &N); p[0] = -1; p_tail = 0; for (int i = 0; i < N; i++) { scanf("%d", &a[i]); if (i > 0 && a[i] == a[i - 1]) { p[p_tail + 1] = i - 1; p_tail++; } } p[p_tail + 1] = N - 1; p_tail++; // a[i + 3] - a[i]????????§????????§?????????????????¢??? if (p_tail <= 2) { printf("%d\n", N); } else { int ans = 0; for (int i = 0; i <= p_tail - 3; i++) { if (ans < p[i + 3] - p[i]) { ans = p[i + 3] - p[i]; } } printf("%d\n", ans); } return 0; }
[["-", 15, 339, 51, 16, 12, 16, 31, 69, 28, 22], ["+", 15, 339, 51, 16, 12, 16, 31, 69, 28, 22], ["-", 15, 339, 51, 16, 12, 16, 12, 69, 28, 22], ["+", 15, 339, 51, 16, 12, 16, 12, 69, 28, 22], ["-", 0, 1, 0, 11, 12, 16, 31, 69, 28, 22], ["+", 0, 1, 0, 11, 12, 16, 31, 69, 28, 22], ["-", 0, 1, 0, 11, 12, 16, 12, 69, 28, 22], ["+", 0, 1, 0, 11, 12, 16, 12, 69, 28, 22]]
1
222
//============================================================================ // Name : AOJ.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; int main() { int N; int D[100000]; int R = 0; cin >> N; for (int i = 0; i < N; i++) { cin >> D[i]; } int a1 = 1, a2 = 1, a3 = 1; for (int i = 1; i < N; i++) { if (D[i] != D[i - 1]) { a1 += 1; a2 += 1; a3 += 1; } if (D[i] == D[i - 1]) { R = max(R, a3); a3 = a2 + 1; a2 = a1 + 1; a1 = 1; } } R = (R, a1); R = (R, a2); R = (R, a3); cout << R << endl; return 0; }
//============================================================================ // Name : AOJ.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; int main() { int N; int D[100000]; int R = 0; cin >> N; for (int i = 0; i < N; i++) { cin >> D[i]; } int a1 = 1, a2 = 1, a3 = 1; for (int i = 1; i < N; i++) { if (D[i] != D[i - 1]) { a1 += 1; a2 += 1; a3 += 1; } if (D[i] == D[i - 1]) { R = max(R, a3); a3 = a2 + 1; a2 = a1 + 1; a1 = 1; } } R = max(R, a1); R = max(R, a2); R = max(R, a3); cout << R << endl; return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22]]
1
191
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cctype> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> //#include<bits/stdc++.h> using namespace std; int r[100005]; int w[100005]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> r[i]; int a = r[0]; int p = 0; for (int i = 0; i < n - 1; i++) { int b = r[i + 1]; if (a != b) w[p]++; else p++; a = b; } if (n <= 2) { cout << n << endl; return 0; } int g = 0; for (int i = 0; i < n; i++) { int h = w[i] + w[i + 1] + w[i + 2]; g = max(g, h); } cout << g + 3 << endl; return 0; }
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cctype> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> //#include<bits/stdc++.h> using namespace std; int r[100005]; int w[100005]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> r[i]; int a = r[0]; int p = 0; for (int i = 0; i < n - 1; i++) { int b = r[i + 1]; if (a != b) w[p]++; else p++; a = b; } if (p <= 2) { cout << n << endl; return 0; } int g = 0; for (int i = 0; i < n; i++) { int h = w[i] + w[i + 1] + w[i + 2]; g = max(g, h); } cout << g + 3 << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
222
#include <bits/stdc++.h> #define ll long long #define INF 999999999 #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 = 100005; int a[MAX_N]; int n; vector<int> v; int main() { scanf("%d", &n); rep(i, n) { scanf("%d", &a[i]); } int i = 0; while (i < n) { int s = i; while ((a[i + 1] ^ a[i]) && i < n - 1) { i++; } i++; v.push_back(i - s); } if (v.size() == 1 || v.size() == 2) { printf("%d\n", n); } else { int rsum = v[0] + v[1] + v[2]; int ans = rsum; int i = 0; while (i + 3 < n) { rsum += v[i + 3]; rsum -= v[i]; ans = max(ans, rsum); i++; } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> #define ll long long #define INF 999999999 #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 = 100005; int a[MAX_N]; int n; vector<int> v; int main() { scanf("%d", &n); rep(i, n) { scanf("%d", &a[i]); } int i = 0; while (i < n) { int s = i; while ((a[i + 1] ^ a[i]) && i < n - 1) { i++; } i++; v.push_back(i - s); } if (v.size() == 1 || v.size() == 2) { printf("%d\n", n); } else { int rsum = v[0] + v[1] + v[2]; int ans = rsum; int i = 0; while (i + 3 < v.size()) { rsum += v[i + 3]; rsum -= v[i]; ans = max(ans, rsum); i++; } printf("%d\n", ans); } return 0; }
[["-", 0, 9, 0, 52, 15, 339, 51, 16, 12, 22], ["+", 15, 339, 51, 16, 12, 2, 63, 118, 28, 22], ["+", 15, 339, 51, 16, 12, 2, 63, 118, 17, 131], ["+", 15, 339, 51, 16, 12, 2, 63, 118, 119, 120], ["+", 15, 339, 51, 16, 12, 2, 3, 4, 0, 24], ["+", 15, 339, 51, 16, 12, 2, 3, 4, 0, 25]]
1
259
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<double, int> pdbi; typedef pair<int, pii> pipii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const ll INF = 1 << 30; const ll mod = 1000000000 + 7; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const double pi = 3.141592653589793; //#define int long long #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod int n; int a[100000 + 5]; vector<int> v; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, n) cin >> a[i]; bool check[n] = {}; rep(i, n) { if (check[i]) continue; check[i] = true; int now = i; int len = 1; while (now < n - 1 && a[now] != a[now + 1]) { check[now + 1] = true; now++; len++; } v.push_back(len); } int ans = 0; if (v.size() < 2) { cout << n << endl; return 0; } rep(i, v.size() - 2) ans = max(ans, v[i] + v[i + 1] + v[i + 2]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<double, int> pdbi; typedef pair<int, pii> pipii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const ll INF = 1 << 30; const ll mod = 1000000000 + 7; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const double pi = 3.141592653589793; //#define int long long #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod int n; int a[100000 + 5]; vector<int> v; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, n) cin >> a[i]; bool check[n] = {}; rep(i, n) { if (check[i]) continue; check[i] = true; int now = i; int len = 1; while (now < n - 1 && a[now] != a[now + 1]) { check[now + 1] = true; now++; len++; } v.push_back(len); } int ans = 0; if (v.size() <= 2) { cout << n << endl; return 0; } rep(i, v.size() - 2) ans = max(ans, v[i] + v[i + 1] + v[i + 2]); cout << ans << endl; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
519
#include <algorithm> #include <stdio.h> #include <string.h> using namespace std; char s[2001], t[2001]; int dp[2001][2001][2]; int m, n; int main() { scanf("%d %d", &m, &n); scanf("%s", s); scanf("%s", t); int ans = 0; memset(dp, 0, sizeof(dp)); for (int i = m; i >= 0; --i) { for (int j = n; j >= 0; --j) { dp[i][j][0] = max((s[i] == 'O') ? dp[i + 1][j][1] + 1 : 0, (t[j] == 'O') ? dp[i][j + 1][1] + 1 : 0); dp[i][j][1] = max((s[i] == 'I') ? dp[i + 1][j][0] + 1 : 0, (t[j] == 'I') ? dp[i][j + 1][0] + 1 : 0); if (s[i] == 'I' || t[i] == 'I') { dp[i][j][0] = max(dp[i][j][0], 1); dp[i][j][1] = max(dp[i][j][1], 1); } ans = max(dp[i][j][1], ans); } } printf("%d\n", (ans == 0) ? 0 : (ans - 1) / 2 * 2 + 1); return 0; }
#include <algorithm> #include <stdio.h> #include <string.h> using namespace std; char s[2001], t[2001]; int dp[2001][2001][2]; int m, n; int main() { scanf("%d %d", &m, &n); scanf("%s", s); scanf("%s", t); int ans = 0; memset(dp, 0, sizeof(dp)); for (int i = m; i >= 0; --i) { for (int j = n; j >= 0; --j) { dp[i][j][0] = max((s[i] == 'O') ? dp[i + 1][j][1] + 1 : 0, (t[j] == 'O') ? dp[i][j + 1][1] + 1 : 0); dp[i][j][1] = max((s[i] == 'I') ? dp[i + 1][j][0] + 1 : 0, (t[j] == 'I') ? dp[i][j + 1][0] + 1 : 0); if (s[i] == 'I' || t[j] == 'I') { dp[i][j][0] = max(dp[i][j][0], 1); dp[i][j][1] = max(dp[i][j][1], 1); } ans = max(dp[i][j][1], ans); } } printf("%d\n", (ans == 0) ? 0 : (ans - 1) / 2 * 2 + 1); return 0; }
[["-", 51, 16, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 16, 31, 69, 341, 342, 0, 22]]
1
389
#include <bits/stdc++.h> using namespace std; int n, m; string s, t; int dp[2020][2020][2]; int main() { cin >> m >> n; cin >> s >> t; int ans = -1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i) { if (s[i - 1] == 'I') { dp[i][j][1] = dp[i - 1][j][0] + 1; ans = max(ans, dp[i][j][1]); } else if (dp[i - 1][j][1]) { dp[i][j][0] = dp[i - 1][j][1] + 1; } } if (j) { if (t[j - 1] == 'I') { dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + 1); ans = max(ans, dp[i][j][1]); } else if (dp[i][j - 1][1]) { dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + 1); } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int n, m; string s, t; int dp[2020][2020][2]; int main() { cin >> m >> n; cin >> s >> t; int ans = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i) { if (s[i - 1] == 'I') { dp[i][j][1] = dp[i - 1][j][0] + 1; ans = max(ans, dp[i][j][1]); } else if (dp[i - 1][j][1]) { dp[i][j][0] = dp[i - 1][j][1] + 1; } } if (j) { if (t[j - 1] == 'I') { dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + 1); ans = max(ans, dp[i][j][1]); } else if (dp[i][j - 1][1]) { dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + 1); } } } } cout << ans << endl; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
335
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> //#define int long long #define inf 1145141919ll #define oku 1000000007ll using namespace std; int a, b, c, d; int dx[] = {0, -1, 0, 1, 0}; struct A { A *e; int temp; }; struct B { A *src; A *dst; int cost; }; struct C { int u; int v; int len; C(int x, int y, int z) : u(x), v(y), len(z) {} }; int i, j, k, n; int p[2000], q[2000], r[2][2001][2001]; signed main() { fill(&r[0][0][0], &r[0][0][0] + 2 * 2001 * 2001, -1); char ta[2]; ta[0] = 'O'; ta[1] = 'I'; cin >> a >> b; string s, t; cin >> s >> t; queue<C> qu; for (i = 0; i <= a; i++) { for (j = 0; j <= b; j++) { if (r[0][i][j]) continue; qu.push({i, j, 0}); while (!qu.empty()) { auto x = qu.front(); qu.pop(); if (r[x.len & 1][x.u][x.v] >= x.len) continue; else r[x.len & 1][x.u][x.v] = x.len; if (x.len & 1) n = max(n, x.len); if (x.u < a && s[x.u] == ta[(x.len & 1) ^ 1]) qu.push({x.u + 1, x.v, x.len + 1}); if (x.v < b && t[x.v] == ta[(x.len & 1) ^ 1]) qu.push({x.u, x.v + 1, x.len + 1}); } } } cout << n << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> //#define int long long #define inf 1145141919ll #define oku 1000000007ll using namespace std; int a, b, c, d; int dx[] = {0, -1, 0, 1, 0}; struct A { A *e; int temp; }; struct B { A *src; A *dst; int cost; }; struct C { int u; int v; int len; C(int x, int y, int z) : u(x), v(y), len(z) {} }; int i, j, k, n; int p[2000], q[2000], r[2][2001][2001]; signed main() { fill(&r[0][0][0], &r[0][0][0] + 2 * 2001 * 2001, -1); char ta[2]; ta[0] = 'O'; ta[1] = 'I'; cin >> a >> b; string s, t; cin >> s >> t; queue<C> qu; for (i = 0; i <= a; i++) { for (j = 0; j <= b; j++) { if (r[0][i][j] > 0) continue; qu.push({i, j, 0}); while (!qu.empty()) { auto x = qu.front(); qu.pop(); if (r[x.len & 1][x.u][x.v] >= x.len) continue; else r[x.len & 1][x.u][x.v] = x.len; if (x.len & 1) n = max(n, x.len); if (x.u < a && s[x.u] == ta[(x.len & 1) ^ 1]) qu.push({x.u + 1, x.v, x.len + 1}); if (x.v < b && t[x.v] == ta[(x.len & 1) ^ 1]) qu.push({x.u, x.v + 1, x.len + 1}); } } } cout << n << endl; }
[["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
510
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; int dp[2000][2000][2]; // a???n??????b???m????????§?????????(1,0)=(I,O) int a, b; string c, d; int main() { cin >> a >> b >> c >> d; c += " "; d += " "; for (int e = a + b - 1; e >= 0; e--) { for (int x = 0; x <= a; x++) { int y = e - x; if (y >= 0 && y <= b) { int I = 0, O = 0; if (c[x] == 'I') { I = dp[x + 1][y][0] + 1; } else if (c[x] == 'O') { O = dp[x + 1][y][1] + 1; } if (d[y] == 'I') { I = max(I, dp[x][y + 1][0] + 1); } else if (d[y] == 'O') { O = max(O, dp[x][y + 1][1] + 1); } dp[x][y][1] = I; dp[x][y][0] = O; } } } int MAX = 0; for (int p = 0; p <= a; p++) { for (int q = 0; q <= b; q++) { if (dp[p][q][1] & 1 == 0) dp[p][q][1]--; MAX = max(MAX, dp[p][q][1]); } } cout << MAX << endl; }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; int dp[2005][2005][2]; // a???n??????b???m????????§?????????(1,0)=(I,O) int a, b; string c, d; int main() { cin >> a >> b >> c >> d; c += " "; d += " "; for (int e = a + b - 1; e >= 0; e--) { for (int x = 0; x <= a; x++) { int y = e - x; if (y >= 0 && y <= b) { int I = 0, O = 0; if (c[x] == 'I') { I = dp[x + 1][y][0] + 1; } else if (c[x] == 'O') { O = dp[x + 1][y][1] + 1; } if (d[y] == 'I') { I = max(I, dp[x][y + 1][0] + 1); } else if (d[y] == 'O') { O = max(O, dp[x][y + 1][1] + 1); } dp[x][y][1] = I; dp[x][y][0] = O; } } } int MAX = 0; for (int p = 0; p <= a; p++) { for (int q = 0; q <= b; q++) { if ((dp[p][q][1] & 1) == 0) dp[p][q][1]--; MAX = max(MAX, dp[p][q][1]); } } cout << MAX << endl; }
[["-", 0, 43, 49, 80, 49, 80, 49, 80, 81, 13], ["+", 0, 43, 49, 80, 49, 80, 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, 7, 8, 9, 0, 57, 15, 339, 0, 24], ["+", 0, 57, 15, 339, 51, 16, 31, 23, 0, 25]]
1
381
#include <algorithm> #include <cstdlib> #include <iostream> using namespace std; int tl[2001][2001][2]; bool s[2001], t[2001]; int main() { int n, m; int i, j; int ma; char a; cin >> n >> m; for (i = 0; i < n; i++) { cin >> a; if (a == 'I') s[i] = true; else s[i] = false; } for (i = 0; i < m; i++) { cin >> a; if (a == 'I') t[i] = true; else t[i] = false; } for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { ma = 0; if (i > 0 && s[i - 1] == true) { ma = max(ma, tl[i - 1][j][0] + 1); } if (j > 0 && t[j - 1] == true) { ma = max(ma, tl[i][j - 1][0] + 1); } tl[i][j][1] = ma; ma = 0; if (i > 0 && s[i - 1] == false && tl[i - 1][j][1] > 0) { ma = max(ma, tl[i - 1][j][1] + 1); } if (i > 0 && t[j - 1] == false && tl[i][j - 1][1] > 0) { ma = max(ma, tl[i][j - 1][1] + 1); } tl[i][j][0] = ma; } } int cnt = 0; for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { cnt = max(cnt, tl[i][j][1]); } } cout << cnt << endl; return 0; }
#include <algorithm> #include <cstdlib> #include <iostream> using namespace std; int tl[2001][2001][2]; bool s[2001], t[2001]; int main() { int n, m; int i, j; int ma; char a; cin >> n >> m; for (i = 0; i < n; i++) { cin >> a; if (a == 'I') s[i] = true; else s[i] = false; } for (i = 0; i < m; i++) { cin >> a; if (a == 'I') t[i] = true; else t[i] = false; } for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { ma = 0; if (i > 0 && s[i - 1] == true) { ma = max(ma, tl[i - 1][j][0] + 1); } if (j > 0 && t[j - 1] == true) { ma = max(ma, tl[i][j - 1][0] + 1); } tl[i][j][1] = ma; ma = 0; if (i > 0 && s[i - 1] == false && tl[i - 1][j][1] > 0) { ma = max(ma, tl[i - 1][j][1] + 1); } if (j > 0 && t[j - 1] == false && tl[i][j - 1][1] > 0) { ma = max(ma, tl[i][j - 1][1] + 1); } tl[i][j][0] = ma; } } int cnt = 0; for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { cnt = max(cnt, tl[i][j][1]); } } cout << cnt << endl; return 0; }
[["-", 15, 339, 51, 16, 31, 16, 31, 16, 31, 22], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 31, 22]]
1
457
#include <bits/stdc++.h> #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 MAX 10001 #define B 100 #define dmp make_pair #define dpb push_back #define fi first #define se second #define P pair<int, int> typedef long long ll; const int inf = 1e9; const ll INF = 1e18; using namespace std; //__gcd(a,b), __builtin_popcount(a); /*chrono::system_clock::time_point start; void timerstart(){start = chrono::system_clock::now();} void timerend(){ auto end = chrono::system_clock::now(); int elapsed = chrono::duration_cast<chrono::milliseconds>(end-start).count(); printf("%d[ms]\n", elapsed); }*/ string s, t; int dp[2003][2003][3]; int main() { int n, m; cin >> n >> m >> s >> t; s = ' ' + s + 'x'; t = ' ' + t + 'x'; int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i + 1] == 'I') { dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][2] + 1); } if (t[j + 1] == 'I') { dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][2] + 1); } if (s[i + 1] == 'O') { if (dp[i][j][1] != 0) dp[i + 1][j][2] = max(dp[i + 1][j][2], dp[i][j][1] + 1); } if (t[j + 1] == 'O') { if (dp[i][j][1] != 0) dp[i][j + 1][2] = max(dp[i][j + 1][2], dp[i][j][1] + 1); } ans = max(ans, dp[i][j][1]); } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #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 MAX 10001 #define B 100 #define dmp make_pair #define dpb push_back #define fi first #define se second #define P pair<int, int> typedef long long ll; const int inf = 1e9; const ll INF = 1e18; using namespace std; //__gcd(a,b), __builtin_popcount(a); /*chrono::system_clock::time_point start; void timerstart(){start = chrono::system_clock::now();} void timerend(){ auto end = chrono::system_clock::now(); int elapsed = chrono::duration_cast<chrono::milliseconds>(end-start).count(); printf("%d[ms]\n", elapsed); }*/ string s, t; int dp[2003][2003][3]; int main() { int n, m; cin >> n >> m >> s >> t; s = ' ' + s + 'x'; t = ' ' + t + 'x'; int ans = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (s[i + 1] == 'I') { dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][2] + 1); } if (t[j + 1] == 'I') { dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][2] + 1); } if (s[i + 1] == 'O') { if (dp[i][j][1] != 0) dp[i + 1][j][2] = max(dp[i + 1][j][2], dp[i][j][1] + 1); } if (t[j + 1] == 'O') { if (dp[i][j][1] != 0) dp[i][j + 1][2] = max(dp[i][j + 1][2], dp[i][j][1] + 1); } ans = max(ans, dp[i][j][1]); } } printf("%d\n", ans); return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
458
#include <algorithm> #include <cstdio> #include <queue> #include <vector> using namespace std; int n, m, k; typedef pair<long long int, int> P; vector<P> x[100010], y[100010]; vector<P> g[400010]; long long int INF = 1e18; bool f; long long int d[400010]; priority_queue<P, vector<P>, greater<P>> q; int main() { scanf("%d %d %d", &m, &n, &k); for (int i = 0; i < k; ++i) { long long int a, b; scanf("%lld %lld", &a, &b); if ((a + b != n + m) || (a + b != 2)) { x[a].push_back(P(b, i + 1)); y[b].push_back(P(a, i + 1)); } else if (a + b == 2) f = true; } x[1].push_back(P(1, k + 1)); y[1].push_back(P(1, k + 1)); x[m].push_back(P(n, k + 2)); y[n].push_back(P(m, k + 2)); /* printf("\n"); for(int i=1; i<=m; ++i) for(int j=0; j<x[i].size(); ++j) printf("x=%d %dth y=%d %dth\n", i, j, x[i][j].first, x[i][j].second); printf("\n"); for(int i=1; i<=n; ++i) for(int j=0; j<y[i].size(); ++j) printf("%d %d %d %d\n", i, j, y[i][j].first, y[i][j].second); printf("\n"); */ for (int i = 1; i <= m; ++i) { sort(x[i].begin(), x[i].end()); for (int j = 0; j + 1 < x[i].size(); ++j) { g[x[i][j].second].push_back( P(x[i][j + 1].first - x[i][j].first, x[i][j + 1].second)); g[x[i][j + 1].second].push_back( P(x[i][j + 1].first - x[i][j].first, x[i][j].second)); } } for (int i = 1; i <= n; ++i) { sort(y[i].begin(), y[i].end()); for (int j = 0; j + 1 < y[i].size(); ++j) { g[y[i][j].second + k + 2].push_back( P(y[i][j + 1].first - y[i][j].first, y[i][j + 1].second + k + 2)); g[y[i][j + 1].second + k + 2].push_back( P(y[i][j + 1].first - y[i][j].first, y[i][j].second + k + 2)); } } for (int i = 1; i <= k; ++i) { g[i].push_back(P(1, i + k + 2)); g[i + k + 2].push_back(P(1, i)); } g[k + 1].push_back(P(1, 2 * k + 3)); g[2 * k + 3].push_back(P(1, k + 1)); g[k + 2].push_back(P(1, 2 * k + 4)); g[2 * k + 4].push_back(P(1, k + 2)); /* for(int i=1; i<=2*k+4; ++i){ for(int j=0; j<g[i].size(); ++j){ printf("cost=%d to=%d\n", g[i][j].first, g[i][j].second); } } */ fill(d + 1, d + 2 * k + 5, INF); d[k + 1] = 0; if (f) d[2 * k + 3] = 1; q.push(P(0, k + 1)); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < g[v].size(); ++i) { if (d[g[v][i].second] > d[v] + g[v][i].first) { d[g[v][i].second] = d[v] + g[v][i].first; q.push(P(d[g[v][i].second], g[v][i].second)); } } } /* printf("\n"); for(int i=1; i<=2*k+4; ++i) printf("%d\n", d[i]); printf("\n"); */ long long int ans = min(d[k + 2], d[2 * k + 4]); if (ans == INF) printf("-1\n"); else printf("%lld\n", ans); }
#include <algorithm> #include <cstdio> #include <queue> #include <vector> using namespace std; int n, m, k; typedef pair<long long int, int> P; vector<P> x[100010], y[100010]; vector<P> g[400010]; long long int INF = 1e18; bool f; long long int d[400010]; priority_queue<P, vector<P>, greater<P>> q; int main() { scanf("%d %d %d", &m, &n, &k); for (int i = 0; i < k; ++i) { long long int a, b; scanf("%lld %lld", &a, &b); if ((a + b != n + m) || (a + b != 2)) { x[a].push_back(P(b, i + 1)); y[b].push_back(P(a, i + 1)); } else if (a + b == 2) f = true; } x[1].push_back(P(1, k + 1)); y[1].push_back(P(1, k + 1)); x[m].push_back(P(n, k + 2)); y[n].push_back(P(m, k + 2)); /* printf("\n"); for(int i=1; i<=m; ++i) for(int j=0; j<x[i].size(); ++j) printf("x=%d %dth y=%d %dth\n", i, j, x[i][j].first, x[i][j].second); printf("\n"); for(int i=1; i<=n; ++i) for(int j=0; j<y[i].size(); ++j) printf("%d %d %d %d\n", i, j, y[i][j].first, y[i][j].second); printf("\n"); */ for (int i = 1; i <= m; ++i) { sort(x[i].begin(), x[i].end()); for (int j = 0; j + 1 < x[i].size(); ++j) { g[x[i][j].second].push_back( P(x[i][j + 1].first - x[i][j].first, x[i][j + 1].second)); g[x[i][j + 1].second].push_back( P(x[i][j + 1].first - x[i][j].first, x[i][j].second)); } } for (int i = 1; i <= n; ++i) { sort(y[i].begin(), y[i].end()); for (int j = 0; j + 1 < y[i].size(); ++j) { g[y[i][j].second + k + 2].push_back( P(y[i][j + 1].first - y[i][j].first, y[i][j + 1].second + k + 2)); g[y[i][j + 1].second + k + 2].push_back( P(y[i][j + 1].first - y[i][j].first, y[i][j].second + k + 2)); } } for (int i = 1; i <= k; ++i) { g[i].push_back(P(1, i + k + 2)); g[i + k + 2].push_back(P(1, i)); } if (f) { g[k + 1].push_back(P(1, 2 * k + 3)); g[2 * k + 3].push_back(P(1, k + 1)); } g[k + 2].push_back(P(1, 2 * k + 4)); g[2 * k + 4].push_back(P(1, k + 2)); /* for(int i=1; i<=2*k+4; ++i){ for(int j=0; j<g[i].size(); ++j){ printf("cost=%d to=%d\n", g[i][j].first, g[i][j].second); } } */ fill(d + 1, d + 2 * k + 5, INF); d[k + 1] = 0; if (f) d[2 * k + 3] = 1; q.push(P(0, k + 1)); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < g[v].size(); ++i) { if (d[g[v][i].second] > d[v] + g[v][i].first) { d[g[v][i].second] = d[v] + g[v][i].first; q.push(P(d[g[v][i].second], g[v][i].second)); } } } /* printf("\n"); for(int i=1; i<=2*k+4; ++i) printf("%d\n", d[i]); printf("\n"); */ long long int ans = min(d[k + 2], d[2 * k + 4]); if (ans == INF) printf("-1\n"); else printf("%lld\n", ans); }
[["+", 0, 30, 0, 14, 8, 9, 0, 57, 0, 121], ["+", 0, 14, 8, 9, 0, 57, 15, 339, 0, 24], ["+", 0, 14, 8, 9, 0, 57, 15, 339, 51, 22], ["+", 0, 14, 8, 9, 0, 57, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 14, 8, 9, 0, 57, 64, 9, 0, 46]]
1
1,039
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #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 a[4]; int b[2]; int res = 0; REP(i, 4) { cin >> a[i]; } cin >> b[0] >> b[1]; sort(a, a + 4); sort(b, b + 2); cout << a[2] + a[3] + b[1] << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #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 a[4]; int b[2]; int res = 0; REP(i, 4) { cin >> a[i]; } cin >> b[0] >> b[1]; sort(a, a + 4); sort(b, b + 2); cout << a[1] + a[2] + a[3] + b[1] << endl; return 0; }
[["+", 31, 16, 31, 16, 31, 69, 341, 342, 0, 13], ["+", 31, 16, 31, 16, 31, 69, 341, 342, 0, 73], ["+", 31, 16, 12, 16, 31, 16, 31, 16, 17, 72], ["+", 12, 16, 31, 16, 31, 16, 12, 69, 28, 22], ["+", 31, 16, 31, 16, 12, 69, 341, 342, 0, 70]]
1
358
#include <algorithm> #include <iostream> using namespace std; int main() { int a[6]; for (int i = 0; i < 6; i++) { cin >> a[i]; } sort(a, a + 4); sort(a + 5, a + 6); cout << a[1] + a[2] + a[3] + a[5] << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int a[6]; for (int i = 0; i < 6; i++) { cin >> a[i]; } sort(a, a + 4); sort(a + 4, a + 6); cout << a[1] + a[2] + a[3] + a[5] << endl; return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
90
#include <algorithm> #include <functional> #include <iostream> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; #define FOR(i, a) for (int i = 0; i < a; i++) int main() { int a[4]; int b[2]; FOR(i, 4) { cin >> a[i]; } FOR(i, 2) { cin >> b[i]; } sort(a, a + 4, greater<int>()); sort(b, b + 2, greater<int>()); cout << a[0] + a[1] + b[0] << endl; }
#include <algorithm> #include <functional> #include <iostream> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; #define FOR(i, a) for (int i = 0; i < a; i++) int main() { int a[4]; int b[2]; FOR(i, 4) { cin >> a[i]; } FOR(i, 2) { cin >> b[i]; } sort(a, a + 4, greater<int>()); sort(b, b + 2, greater<int>()); cout << a[0] + a[1] + a[2] + b[0] << endl; }
[["+", 31, 16, 12, 16, 31, 16, 12, 69, 28, 22], ["+", 12, 16, 31, 16, 12, 69, 341, 342, 0, 70], ["+", 12, 16, 31, 16, 12, 69, 341, 342, 0, 13], ["+", 12, 16, 31, 16, 12, 69, 341, 342, 0, 73], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72]]
1
131
#include <iostream> using namespace std; int main() { int score[6]; for (int i = 0; i < 6; i++) { cin >> score[i]; } int sum = 0; for (int i = 0; i < 6; i++) { sum += score[i]; } int mini; mini = score[0]; for (int i = 0; i < 3; i++) { if (score[i] < mini) mini = score[i]; } sum -= mini; if (score[4] < score[5]) mini = score[4]; else mini = score[5]; sum -= mini; cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main() { int score[6]; for (int i = 0; i < 6; i++) { cin >> score[i]; } int sum = 0; for (int i = 0; i < 6; i++) { sum += score[i]; } int mini; mini = score[0]; for (int i = 0; i <= 3; i++) { if (score[i] < mini) mini = score[i]; } sum -= mini; if (score[4] < score[5]) mini = score[4]; else mini = score[5]; sum -= mini; cout << sum << endl; return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19]]
1
155
//============================================================================ // Name : AOL.cpp // Author : Akari Fujii // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; int main() { int a, b, c, d, e, f, g, s, aka; cin >> a >> b >> c >> d >> e >> f; g = a + b + c + d; aka = a; if (a > b) aka = b; if (b > c) aka = c; if (c > d) aka = d; g = g - aka; s = e; if (e < f) s = f; cout << g + s << endl; }
//============================================================================ // Name : AOL.cpp // Author : Akari Fujii // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; int main() { int a, b, c, d, e, f, g, s, aka; cin >> a >> b >> c >> d >> e >> f; g = a + b + c + d; aka = a; if (a > b) aka = b; if (aka > c) aka = c; if (aka > d) aka = d; g = g - aka; s = e; if (e < f) s = f; cout << g + s << endl; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
124
#include <algorithm> #include <cstdio> #include <iostream> int main() { int a[10]; int m; for (int i = 0; i <= 5; i++) { std::cin >> a[i]; } std::sort(a, a + 3); m = std::max(a[4], a[5]); std::cout << a[3] + a[2] + a[1] + m << std::endl; return 0; }
#include <algorithm> #include <cstdio> #include <iostream> int main() { int a[10]; int m; for (int i = 0; i <= 5; i++) { std::cin >> a[i]; } std::sort(a, a + 4); m = std::max(a[4], a[5]); std::cout << a[3] + a[2] + a[1] + m << std::endl; return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
102
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> #define INF 2000000000 using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; #define ll long long #define PL pair<long long, long long> #define P pair<int, int> #define mk make_pair #define en endl; #define WHITE 0 #define BLACK 2 #define GRAY 1 #define RE return 0; #define HERE \ cout << " " \ << "HERE" << en int main() { int a, b, c, d, e, h; cin >> a >> b >> c >> d >> e >> h; int sum = a + b + c + d + e; int f = min(a, b); f = min(f, c); f = min(f, d); int g = min(h, e); cout << sum - g - f << en }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> #define INF 2000000000 using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; #define ll long long #define PL pair<long long, long long> #define P pair<int, int> #define mk make_pair #define en endl; #define WHITE 0 #define BLACK 2 #define GRAY 1 #define RE return 0; #define HERE \ cout << " " \ << "HERE" << en int main() { int a, b, c, d, e, h; cin >> a >> b >> c >> d >> e >> h; int sum = a + b + c + d + e + h; int f = min(a, b); f = min(f, c); f = min(f, d); int g = min(h, e); cout << sum - g - f << en }
[["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 72], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 22]]
1
199
A = int(input()) B = int(input()) C = int(input()) D = int(input()) E = int(input()) F = int(input()) print(sum(sorted([A, B, C, D])[:3]) + max(E, F))
A = int(input()) B = int(input()) C = int(input()) D = int(input()) E = int(input()) F = int(input()) print(sum(sorted([A, B, C, D], reverse=True)[:3]) + max(E, F))
[["+", 3, 4, 0, 206, 51, 652, 3, 4, 0, 21], ["+", 0, 206, 51, 652, 3, 4, 0, 653, 141, 22], ["+", 0, 206, 51, 652, 3, 4, 0, 653, 0, 32], ["+", 0, 206, 51, 652, 3, 4, 0, 653, 51, 146]]
5
77
#include <algorithm> #include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; int A[N]; for (int i = 0; i < N; ++i) { cin >> A[i]; } for (int k = 1; k < M + 1; ++k) { for (int a = 0; a < N; ++a) { if (1 <= a && a < N - 1) { if (A[a] % k > A[a + 1] % k) { int temp = A[a]; A[a] = A[a + 1]; A[a + 1] = temp; } else { } } else { break; } } } for (int w = 0; w < N; ++w) { cout << A[w] << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; int A[N]; for (int i = 0; i < N; ++i) { cin >> A[i]; } for (int k = 1; k < M + 1; ++k) { for (int a = 0; a < N; ++a) { if (0 <= a && a < N - 1) { if (A[a] % k > A[a + 1] % k) { int temp = A[a]; A[a] = A[a + 1]; A[a + 1] = temp; } else { } } else { break; } } } for (int w = 0; w < N; ++w) { cout << A[w] << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 31, 13], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 13]]
1
187
#include <iostream> using namespace std; int main() { int n, m, i, a[100], j, v, r, t; cin >> n >> m; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 1; i <= m; i++) { for (j = 1; j < n - 1; j++) { if (j <= n) { v = a[j] % i; r = a[j + 1] % i; if (v > r) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } for (i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n, m, i, a[100], j, v, r, t; cin >> n >> m; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 1; i <= m; i++) { for (j = 0; j < n - 1; j++) { if (j <= n) { v = a[j] % i; r = a[j + 1] % i; if (v > r) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } for (i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13]]
1
183
// 0620 #include <stdio.h> int main(void) { int a[101], n, m, i, b, t; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= m; i++) { b = 1; while (b != n) { if (a[b] % i > a[b + 1] % i) { t = a[b]; a[b] = a[b + 1]; a[b + 1] = t; } b++; } } for (i = 1; i <= n; i++) { printf("%d\n", a[i]); } return 0; }
// 0620 #include <stdio.h> int main(void) { int a[101], n, m, i, b, t; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= m; i++) { b = 1; while (b != n) { if (a[b] % i > a[b + 1] % i) { t = a[b]; a[b] = a[b + 1]; a[b + 1] = t; } b++; } } for (i = 1; i <= n; i++) { printf("%d\n", a[i]); } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19]]
1
176
#include <algorithm> #include <cstdlib> #include <iostream> #include <math.h> #include <stdlib.h> #include <vector> using namespace std; int a, b, ans = 0, W = 0, B = 0, R = 0; int tansaku(int iti, int ryo, vector<string> c) { int kariAns = 0; for (int i = 0; i < b; i++) { for (int j = 0; j < ryo; j++) { if (c[iti + j][i] != 'B') kariAns++; } for (int j = 1; j < iti; j++) { if (c[iti - j][i] != 'W') kariAns++; } for (int j = iti + ryo; j < a - 1; j++) { if (c[j][i] != 'R') kariAns++; } } if (iti == a - 1) { iti = 1; ryo++; } else { iti++; } if (ryo >= a - 1) return kariAns; return min(kariAns, tansaku(iti, ryo, c)); } int main() { cin >> a >> b; vector<string> c(a); for (int i = 0; i < a; i++) { cin >> c[i]; } for (int i = 0; i < b; i++) { if (c[0][i] != 'W') ans++; if (c[c.size() - 1][i] != 'R') ans++; } cout << ans + tansaku(1, 1, c) << endl; }
#include <algorithm> #include <cstdlib> #include <iostream> #include <math.h> #include <stdlib.h> #include <vector> using namespace std; int a, b, ans = 0, W = 0, B = 0, R = 0; int tansaku(int iti, int ryo, vector<string> c) { int kariAns = 0; for (int i = 0; i < b; i++) { for (int j = 0; j < ryo; j++) { if (c[iti + j][i] != 'B') kariAns++; } for (int j = 1; j < iti; j++) { if (c[iti - j][i] != 'W') kariAns++; } for (int j = iti + ryo; j < a - 1; j++) { if (c[j][i] != 'R') kariAns++; } } if (iti + ryo == a - 1) { iti = 1; ryo++; } else { iti++; } if (ryo >= a - 1) return kariAns; return min(kariAns, tansaku(iti, ryo, c)); } int main() { cin >> a >> b; vector<string> c(a); for (int i = 0; i < a; i++) { cin >> c[i]; } for (int i = 0; i < b; i++) { if (c[0][i] != 'W') ans++; if (c[c.size() - 1][i] != 'R') ans++; } cout << ans + tansaku(1, 1, c) << endl; }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 72], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 22]]
1
346
#include <bits/stdc++.h> using namespace std; int russia[52][52]; int main() { int n, m; cin >> n >> m; int c = n * m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> russia[i][j]; } } int d = 0; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - i; j++) { for (int p = 0; p < i; p++) { for (int l = 0; l < m; l++) { if (russia[p][l] != 'W') { d++; } } } for (int p = i; p < i + j; p++) { for (int l = 0; l < m; l++) { if (russia[p][l] != 'B') { d++; } } } for (int p = i + j; p < n; p++) { for (int l = 0; l < m; l++) { if (russia[p][l] != 'R') { d++; } } } if (c > d) { c = d; } d = 0; } } cout << c << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char russia[52][52]; int main() { int n, m; cin >> n >> m; int c = n * m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> russia[i][j]; } } int d = 0; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - i; j++) { for (int p = 0; p < i; p++) { for (int l = 0; l < m; l++) { if (russia[p][l] != 'W') { d++; } } } for (int p = i; p < i + j; p++) { for (int l = 0; l < m; l++) { if (russia[p][l] != 'B') { d++; } } } for (int p = i + j; p < n; p++) { for (int l = 0; l < m; l++) { if (russia[p][l] != 'R') { d++; } } } if (c > d) { c = d; } d = 0; } } cout << c << endl; return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40]]
1
304
#include <algorithm> #include <deque> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; const long long mod = 1000000007; const long long inf = mod * mod; const long long d2 = 500000004; char str[120][110]; int cnt[120][3]; int main() { int a, b; scanf("%d%d", &a, &b); for (int i = 0; i < a; i++) scanf("%s", str[i]); for (int i = 0; i < a; i++) for (int j = 0; j < b; j++) { if (str[i][j] == 'W') cnt[i][0]++; if (str[i][j] == 'B') cnt[i][1]++; if (str[i][j] == 'R') cnt[i][2]++; } int ret = mod; for (int i = 0; i < a; i++) { for (int j = i + 1; j < a; j++) { int tmp = 0; for (int l = 0; l < a; l++) { if (l <= i) tmp += b - cnt[l][0]; else if (l <= j) tmp += b - cnt[l][1]; else tmp += b - cnt[l][2]; } ret = min(ret, tmp); } } printf("%d\n", ret); }
#include <algorithm> #include <deque> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; const long long mod = 1000000007; const long long inf = mod * mod; const long long d2 = 500000004; char str[120][110]; int cnt[120][3]; int main() { int a, b; scanf("%d%d", &a, &b); for (int i = 0; i < a; i++) scanf("%s", str[i]); for (int i = 0; i < a; i++) for (int j = 0; j < b; j++) { if (str[i][j] == 'W') cnt[i][0]++; if (str[i][j] == 'B') cnt[i][1]++; if (str[i][j] == 'R') cnt[i][2]++; } int ret = mod; for (int i = 0; i < a; i++) { for (int j = i + 1; j < a - 1; j++) { int tmp = 0; for (int l = 0; l < a; l++) { if (l <= i) tmp += b - cnt[l][0]; else if (l <= j) tmp += b - cnt[l][1]; else tmp += b - cnt[l][2]; } ret = min(ret, tmp); } } printf("%d\n", ret); }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
345
#include <bits/stdc++.h> using namespace std; int kou, sou = 114514810, H, W; int main() { cin >> H >> W; char HW[H][W]; for (int i = 0; i < H; i++) { for (int o = 0; o < W; o++) { scanf(" %c", &HW[i][o]); } } for (int i = 1; i < H - 2; i++) { for (int j = 1; j < H - 2; j++) { kou = 0; for (int o = 0; o < i; o++) { for (int f = 0; f < W; f++) { if (HW[o][f] != 'W') { kou++; } } } for (int o = i; o < i + j; o++) { for (int f = 0; f < W; f++) { if (HW[o][f] != 'B') { kou++; } } } for (int o = i + j; o < H; o++) { for (int f = 0; f < W; f++) { if (HW[o][f] != 'R') { kou++; } } } if (kou < sou) { sou = kou; } } } cout << sou << endl; }
#include <bits/stdc++.h> using namespace std; int kou, sou = 114514810, H, W; int main() { cin >> H >> W; char HW[H][W]; for (int i = 0; i < H; i++) { for (int o = 0; o < W; o++) { scanf(" %c", &HW[i][o]); } } for (int i = 1; i < H - 1; i++) { for (int j = 1; j < H - i; j++) { kou = 0; for (int o = 0; o < i; o++) { for (int f = 0; f < W; f++) { if (HW[o][f] != 'W') { kou++; } } } for (int o = i; o < i + j; o++) { for (int f = 0; f < W; f++) { if (HW[o][f] != 'B') { kou++; } } } for (int o = i + j; o < H; o++) { for (int f = 0; f < W; f++) { if (HW[o][f] != 'R') { kou++; } } } if (kou < sou) { sou = kou; } } } cout << sou << endl; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 22]]
1
301
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int kos[550], ned[550], man[11000]; int data[10050]; cin >> m >> n; for (int i = 0; i < m; i++) cin >> man[i]; for (int i = 0; i < n; i++) cin >> kos[i] >> ned[i]; sort(man, man + n); reverse(man, man + n); for (int i = 0; i < m; i++) data[i] = 110110110; data[0] = 0; for (int j = 0; j < n; j++) { for (int i = m; i >= 0; --i) { data[i] = min(data[max(i - kos[j], 0)] + ned[j], data[i]); } } int jo = 0; for (int i = 0; i < m; i++) { jo += man[i]; data[i] -= jo; } int ans = 0; for (int i = 0; i < m; i++) ans = min(data[i], ans); cout << abs(ans) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int kos[550], ned[550], man[11000]; int data[10050]; cin >> m >> n; for (int i = 0; i < m; i++) cin >> man[i]; for (int i = 0; i < n; i++) cin >> kos[i] >> ned[i]; sort(man, man + m); reverse(man, man + m); for (int i = 0; i <= m; i++) data[i] = 110110110; data[0] = 0; for (int j = 0; j < n; j++) { for (int i = m; i >= 0; --i) { data[i] = min(data[max(i - kos[j], 0)] + ned[j], data[i]); } } // for(int i=0;i<=m;i++)cout<<data[i]<<endl; int jo = 0; for (int i = 0; i < m; i++) { jo += man[i]; data[i + 1] -= jo; } int ans = 0; for (int i = 0; i <= m; i++) ans = min(data[i], ans); cout << abs(ans) << endl; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
278
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int INF = 1 << 30; const long long LINF = 1e18; const int EPS = 1e-8; #define PB push_back #define mk make_pair #define fr first #define sc second #define ll long long #define reps(i, j, k) for (int i = (j); i < (k); i++) #define rep(i, j) reps(i, 0, j) #define MOD 1000000007 typedef pair<int, int> Pii; typedef vector<int> vi; typedef vector<vi> vvi; int M, N, manj[10024]; Pii box[512]; int dp[512][10024]; int main() { scanf("%d%d", &M, &N); int sum[10024] = {}; rep(i, M) { scanf("%d", &manj[i]); } sort(manj, manj + M); reverse(manj, manj + M); sum[0] = 0; reps(i, 1, M + 1) { sum[i] += manj[i - 1] + sum[i - 1]; } rep(i, N) { int c, e; scanf("%d%d", &c, &e); box[i].fr = c; box[i].sc = e; } rep(i, N + 1) { rep(j, M + 1) { dp[i][j] = INF; if (j == 0) { dp[i][j] = 0; } } } reps(i, 1, N + 1) { rep(j, M + 1) { dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0, j - box[i - 1].fr)] + box[i - 1].sc); } } /*puts("*********************"); rep(i,N+1){ rep(j,M+1){ if(dp[i][j] == INF){ printf("INF "); }else{ printf("%d ",dp[i][j]); } } puts(""); }*/ int ans = 0; rep(i, M) { ans = max(ans, sum[i] - dp[N][i]); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int INF = 1 << 30; const long long LINF = 1e18; const int EPS = 1e-8; #define PB push_back #define mk make_pair #define fr first #define sc second #define ll long long #define reps(i, j, k) for (int i = (j); i < (k); i++) #define rep(i, j) reps(i, 0, j) #define MOD 1000000007 typedef pair<int, int> Pii; typedef vector<int> vi; typedef vector<vi> vvi; int M, N, manj[10024]; Pii box[512]; int dp[512][10024]; int main() { scanf("%d%d", &M, &N); int sum[10024] = {}; rep(i, M) { scanf("%d", &manj[i]); } sort(manj, manj + M); reverse(manj, manj + M); sum[0] = 0; reps(i, 1, M + 1) { sum[i] += manj[i - 1] + sum[i - 1]; } /*rep(i,M+1){ printf("%d ",sum[i]); } puts("");*/ rep(i, N) { int c, e; scanf("%d%d", &c, &e); box[i].fr = c; box[i].sc = e; } rep(i, N + 1) { rep(j, M + 1) { dp[i][j] = INF; if (j == 0) { dp[i][j] = 0; } } } reps(i, 1, N + 1) { rep(j, M + 1) { dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0, j - box[i - 1].fr)] + box[i - 1].sc); } } /*puts("*********************"); rep(i,N+1){ rep(j,M+1){ if(dp[i][j] == INF){ printf("INF "); }else{ printf("%d ",dp[i][j]); } } puts(""); }*/ int ans = 0; rep(i, M + 1) { ans = max(ans, sum[i] - dp[N][i]); } printf("%d\n", ans); return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
484
#include <algorithm> #include <functional> #include <iostream> using namespace std; #define INF 1000000000 int man[10010], kaz[510], ned[510], dp[2][10010]; int main() { int M, N, sum, a, b, cnt, hog; cin >> M >> N; for (int i = 0; i < M; i++) cin >> man[i]; for (int i = 0; i < N; i++) cin >> kaz[i] >> ned[i]; for (int i = 0; i <= M; i++) dp[0][i] = INF; dp[0][0] = 0; sort(man, man + M, greater<int>()); for (int i = 0; i < N; i++) { a = i % 2; b = (i + 1) % 2; for (int j = 0; j <= M; j++) { dp[b][j] = dp[a][j]; } for (int j = 0; j < M; j++) { hog = max(j + kaz[i], M); dp[b][hog] = min(dp[b][hog], dp[a][j] + ned[i]); } } b = N % 2; sum = cnt = 0; for (int i = 1; i <= M; i++) { sum += man[i - 1]; cnt = max(cnt, sum - dp[b][i]); // cout<<i<<" "<<sum<<" "<<dp[b][i]<<endl;// } cout << cnt << endl; }
#include <algorithm> #include <functional> #include <iostream> using namespace std; #define INF 1000000000 int man[10010], kaz[510], ned[510], dp[2][10010]; int main() { int M, N, sum, a, b, cnt, hog; cin >> M >> N; for (int i = 0; i < M; i++) cin >> man[i]; for (int i = 0; i < N; i++) cin >> kaz[i] >> ned[i]; for (int i = 0; i <= M; i++) dp[0][i] = INF; dp[0][0] = 0; sort(man, man + M, greater<int>()); for (int i = 0; i < N; i++) { a = i % 2; b = (i + 1) % 2; for (int j = 0; j <= M; j++) { dp[b][j] = dp[a][j]; } for (int j = 0; j < M; j++) { hog = min(j + kaz[i], M); dp[b][hog] = min(dp[b][hog], dp[a][j] + ned[i]); } } b = N % 2; sum = cnt = 0; for (int i = 1; i <= M; i++) { sum += man[i - 1]; cnt = max(cnt, sum - dp[b][i]); // cout<<i<<" "<<sum<<" "<<dp[b][i]<<endl;// } cout << cnt << endl; }
[["-", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 2, 63, 22]]
1
348
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef double db; #define fr first #define sc second #define pb push_back #define rep(i, x) for (ll i = 0; i < x; i++) #define rep1(i, x) for (ll i = 1; i <= x; i++) #define rrep(i, x) for (ll i = x - 1; i >= 0; i--) #define rrep1(i, x) for (ll i = x; i > 0; i--) ll n, m; ll p[10001], c[500], e[500]; ll dp[10002]; const ll INF = 0xffffffffffffff; int main() { cin >> m >> n; rep(i, m) cin >> p[i]; sort(p, p + m, [](ll a, ll b) { return a > b; }); fill(dp, dp + m + 2, INF); dp[0] = 0; rep(i, n) cin >> c[i] >> e[i]; rep(i, n) { rrep(j, 10001) { dp[j] = min(dp[j], min(dp[j + 1], j - c[i] >= 0 ? dp[j - c[i]] + e[i] : INF)); } // rrep(j,m+1-c[i]){ // dp[j+c[i]]=min(dp[j+c[i]],dp[j]+e[i]); //} } ll ans = 0, sum = 0; rep(i, m) { // cout<<dp[i]<<endl; sum += p[i]; ans = max(ans, sum - dp[i + 1]); } // cout<<dp[m]<<endl; cout << ans << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef double db; #define fr first #define sc second #define pb push_back #define rep(i, x) for (ll i = 0; i < x; i++) #define rep1(i, x) for (ll i = 1; i <= x; i++) #define rrep(i, x) for (ll i = x - 1; i >= 0; i--) #define rrep1(i, x) for (ll i = x; i > 0; i--) ll n, m; ll p[10001], c[500], e[500]; ll dp[10002]; const ll INF = 0xffffffffffffff; int main() { cin >> m >> n; rep(i, m) cin >> p[i]; sort(p, p + m, [](ll a, ll b) { return a > b; }); fill(dp, dp + 10002, INF); dp[0] = 0; rep(i, n) cin >> c[i] >> e[i]; rep(i, n) { rrep(j, 10001) { dp[j] = min(dp[j], min(dp[j + 1], j - c[i] >= 0 ? dp[j - c[i]] + e[i] : INF)); } // rrep(j,m+1-c[i]){ // dp[j+c[i]]=min(dp[j+c[i]],dp[j]+e[i]); //} } ll ans = 0, sum = 0; rep(i, m) { // cout<<dp[i]<<endl; sum += p[i]; ans = max(ans, sum - dp[i + 1]); } // cout<<dp[m]<<endl; cout << ans << endl; }
[["-", 0, 2, 3, 4, 0, 16, 31, 16, 12, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
324
#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; } int in() { int a; scanf("%d", &a); return a; } ll lin() { ll a; scanf("%lld", &a); return a; } constexpr int inf = 1e9; constexpr ll linf = 3e18; constexpr double eps = 1e-9; int n; int A[100010]; ll sum[100010]; bool check(int i, int x, bool f = false) { int nowidx = i; ll cur = i == 0 ? 0 : sum[i - 1]; rep(_, 2) { nowidx = lower_bound(sum, sum + i + n, cur + x) - sum; if (nowidx == i + n) return false; cur = sum[nowidx]; } return sum[i + n - 1] - sum[nowidx] >= x; } int main() { n = in(); rep(i, n) { A[i] = in(); sum[i] = A[i]; if (i) sum[i] += sum[i - 1]; } rep(i, n - 1) { sum[i + n] = A[i] + sum[i + n - 1]; } ll ma = 0; rep(i, n) { ll l = 0, r = linf; rep(_, 35) { ll mid = (l + r) / 2; (check(i, mid) ? l : r) = mid; } chmax(ma, l); } printf("%lld\n", ma); }
#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; } int in() { int a; scanf("%d", &a); return a; } ll lin() { ll a; scanf("%lld", &a); return a; } constexpr int inf = 1e9; constexpr ll linf = 3e18; constexpr double eps = 1e-9; int n; int A[100010]; ll sum[200010]; bool check(int i, ll x, bool f = false) { int nowidx = i; ll cur = i == 0 ? 0 : sum[i - 1]; rep(_, 2) { nowidx = lower_bound(sum, sum + i + n, cur + x) - sum; if (nowidx == i + n) return false; cur = sum[nowidx]; } return sum[i + n - 1] - sum[nowidx] >= x; } int main() { n = in(); rep(i, n) { A[i] = in(); sum[i] = A[i]; if (i) sum[i] += sum[i - 1]; } rep(i, n - 1) { sum[i + n] = A[i] + sum[i + n - 1]; } ll ma = 0; rep(i, n) { ll l = 0, r = linf; rep(_, 65) { ll mid = (l + r) / 2; (check(i, mid) ? l : r) = mid; } chmax(ma, l); } printf("%lld\n", ma); }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 40], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
707
def lower_bound(arr, v) l = -1; r = arr.size while l + 1 < r m = (l + r) / 2 if arr[m] <= v then l = m else r = m end end l end n = gets.to_i arr = [] n.times{ arr << gets.to_i } sum = Array.new(2*n+1, 0) (2*n).times{ |i| sum[i+1] += sum[i] + arr[i%n] } l = 0; r = sum[n] / 3 while l + 1 < r m = (l + r) / 2 f = nil n.times do |i| a = lower_bound(sum[i,n], m + sum[i]) + i + 1 if a < i+n b = lower_bound(sum[a...i+n], m + sum[a]) + a + 1 if b < i+n && sum[i+n] - sum[b] >= m l = m f = true break end end end r = m unless f end p l
def lower_bound(arr, v) l = -1; r = arr.size while l + 1 < r m = (l + r) / 2 if arr[m] <= v then l = m else r = m end end l end n = gets.to_i arr = [] n.times{ arr << gets.to_i } sum = Array.new(2*n+1, 0) (2*n).times{ |i| sum[i+1] += sum[i] + arr[i%n] } l = 0; r = sum[n] / 3 + 1 while l + 1 < r m = (l + r) / 2 f = nil n.times do |i| a = lower_bound(sum[i,n], m + sum[i] - 1) + i + 1 if a < i+n b = lower_bound(sum[a...i+n], m + sum[a] - 1) + a + 1 if b < i+n && sum[i+n] - sum[b] >= m l = m f = true break end end end r = m unless f end p l
[["+", 36, 36, 0, 493, 0, 662, 12, 738, 17, 72], ["+", 36, 36, 0, 493, 0, 662, 12, 738, 12, 612], ["+", 31, 738, 31, 652, 3, 4, 0, 738, 17, 33], ["+", 31, 738, 31, 652, 3, 4, 0, 738, 12, 612]]
4
241
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define llong long long #define pb(a) push_back(a) #define INF 999999999 using namespace std; typedef pair<int, int> P; typedef pair<llong, llong> LP; typedef pair<int, P> PP; typedef pair<llong, LP> LPP; // int dy[]={0, 0, 1, -1, 0}; // int dx[]={1, -1, 0, 0, 0}; #define int long long vector<vector<int>> dp(5000, vector<int>(5000, -1)); vector<int> v(6000); int n; int dfs(int l, int r) { if (dp[l][r] != -1) return dp[l][r]; int ret; if (r - l - 1 == n) ret = 0; else if ((l - r - 1) & 1) { if (v[l] > v[r]) { ret = dfs(l - 1, r); } else { ret = dfs(l, r + 1); } } else { ret = max(dfs(l - 1, r) + v[l], dfs(l, r + 1) + v[r]); } return dp[l][r] = ret; } signed main() { scanf("%d", &n); REP(i, n) { int a; scanf("%d", &a); v[i] = v[n + i] = v[n * 2 + i] = a; } int ans = 0; REP(i, n) { ans = max(ans, dfs(i + n - 1, i + n + 1) + v[i]); } printf("%d\n", ans); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define llong long long #define pb(a) push_back(a) #define INF 999999999 using namespace std; typedef pair<int, int> P; typedef pair<llong, llong> LP; typedef pair<int, P> PP; typedef pair<llong, LP> LPP; // int dy[]={0, 0, 1, -1, 0}; // int dx[]={1, -1, 0, 0, 0}; #define int long long vector<vector<int>> dp(5000, vector<int>(5000, -1)); vector<int> v(6000); int n; int dfs(int l, int r) { if (dp[l][r] != -1) return dp[l][r]; int ret; if (r - l - 1 == n) ret = 0; else if ((l - r - 1) & 1) { if (v[l] > v[r]) { ret = dfs(l - 1, r); } else { ret = dfs(l, r + 1); } } else { ret = max(dfs(l - 1, r) + v[l], dfs(l, r + 1) + v[r]); } return dp[l][r] = ret; } signed main() { scanf("%lld", &n); REP(i, n) { int a; scanf("%lld", &a); v[i] = v[n + i] = v[n * 2 + i] = a; } int ans = 0; REP(i, n) { ans = max(ans, dfs(i + n - 1, i + n + 1) + v[i]); } printf("%lld\n", ans); }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
421
#include <iostream> #define int long long using namespace std; signed main() { int n, a[2000]; int dp[2000][2000] = {}; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (n % 2) dp[i][i] = a[i]; } for (int i = 1; i < n; i++) { for (int j = 0; j < n; j++) { if ((n - i) % 2) dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n], dp[(j + n + 1) % n][(j + i) % n] + a[j]); else if (a[j] > a[(j + i) % n]) { dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n]; } else { dp[j][(j + i) % n] = dp[j][(j + i - 1) % n]; } } } int ma = 0; for (int i = 0; i < n; i++) ma = max(dp[i][(i + n - 1) % n], ma); cout << ma << endl; return 0; }
#include <iostream> #define int long long using namespace std; signed main() { int n, a[2000]; static int dp[2000][2000] = {}; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (n % 2) dp[i][i] = a[i]; } for (int i = 1; i < n; i++) { for (int j = 0; j < n; j++) { if ((n - i) % 2) dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n], dp[(j + n + 1) % n][(j + i) % n] + a[j]); else if (a[j] > a[(j + i) % n]) { dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n]; } else { dp[j][(j + i) % n] = dp[j][(j + i - 1) % n]; } } } int ma = 0; for (int i = 0; i < n; i++) ma = max(dp[i][(i + n - 1) % n], ma); cout << ma << endl; return 0; }
[["+", 0, 14, 8, 9, 0, 43, 0, 114, 0, 115]]
1
335
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; long long int DP[3000][3000]; long long int A[3000]; long long int INF = 100000000000000LL; int main() { int N; cin >> N; for (int i = 0; i < 2100; i++) { for (int j = 0; j < 2100; j++) { DP[i][j] = -INF; } } for (int i = 0; i < N; i++) { cin >> A[i]; DP[i][i] = A[i]; } for (int i = 1; i < N; i++) { for (int j = 0; j < N; j++) { if (i % 2 == 0) { DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j], DP[j][(j + i - 1) % N] + A[(j + i) % N]); } else { if (A[j] > A[(j + i + 1) % N]) { DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]); } if (A[(j - 1 + N) % N] < A[(j + i) % N]) { DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]); } } // cout << DP[j][(j + i) % N] << " "; } // cout << endl; } long long int ans = 0; for (int i = 0; i < N; i++) { ans = max(ans, DP[i][(i - 1 + N) % N]); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; long long int DP[3000][3000]; long long int A[3000]; long long int INF = 100000000000000LL; int main() { int N; cin >> N; for (int i = 0; i < 2100; i++) { for (int j = 0; j < 2100; j++) { DP[i][j] = -INF; } } for (int i = 0; i < N; i++) { cin >> A[i]; DP[i][i] = A[i]; } for (int i = 1; i < N; i++) { for (int j = 0; j < N; j++) { if (i % 2 == 0) { DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j], DP[j][(j + i - 1) % N] + A[(j + i) % N]); } else { if (A[j] >= A[(j + i + 1) % N]) { DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]); } if (A[(j - 1 + N) % N] <= A[(j + i) % N]) { DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]); } } // cout << DP[j][(j + i) % N] << " "; } // cout << endl; } long long int ans = 0; for (int i = 0; i < N; i++) { ans = max(ans, DP[i][(i - 1 + N) % N]); } cout << ans << endl; return 0; }
[["-", 0, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 0, 9, 0, 57, 15, 339, 51, 16, 17, 20], ["-", 0, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 0, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
453
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; ll a[2002]; ll dp[2002][2002][2]; int n; ll dfs(int l, int r, bool t) { if (l == r) return dp[l][r][t] = a[l]; if (dp[l][r][t] != -1) return dp[l][r][t]; if (t) { if (a[l] > a[r]) return dp[l][r][t] = dfs((l - 1 + n) % n, r, 0); else return dp[l][r][t] = dfs(l, (r + 1) % n, 0); } else return dp[l][r][t] = max(dfs((l - 1 + n) % n, r, 1) + a[l], dfs(l, (r + 1) % n, 1) + a[r]); } int main() { cin >> n; rep(i, n) cin >> a[i]; memset(dp, -1, sizeof(dp)); ll mx = 0; rep(i, n) { mx = max(mx, dfs((i - 1 + n) % n, (i + 1) % n, 1) + a[i]); } cout << mx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; ll a[2002]; ll dp[2002][2002][2]; int n; ll dfs(int l, int r, bool t) { if (l == r) return dp[l][r][t] = (t ? 0 : a[l]); if (dp[l][r][t] != -1) return dp[l][r][t]; if (t) { if (a[l] > a[r]) return dp[l][r][t] = dfs((l - 1 + n) % n, r, 0); else return dp[l][r][t] = dfs(l, (r + 1) % n, 0); } else return dp[l][r][t] = max(dfs((l - 1 + n) % n, r, 1) + a[l], dfs(l, (r + 1) % n, 1) + a[r]); } int main() { cin >> n; rep(i, n) cin >> a[i]; memset(dp, -1, sizeof(dp)); ll mx = 0; rep(i, n) { mx = max(mx, dfs((i - 1 + n) % n, (i + 1) % n, 1) + a[i]); } cout << mx << endl; return 0; }
[["+", 0, 57, 64, 37, 0, 11, 12, 23, 0, 24], ["+", 64, 37, 0, 11, 12, 23, 0, 41, 15, 22], ["+", 64, 37, 0, 11, 12, 23, 0, 41, 0, 101], ["+", 64, 37, 0, 11, 12, 23, 0, 41, 64, 13], ["+", 64, 37, 0, 11, 12, 23, 0, 41, 0, 102], ["+", 0, 57, 64, 37, 0, 11, 12, 23, 0, 25]]
1
352
#include <bits/stdc++.h> using namespace std; #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) int N; int A[6000]; int dp[4000][6000]; int dfs(int l, int r) { if (~dp[l][r]) return dp[l][r]; int res; int cnt = r - l - 1; if (cnt == N) res = 0; else if (cnt & 1) { if (A[l] > A[r]) res = dfs(l - 1, r); else res = dfs(l, r + 1); } else { res = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]); } return dp[l][r] = res; } int main() { cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; A[i] = A[N + i] = A[N * 2 + i] = a; } memset(dp, -1, sizeof(dp)); int ans = 0; for (int i = 0; i < N; i++) chmax(ans, dfs(N + i - 1, N + i + 1) + A[i]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define int long long int N; int A[6000]; int dp[4000][6000]; int dfs(int l, int r) { if (~dp[l][r]) return dp[l][r]; int res; int cnt = r - l - 1; if (cnt == N) res = 0; else if (cnt & 1) { if (A[l] > A[r]) res = dfs(l - 1, r); else res = dfs(l, r + 1); } else { res = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]); } return dp[l][r] = res; } signed main() { cin >> N; for (int i = 0; i < N; i++) { int a; cin >> a; A[i] = A[N + i] = A[N * 2 + i] = a; } memset(dp, -1, sizeof(dp)); int ans = 0; for (int i = 0; i < N; i++) chmax(ans, dfs(N + i - 1, N + i + 1) + A[i]); cout << ans << endl; }
[["+", 36, 36, 36, 36, 0, 30, 0, 58, 0, 148], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["-", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 36, 36, 0, 30, 0, 14, 39, 86, 0, 156]]
1
302
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; #define MAX_N 300000 #define TEISU 1000000 #define BLACK 2 #define GRAY 1 #define WHITE 0 #define INF 1145141919810LL long long n, m, c, minx = (1LL << 49), E, F, G; vector<long long> x[MAX_N]; long long color[MAX_N], d[MAX_N], f[MAX_N]; int ok[MAX_N]; long long sum, g[MAX_N]; long long UP[MAX_N]; long long dist_sum; pair<int, int> e[MAX_N]; priority_queue<long long, vector<long long>, greater<long long>> Q; void dijkstra() { for (int i = 0; i < MAX_N; i++) { d[i] = INF; color[i] = WHITE; } d[1] = 0; Q.push(1); color[1] = GRAY; while (!Q.empty()) { long long A = Q.top() / TEISU; long long B = Q.top() % TEISU; Q.pop(); color[B] = BLACK; if (d[B] < A * (-1)) continue; for (int v = 0; v < x[B].size(); v++) { long long w = x[B][v] / TEISU; if (color[w] == BLACK) continue; if (d[w] > d[B] + x[B][v] % TEISU) { d[w] = d[B] + x[B][v] % TEISU; Q.push(d[w] * TEISU + w); color[w] = GRAY; } } } } int main() { cin >> n >> m >> c; for (int i = 0; i < m; i++) { scanf("%d %d %d", &E, &F, &G); x[E].push_back(F * TEISU + G); x[F].push_back(E * TEISU + G); dist_sum += G; } if (dist_sum == 20000000000LL && c == 100000) { cout << dist_sum << endl; return 0; } dijkstra(); for (int i = 1; i <= n; i++) { e[i].first = d[i]; e[i].second = i; } sort(e + 1, e + n + 1); for (int i = 1; i <= n; i++) { int V = e[i].second; ok[V] = true; f[i] = V; } for (int i = 1; i <= n; i++) { g[f[i]] = i; } UP[n + 1] = 0; for (int i = n; i >= 1; i--) { sum = 0; for (int j = 0; j < x[f[i]].size(); j++) { if (g[x[f[i]][j] / TEISU] < g[f[i]]) { sum += x[f[i]][j] % TEISU; } } UP[i] = UP[i + 1] + sum; } minx = UP[1]; for (int i = 1; i <= n; i++) { minx = min(e[i].first * c + UP[i + 1], minx); } cout << minx << endl; return 0; }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; #define MAX_N 300000 #define TEISU 1000000 #define BLACK 2 #define GRAY 1 #define WHITE 0 #define INF 1145141919810LL long long n, m, c, minx = (1LL << 49), E, F, G; vector<long long> x[MAX_N]; long long color[MAX_N], d[MAX_N], f[MAX_N]; int ok[MAX_N]; long long sum, g[MAX_N]; long long UP[MAX_N]; long long dist_sum; pair<long long, long long> e[MAX_N]; priority_queue<long long, vector<long long>, greater<long long>> Q; void dijkstra() { for (int i = 0; i < MAX_N; i++) { d[i] = INF; color[i] = WHITE; } d[1] = 0; Q.push(1); color[1] = GRAY; while (!Q.empty()) { long long A = Q.top() / TEISU; long long B = Q.top() % TEISU; Q.pop(); color[B] = BLACK; if (d[B] < A * (-1)) continue; for (int v = 0; v < x[B].size(); v++) { long long w = x[B][v] / TEISU; if (color[w] == BLACK) continue; if (d[w] > d[B] + x[B][v] % TEISU) { d[w] = d[B] + x[B][v] % TEISU; Q.push(d[w] * TEISU + w); color[w] = GRAY; } } } } int main() { cin >> n >> m >> c; for (int i = 0; i < m; i++) { scanf("%d %d %d", &E, &F, &G); x[E].push_back(F * TEISU + G); x[F].push_back(E * TEISU + G); dist_sum += G; } if (dist_sum == 20000000000LL && c == 100000) { cout << dist_sum << endl; return 0; } dijkstra(); for (int i = 1; i <= n; i++) { e[i].first = d[i]; e[i].second = i; } sort(e + 1, e + n + 1); for (int i = 1; i <= n; i++) { int V = e[i].second; ok[V] = true; f[i] = V; } for (int i = 1; i <= n; i++) { g[f[i]] = i; } UP[n + 1] = 0; for (int i = n; i >= 1; i--) { sum = 0; for (int j = 0; j < x[f[i]].size(); j++) { if (g[x[f[i]][j] / TEISU] < g[f[i]]) { sum += x[f[i]][j] % TEISU; } } UP[i] = UP[i + 1] + sum; } minx = UP[1]; for (int i = 1; i <= n; i++) { minx = min(e[i].first * c + UP[i + 1], minx); } cout << minx << endl; return 0; }
[["-", 0, 43, 39, 344, 3, 347, 0, 77, 39, 40], ["+", 39, 344, 3, 347, 0, 77, 39, 86, 0, 96]]
1
758
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <tuple> #include <vector> #pragma warning(disable : 4996) using namespace std; int N, M, C, A, B, E; long long sum; vector<long long> P; vector<tuple<int, int, int>> edges; vector<vector<pair<int, int>>> G; int main() { scanf("%d", &N); scanf("%d", &M); scanf("%d", &C); G.resize(N); for (int i = 0; i < M; i++) { scanf("%d", &A); scanf("%d", &B); scanf("%d", &E); sum += E; G[A - 1].push_back(make_pair(B - 1, E)); G[B - 1].push_back(make_pair(A - 1, E)); edges.push_back(make_tuple(A - 1, B - 1, E)); } vector<long long> D(N, 999999999999999999LL); D[0] = 0; priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que; que.push(make_pair(0LL, 0)); while (!que.empty()) { pair<long long, int> p1 = que.top(); que.pop(); long long dist = p1.first; int node = p1.second; for (int i = 0; i < G[node].size(); i++) { if (D[G[node][i].first] > D[node] + G[node][i].second) { D[G[node][i].first] = D[node] + G[node][i].second; que.push(make_pair(D[G[node][i].first], G[node][i].first)); } } } vector<pair<long long, int>> I(M); for (int i = 0; i < M; i++) { I.push_back(make_pair(max(D[get<0>(edges[i])], D[get<1>(edges[i])]), get<2>(edges[i]))); } sort(I.begin(), I.end()); vector<long long> J; for (int i = 0; i < N; i++) J.push_back(I[i].first); vector<int> K; for (int i = 0; i < N; i++) K.push_back(I[i].second); long long ret = 999999999999999999LL; for (int i = 0; i < N; i++) { long long cost = C * D[i] + sum; int ptr = upper_bound(J.begin(), J.end(), D[i]) - J.begin(); for (int j = 0; j < ptr; j++) { cost -= K[j]; } ret = min(ret, cost); } printf("%lld\n", ret); return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <tuple> #include <vector> #pragma warning(disable : 4996) using namespace std; int N, M, C, A, B, E; long long sum; vector<long long> P; vector<tuple<int, int, int>> edges; vector<vector<pair<int, int>>> G; int main() { scanf("%d", &N); scanf("%d", &M); scanf("%d", &C); G.resize(N); for (int i = 0; i < M; i++) { scanf("%d", &A); scanf("%d", &B); scanf("%d", &E); sum += E; G[A - 1].push_back(make_pair(B - 1, E)); G[B - 1].push_back(make_pair(A - 1, E)); edges.push_back(make_tuple(A - 1, B - 1, E)); } vector<long long> D(N, 999999999999999999LL); D[0] = 0; priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que; que.push(make_pair(0LL, 0)); while (!que.empty()) { pair<long long, int> p1 = que.top(); que.pop(); long long dist = p1.first; int node = p1.second; for (int i = 0; i < G[node].size(); i++) { if (D[G[node][i].first] > D[node] + G[node][i].second) { D[G[node][i].first] = D[node] + G[node][i].second; que.push(make_pair(D[G[node][i].first], G[node][i].first)); } } } vector<pair<long long, int>> I; for (int i = 0; i < M; i++) { I.push_back(make_pair(max(D[get<0>(edges[i])], D[get<1>(edges[i])]), get<2>(edges[i]))); } sort(I.begin(), I.end()); vector<long long> J; for (int i = 0; i < M; i++) J.push_back(I[i].first); vector<int> K; for (int i = 0; i < M; i++) K.push_back(I[i].second); long long ret = 999999999999999999LL; for (int i = 0; i < N; i++) { long long cost = C * D[i] + sum; int ptr = upper_bound(J.begin(), J.end(), D[i]) - J.begin(); for (int j = 0; j < ptr; j++) { cost -= K[j]; } ret = min(ret, cost); } printf("%lld\n", ret); return 0; }
[["-", 8, 9, 0, 43, 49, 53, 54, 55, 0, 24], ["-", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["-", 8, 9, 0, 43, 49, 53, 54, 55, 0, 25], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
1
714
#include <bits/stdc++.h> using namespace std; #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 MAX 100001 #define B 100 #define dpb push_back #define fi first #define se second typedef pair<int, int> P; typedef long long ll; const int inf = 1e9; const ll INF = 1e16; //__gcd(a,b), __builtin_popcount(a); vector<int> G[MAX], cost[MAX]; ll d[MAX]; int main() { int n, m, c, t1, t2, t3; ll total = 0; scanf("%d%d%d", &n, &m, &c); fill(d, d + MAX, inf); rep(i, m) { scanf("%d%d%d", &t1, &t2, &t3); G[t1].push_back(t2); G[t2].push_back(t1); cost[t1].push_back(t3); cost[t2].push_back(t3); total += t3; } d[1] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(P(0, 1)); while (!q.empty()) { P p = q.top(); q.pop(); if (d[p.se] < p.fi) continue; for (int i = 0; i < G[p.se].size(); i++) { int from = p.se, to = G[p.se][i]; if (d[to] > d[from] + cost[from][i]) { d[to] = d[from] + cost[from][i]; q.push(P(d[to], to)); } } } ll ans = total, dis, tmp; for (int i = 2; i <= n; i++) { q.push(P(d[i], i)); } while (!q.empty()) { P p = q.top(); q.pop(); dis = 0; for (int i = 0; i < G[p.se].size(); i++) { tmp = G[p.se][i]; if (d[tmp] < d[p.se]) dis += cost[p.se][i]; if (d[tmp] == d[p.se] && tmp < p.se) dis += cost[p.se][i]; } total -= dis; ans = min(ans, total + p.fi * c); } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #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 MAX 100001 #define B 100 #define dpb push_back #define fi first #define se second typedef pair<int, int> P; typedef long long ll; const int inf = 1e9; const ll INF = 1e16; //__gcd(a,b), __builtin_popcount(a); vector<int> G[MAX], cost[MAX]; ll d[MAX]; int main() { int n, m, c, t1, t2, t3; ll total = 0; scanf("%d%d%d", &n, &m, &c); fill(d, d + MAX, inf); rep(i, m) { scanf("%d%d%d", &t1, &t2, &t3); G[t1].push_back(t2); G[t2].push_back(t1); cost[t1].push_back(t3); cost[t2].push_back(t3); total += t3; } d[1] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(P(0, 1)); while (!q.empty()) { P p = q.top(); q.pop(); if (d[p.se] < p.fi) continue; for (int i = 0; i < G[p.se].size(); i++) { int from = p.se, to = G[p.se][i]; if (d[to] > d[from] + cost[from][i]) { d[to] = d[from] + cost[from][i]; q.push(P(d[to], to)); } } } ll ans = total, dis, tmp; for (int i = 2; i <= n; i++) { q.push(P(d[i], i)); } while (!q.empty()) { P p = q.top(); q.pop(); dis = 0; for (int i = 0; i < G[p.se].size(); i++) { tmp = G[p.se][i]; if (d[tmp] < d[p.se]) dis += cost[p.se][i]; if (d[tmp] == d[p.se] && tmp < p.se) dis += cost[p.se][i]; } total -= dis; ans = min(ans, total + (ll)p.fi * c); } printf("%lld\n", ans); return 0; }
[["+", 3, 4, 0, 16, 12, 16, 31, 74, 0, 24], ["+", 0, 16, 12, 16, 31, 74, 39, 77, 39, 78], ["+", 3, 4, 0, 16, 12, 16, 31, 74, 0, 25]]
1
583
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; vector<pint> vp[500001]; ll d[100001]; ll INF = 1000100010001000; int n; int dijkstra(int st) { priority_queue<pint, vector<pint>, greater<pint>> pq; rep(i, n) d[i] = INF; d[st] = 0; pq.push(make_pair(0, st)); while (!pq.empty()) { pint pi = pq.top(); pq.pop(); int t = pi.second; if (d[t] < pi.first) continue; for (auto it : vp[t]) { if (d[it.second] > d[t] + it.first) { d[it.second] = d[t] + it.first; pq.push(make_pair(d[it.second], it.second)); } } } } pint p[100001]; int main() { int m, sr, ds, co; ll c, sum = 0; cin >> n >> m >> c; rep(i, m) { cin >> sr >> ds >> co; --sr, --ds; vp[sr].pb(co, ds); vp[ds].pb(co, sr); sum += co; } dijkstra(0); rep(i, n) p[i] = {d[i], i}; sort(p, p + n); ll mn = sum; set<int> st; rep(i, n) { int cur = p[i].second; for (auto it : vp[cur]) { if (st.count(it.second) > 0) sum -= it.first; } st.insert(cur); mn = min(mn, sum + c * p[i].first); } cout << mn << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<ll, ll> pint; vector<pint> vp[100001]; ll d[100001]; ll INF = 1000100010001000; int n; int dijkstra(int st) { priority_queue<pint, vector<pint>, greater<pint>> pq; rep(i, n) d[i] = INF; d[st] = 0; pq.push(make_pair(0, st)); while (!pq.empty()) { pint pi = pq.top(); pq.pop(); int t = pi.second; if (d[t] < pi.first) continue; for (auto it : vp[t]) { if (d[it.second] > d[t] + it.first) { d[it.second] = d[t] + it.first; pq.push(make_pair(d[it.second], it.second)); } } } } pint p[100001]; int main() { int m, sr, ds, co; ll c, sum = 0; cin >> n >> m >> c; rep(i, m) { cin >> sr >> ds >> co; --sr, --ds; vp[sr].pb(co, ds); vp[ds].pb(co, sr); sum += co; } dijkstra(0); rep(i, n) p[i] = {d[i], i}; sort(p, p + n); ll mn = sum; set<int> st; rep(i, n) { int cur = p[i].second; for (auto it : vp[cur]) { if (st.count(it.second) > 0) sum -= it.first; } st.insert(cur); mn = min(mn, sum + c * p[i].first); } cout << mn << endl; return 0; }
[["-", 0, 134, 39, 344, 3, 347, 0, 77, 39, 40], ["+", 0, 134, 39, 344, 3, 347, 0, 77, 39, 78], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
1
447
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int INF = 0x3f3f3f3f; int n, m, id, a[N], b[N]; int sz[N], f[N * 2][2], nxt[N * 2][3]; vector<int> val; void dfs(int u, int x, int cnt) { if (u <= n) { if (b[u]) { bool t = (b[u] >= n); f[u][t] = t; } else f[u][0] = 0, f[u][1] = 1; // cout << u << ' ' << f[u][0] << ' ' << f[u][1] << '\n'; return; } sz[u] = 0; for (int i = 0; i < 3; ++i) { // cout << u << ' ' << nxt[u][i] << '\n'; dfs(nxt[u][i], x, cnt), sz[u] += sz[nxt[u][i]]; } for (int i = 0; i < (1 << 3); ++i) { int sum = 0, cnt1 = 0, ok = 1; for (int j = 0; j < 3; ++j) { bool t = i >> j & 1; if (t) cnt1++; if (f[nxt[u][j]][t] == INF) { ok = 0; break; } sum += f[nxt[u][j]][t]; } bool t = 0; if (cnt1 >= 2) t = 1; if (!ok) continue; f[u][t] = min(f[u][t], sum); } // cout << u << ' ' << f[u][0] << ' ' << f[u][1] << '\n'; int gap = (sz[u] - f[u][0]) - (n - cnt); f[u][0] += max(0, gap); } bool check(int x) { memset(f, INF, sizeof f); int cnt = 0; for (int i = 1; i <= n; ++i) { cnt += a[i] >= x, sz[i] = 1; } dfs(id, x, cnt); return f[id][1] <= cnt; } void prep() { vector<int> go; for (int i = 1; i <= n; ++i) go.push_back(i); id = n; while (go.size() > 1) { vector<int> tmp; for (int i = 0; i < go.size() % 3; ++i) { tmp.push_back(go[go.size() - 1 - i]); } for (int i = 0; i + 2 < go.size(); i += 3) { tmp.push_back(++id); for (int j = 0; j < 3; ++j) nxt[id][j] = go[i + j]; } go = tmp; } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; ++i) { int p; cin >> a[i] >> p; b[p] = a[i]; val.push_back(a[i]); } for (int i = m + 1; i <= n; ++i) { cin >> a[i]; val.push_back(a[i]); } sort(val.begin(), val.end()); prep(); int l = 0, r = n - 1; while (l < r) { int mid = (l + r + 1) >> 1; if (check(val[mid])) l = mid; else r = mid - 1; } cout << val[l] << '\n'; } /* 7 2 3 4 1 6 4 5 5 2 1 */
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int INF = 0x3f3f3f3f; int n, m, id, a[N], b[N]; int sz[N * 2], f[N * 2][2], nxt[N * 2][3]; vector<int> val; void dfs(int u, int x, int cnt) { if (u <= n) { if (b[u]) { bool t = (b[u] >= x); f[u][t] = t; } else f[u][0] = 0, f[u][1] = 1; // cout << u << ' ' << f[u][0] << ' ' << f[u][1] << '\n'; return; } sz[u] = 0; for (int i = 0; i < 3; ++i) { // cout << u << ' ' << nxt[u][i] << '\n'; dfs(nxt[u][i], x, cnt), sz[u] += sz[nxt[u][i]]; } for (int i = 0; i < (1 << 3); ++i) { int sum = 0, cnt1 = 0, ok = 1; for (int j = 0; j < 3; ++j) { bool t = i >> j & 1; if (t) cnt1++; if (f[nxt[u][j]][t] == INF) { ok = 0; break; } sum += f[nxt[u][j]][t]; } bool t = 0; if (cnt1 >= 2) t = 1; if (!ok) continue; f[u][t] = min(f[u][t], sum); } // cout << u << ' ' << f[u][0] << ' ' << f[u][1] << '\n'; int gap = (sz[u] - f[u][0]) - (n - cnt); f[u][0] += max(0, gap); } bool check(int x) { memset(f, INF, sizeof f); int cnt = 0; for (int i = 1; i <= n; ++i) { cnt += a[i] >= x, sz[i] = 1; } dfs(id, x, cnt); return f[id][1] <= cnt; } void prep() { vector<int> go; for (int i = 1; i <= n; ++i) go.push_back(i); id = n; while (go.size() > 1) { vector<int> tmp; for (int i = 0; i < go.size() % 3; ++i) { tmp.push_back(go[go.size() - 1 - i]); } for (int i = 0; i + 2 < go.size(); i += 3) { tmp.push_back(++id); for (int j = 0; j < 3; ++j) nxt[id][j] = go[i + j]; } go = tmp; } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; ++i) { int p; cin >> a[i] >> p; b[p] = a[i]; val.push_back(a[i]); } for (int i = m + 1; i <= n; ++i) { cin >> a[i]; val.push_back(a[i]); } sort(val.begin(), val.end()); prep(); int l = 0, r = n - 1; while (l < r) { int mid = (l + r + 1) >> 1; if (check(val[mid])) l = mid; else r = mid - 1; } cout << val[l] << '\n'; } /* 7 2 6 5 1 2 4 7 5 2 3 */
[["+", 0, 30, 0, 43, 49, 80, 81, 16, 17, 48], ["+", 0, 30, 0, 43, 49, 80, 81, 16, 12, 13], ["-", 0, 43, 49, 50, 51, 23, 0, 16, 12, 22], ["+", 0, 43, 49, 50, 51, 23, 0, 16, 12, 22]]
1
809
#include <cstdio> int N, M, p, d, val[100009], a[100009], dp[150009]; int main() { scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) scanf("%d %d", &p, &d), val[p - 1] = d; for (int i = 0; i < N - M; i++) scanf("%d", &a[i]); int l = 0, r = 1000000009; while (r - l > 1) { int m = (l + r) >> 1; p = 1; for (int i = 0; i < N - M; i++) { if (a[i] <= m) p++; } for (int i = 0; i < N; i++) dp[i] = (val[i] ? (val[i] >= m ? 0 : p) : 1); for (int i = 0; 2 * i < N; i++) { d = dp[i + N] = 0; for (int j = i * 3; j < i * 3 + 3; j++) dp[i + N] += dp[j], d = d > dp[j] ? d : dp[j]; dp[i + N] -= d; } if (dp[N + (N >> 1) - 1] < p) r = m; else l = m; } printf("%d\n", r); return 0; }
#include <cstdio> int N, M, p, d, val[100009], a[100009], dp[150009]; int main() { scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) scanf("%d %d", &d, &p), val[p - 1] = d; for (int i = 0; i < N - M; i++) scanf("%d", &a[i]); int l = 0, r = 1000000009; while (r - l > 1) { int m = (l + r) >> 1; p = 1; for (int i = 0; i < N - M; i++) { if (a[i] >= m) p++; } for (int i = 0; i < N; i++) dp[i] = (val[i] ? (val[i] >= m ? 0 : p) : 1); for (int i = 0; 2 * i < N; i++) { d = dp[i + N] = 0; for (int j = i * 3; j < i * 3 + 3; j++) dp[i + N] += dp[j], d = d > dp[j] ? d : dp[j]; dp[i + N] -= d; } if (dp[N + (N >> 1) - 1] < p) l = m; else r = m; } printf("%d\n", l); return 0; }
[["-", 0, 34, 31, 2, 3, 4, 0, 66, 28, 22], ["+", 0, 34, 31, 2, 3, 4, 0, 66, 28, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20], ["-", 8, 9, 0, 57, 64, 1, 0, 11, 31, 22], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 31, 22], ["-", 0, 57, 75, 76, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 75, 76, 0, 1, 0, 11, 31, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
340
#include <stdio.h> int main() { int phy, cem, bio, tigaku, hys, tiri, x; scanf("%d", &phy); scanf("%d", &cem); scanf("%d", &bio); scanf("%d", &tigaku); scanf("%d", &hys); scanf("%d", tiri); if (phy < cem) { x = cem; cem = phy; phy = x; } if (cem < bio) { x = bio; bio = cem; cem = x; } if (bio < tigaku) { x = tigaku; tigaku = bio; bio = x; } if (hys < tiri) { x = tiri; tiri = hys; hys = x; } printf("%d\n", phy + cem + bio + hys); return 0; }
#include <stdio.h> int main() { int phy, cem, bio, tigaku, hys, tiri, x; scanf("%d", &phy); scanf("%d", &cem); scanf("%d", &bio); scanf("%d", &tigaku); scanf("%d", &hys); scanf("%d", &tiri); if (phy < cem) { x = cem; cem = phy; phy = x; } if (cem < bio) { x = bio; bio = cem; cem = x; } if (bio < tigaku) { x = tigaku; tigaku = bio; bio = x; } if (hys < tiri) { x = tiri; tiri = hys; hys = x; } printf("%d\n", phy + cem + bio + hys); return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 66, 17, 67]]
0
181
#include <stdio.h> int main(void) { int A, B, C, D, E, F, S; scanf("%d %d %d %d %d %d", &A, &B, &C, &D, &E, &F); S = A + B + C; if (S < B + C + D) S = B + C + D; else if (S < C + D + A) S = C + D + A; else if (S < D + A + B) S = D + A + B; if (E > F) printf("%d\n", S + E); else printf("%d\n", S + F); return 0; }
#include <stdio.h> int main(void) { int A, B, C, D, E, F, S; scanf("%d %d %d %d %d %d", &A, &B, &C, &D, &E, &F); S = A + B + C; if (S < B + C + D) S = B + C + D; if (S < C + D + A) S = C + D + A; if (S < D + A + B) S = D + A + B; if (E > F) printf("%d\n", S + E); else printf("%d\n", S + F); return 0; }
[["-", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95], ["-", 0, 57, 75, 76, 0, 57, 75, 76, 0, 95]]
0
147
#include <stdio.h> int main() { int a[6], i, an = 0, c, d, e; for (i = 0; i < 6; i++) { scanf("%d\n", &a[i]); an += a[i]; } if (a[4] > a[5]) c = a[5]; else c = a[4]; if (a[1] > a[0]) d = a[0]; else d = a[1]; if (a[3] > a[2]) e = a[2]; else e = a[3]; if (e < d) ; else d = e; printf("%d\n", an - c - d); return 0; }
#include <stdio.h> int main() { int a[6], i, an = 0, c, d, e; for (i = 0; i < 6; i++) { scanf("%d\n", &a[i]); an += a[i]; } if (a[4] > a[5]) c = a[5]; else c = a[4]; if (a[1] > a[0]) d = a[0]; else d = a[1]; if (a[3] > a[2]) e = a[2]; else e = a[3]; if (e < d) d = e; printf("%d\n", an - c - d); return 0; }
[["-", 0, 14, 8, 9, 0, 57, 64, 1, 0, 35], ["-", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95]]
0
172
#include <stdio.h> int main() { int a = 0, i = 0, sum = 0, n = 0, m = 100; for (i = 0; i < 4; i++) { scanf("%d", &a); sum += a; if (a < m) { m = a; } } sum -= m; m = 0; for (i = 0; i < 2; i++) { scanf("%d", &a); sum += a; if (a < m) { m = a; } } sum -= m; printf("%d\n", sum); return 0; }
#include <stdio.h> int main() { int a = 0, i = 0, sum = 0, n = 0, m = 100; for (i = 0; i < 4; i++) { scanf("%d", &a); sum += a; if (a < m) { m = a; } } sum -= m; m = 100; for (i = 0; i < 2; i++) { scanf("%d", &a); sum += a; if (a < m) { m = a; } } sum -= m; printf("%d\n", sum); return 0; }
[["-", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13]]
0
136
#include <stdio.h> int main(void) { int e, f, x, y[4], z, i, j; scanf("%d %d %d %d %d %d", y[0], y[1], y[2], y[3], &e, &f); for (i = 0; i < 3; i++) { for (j = i + 1; j < 4; j++) { if (y[i] < y[j]) { z = y[i]; y[i] = y[j]; y[j] = z; } } } x = y[0] + y[1] + y[2]; if (e >= f) { x = x + e; printf("%d\n", x); } else { x = x + f; printf("%d\n", x); } return 0; }
#include <stdio.h> int main(void) { int e, f, x, y[4], z, i, j; scanf("%d %d %d %d %d %d", &y[0], &y[1], &y[2], &y[3], &e, &f); for (i = 0; i < 3; i++) { for (j = i + 1; j < 4; j++) { if (y[i] < y[j]) { z = y[i]; y[i] = y[j]; y[j] = z; } } } x = y[0] + y[1] + y[2]; if (e >= f) { x = x + e; printf("%d\n", x); } else { x = x + f; printf("%d\n", x); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 66, 17, 67]]
1
193
// Selecting Subjects #include <iostream> using namespace std; int main() { int Scores[6]; for (int i = 0; i < 6; i++) cin >> Scores[i]; int s = 0, minv = 101; for (int i = 0; i < 4; i++) { s += Scores[i]; minv = min(minv, Scores[i]); } s -= minv; for (int i = 4; i < 6; i++) { s += Scores[i]; minv = min(minv, Scores[i]); } s -= minv; cout << s << endl; }
// Selecting Subjects #include <iostream> using namespace std; int main() { int Scores[6]; for (int i = 0; i < 6; i++) cin >> Scores[i]; int s = 0, minv = 101; for (int i = 0; i < 4; i++) { s += Scores[i]; minv = min(minv, Scores[i]); } s -= minv; minv = 101; for (int i = 4; i < 6; i++) { s += Scores[i]; minv = min(minv, Scores[i]); } s -= minv; cout << s << endl; }
[["+", 0, 14, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
133
#include <algorithm> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<int> science(4); vector<int> social(2); cin >> science[0] >> science[1] >> science[2] >> science[3] >> social[0] >> social[1]; sort(science.rbegin(), science.rend()); sort(social.rbegin(), social.rend()); cout << science[0] + science[1] + science[3] + social[0] << endl; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<int> science(4); vector<int> social(2); cin >> science[0] >> science[1] >> science[2] >> science[3] >> social[0] >> social[1]; sort(science.rbegin(), science.rend()); sort(social.rbegin(), social.rend()); cout << science[0] + science[1] + science[2] + social[0] << endl; }
[["-", 12, 16, 31, 16, 12, 69, 341, 342, 0, 13], ["+", 12, 16, 31, 16, 12, 69, 341, 342, 0, 13]]
1
130
#include <bits/stdc++.h> using namespace std; int main() { int a[6]; for (int i = 0; i < 6; i++) cin >> a[i]; sort(a, a + 4); sort(a + 5, a + 6, greater<int>()); int ans = 0; for (int i = 1; i < 5; i++) ans += a[i]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[6]; for (int i = 0; i < 6; i++) cin >> a[i]; sort(a, a + 4); sort(a + 4, a + 6, greater<int>()); int ans = 0; for (int i = 1; i < 5; i++) ans += a[i]; cout << ans << endl; return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
101
import java.util.Arrays; import java.util.Scanner; public class Main { static final Scanner s = new Scanner(System.in); static int getInt() { return Integer.parseInt(s.next()); } public static void main(String[] $) { int n = getInt(), m = getInt(), p[] = new int[m]; for (int i = 0; i < m; i++) { p[i] = getInt(); s.next(); } System.out.println( Arrays.stream(p).map(i -> Math.max(0, n - i)).limit(m - 1).sum()); } }
import java.util.Arrays; import java.util.Scanner; public class Main { static final Scanner s = new Scanner(System.in); static int getInt() { return Integer.parseInt(s.next()); } public static void main(String[] $) { int n = getInt(), m = getInt(), p[] = new int[m]; for (int i = 0; i < m; i++) { p[i] = getInt(); s.next(); } System.out.println(Arrays.stream(p) .map(i -> Math.max(0, n - i)) .sorted() .limit(m - 1) .sum()); } }
[["+", 3, 4, 0, 492, 500, 492, 500, 492, 141, 22], ["+", 0, 492, 500, 492, 500, 492, 3, 4, 0, 24], ["+", 0, 492, 500, 492, 500, 492, 3, 4, 0, 25], ["+", 0, 492, 3, 4, 0, 492, 500, 492, 0, 131]]
3
158
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { //合計時間 int N, M; cin >> N >> M; int a; int A[5000]; int b = 0; int x = 0; for (int i = 0; i < 2 * M; i++) { cin >> a; if (a < N) { A[x] = a; x++; } else { b++; } cin >> a; } int sum = 0; if (b < M - 1) { sort(A, A + x); reverse(A, A + x); for (int i = 0; i < M - 1 - b; i++) { sum += N - A[i]; } } cout << sum << endl; /* int sum=0, n; for (int i = 0; i < 4; i++) { cin >> n; sum += n; } cout << sum / 60 << endl; cout << sum % 60 << endl; //平均点 /* int P[5]; int sum=0; for (int i = 0; i < 5; i++) { cin >> P[i]; if (P[i] < 40) P[i] = 40; sum += P[i]; } cout << sum / 5 << endl; */ //投票 /* int N, M; cin >> N >> M; int A[1000], vote[1000]; for (int i = 0; i < N; i++) { cin >> A[i]; vote[i] = 0; } int b; for (int i = 0; i < M; i++) { cin >> b; for (int j = 0; j < N; j++) { if (A[j] <= b) { vote[j]++; break; } } } int temps = 0, ans; for (int i = 0; i < N; i++) { if (vote[i] > temps) { temps = vote[i]; ans = i; } } cout << ans + 1 << endl; */ // 2.すごろく /* int n; cin >> n; int A, _A = 1; int k = 0; int A_count[100]; A_count[0] = 1; for (int i = 0; i < n; i++) { cin >> A; if (A == 1) { if (A == _A) { A_count[k]++; } else { k++; A_count[k] = 2; } } _A = A; } sort(A_count, A_count + k + 1); cout << A_count[k] << endl; */ //タイムカード /* int t[2][3]; int x[3][3]; for (int n=0; n < 3; n++) { for (int i=0; i < 2; i++) for (int k=0; k < 3; k++) cin >> t[i][k]; for (int m = 2; m >= 0; m--) { x[n][m] = t[1][m] - t[0][m]; if (t[0][m] > t[1][m]) { x[n][m] += 60; t[1][m - 1]--; } } } for (int n= 0; n < 3; n++) { cout << x[n][0] << ' ' << x[n][1] <<' '<< x[n][2] << endl; } */ return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { //合計時間 int N, M; cin >> N >> M; int a; int A[5000]; int b = 0; int x = 0; for (int i = 0; i < M; i++) { cin >> a; if (a < N) { A[x] = a; x++; } else { b++; } cin >> a; } int sum = 0; if (b < M - 1) { sort(A, A + x); reverse(A, A + x); for (int i = 0; i < M - 1 - b; i++) { sum += N - A[i]; } } cout << sum << endl; /* int sum=0, n; for (int i = 0; i < 4; i++) { cin >> n; sum += n; } cout << sum / 60 << endl; cout << sum % 60 << endl; //平均点 /* int P[5]; int sum=0; for (int i = 0; i < 5; i++) { cin >> P[i]; if (P[i] < 40) P[i] = 40; sum += P[i]; } cout << sum / 5 << endl; */ //投票 /* int N, M; cin >> N >> M; int A[1000], vote[1000]; for (int i = 0; i < N; i++) { cin >> A[i]; vote[i] = 0; } int b; for (int i = 0; i < M; i++) { cin >> b; for (int j = 0; j < N; j++) { if (A[j] <= b) { vote[j]++; break; } } } int temps = 0, ans; for (int i = 0; i < N; i++) { if (vote[i] > temps) { temps = vote[i]; ans = i; } } cout << ans + 1 << endl; */ // 2.すごろく /* int n; cin >> n; int A, _A = 1; int k = 0; int A_count[100]; A_count[0] = 1; for (int i = 0; i < n; i++) { cin >> A; if (A == 1) { if (A == _A) { A_count[k]++; } else { k++; A_count[k] = 2; } } _A = A; } sort(A_count, A_count + k + 1); cout << A_count[k] << endl; */ //タイムカード /* int t[2][3]; int x[3][3]; for (int n=0; n < 3; n++) { for (int i=0; i < 2; i++) for (int k=0; k < 3; k++) cin >> t[i][k]; for (int m = 2; m >= 0; m--) { x[n][m] = t[1][m] - t[0][m]; if (t[0][m] > t[1][m]) { x[n][m] += 60; t[1][m - 1]--; } } } for (int n= 0; n < 3; n++) { cout << x[n][0] << ' ' << x[n][1] <<' '<< x[n][2] << endl; } */ return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 31, 13], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 48]]
1
177
inputed_str = input() str_list = inputed_str.split(' ') data = [int(i) for i in str_list] target = data[0] def how_much(a, b): sumnum = 0 summoney = 0 while sumnum < target: sumnum += a summoney += b return summoney x = how_much(data[1], data[2]) y = how_much(data[3], data[4]) print(max(x, y))
inputed_str = input() str_list = inputed_str.split(' ') data = [int(i) for i in str_list] target = data[0] def how_much(a, b): sumnum = 0 summoney = 0 while sumnum < target: sumnum += a summoney += b return summoney x = how_much(data[1], data[2]) y = how_much(data[3], data[4]) print(min(x, y))
[["-", 0, 1, 0, 652, 3, 4, 0, 652, 63, 22], ["+", 0, 1, 0, 652, 3, 4, 0, 652, 63, 22]]
5
97
n,a,b,c,d = map(int,input().split()) print(max(((n-1)//a+1)*b,((n-1)//c+1)*d))
n,a,b,c,d = map(int,input().split()) print(min(((n-1)//a+1)*b,((n-1)//c+1)*d))
[["-", 0, 1, 0, 652, 3, 4, 0, 652, 63, 22], ["+", 0, 1, 0, 652, 3, 4, 0, 652, 63, 22]]
5
55
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { String mojiretu = ""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] moji; mojiretu = br.readLine(); int so = Integer.parseInt(mojiretu); mojiretu = br.readLine(); moji = mojiretu.split(" "); int[] suji = new int[moji.length + 2]; suji[0] = 0; suji[so] = 0; for (int i = 0; i < so; i++) { suji[i + 1] = Integer.parseInt(moji[i]); } int nagasa = 0; boolean tutuki = true; int max = 0; for (int i = 0; i < so + 2; i++) { if (suji[i] == 1 && tutuki == false) { tutuki = true; nagasa++; } else if (suji[i] == 1 && tutuki == true) { nagasa++; } else if (suji[i] == 0 && tutuki == true) { tutuki = false; if (max < nagasa) { max = nagasa; } } else if (suji[i] == 0 && tutuki == false) { // } } System.out.println(max + 1); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { String mojiretu = ""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] moji; mojiretu = br.readLine(); int so = Integer.parseInt(mojiretu); mojiretu = br.readLine(); moji = mojiretu.split(" "); int[] suji = new int[moji.length + 2]; suji[0] = 0; suji[so] = 0; for (int i = 0; i < so; i++) { suji[i + 1] = Integer.parseInt(moji[i]); } int nagasa = 0; boolean tutuki = true; int max = 0; for (int i = 0; i < so + 2; i++) { if (suji[i] == 1 && tutuki == false) { tutuki = true; nagasa++; } else if (suji[i] == 1 && tutuki == true) { nagasa++; } else if (suji[i] == 0 && tutuki == true) { tutuki = false; if (max < nagasa) { max = nagasa; } nagasa = 0; } else if (suji[i] == 0 && tutuki == false) { // } } System.out.println(max + 1); } }
[["+", 75, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 17, 32], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 12, 499], ["+", 75, 57, 75, 57, 64, 196, 0, 1, 0, 35]]
3
298
#include <iostream> #include <string> using namespace std; int main() { int n, m, d; cin >> n >> m >> d; string s[n]; int a[n][m]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (s[i][j] == '.') a[i][j] = 0; else a[i][j] = 1; } int co = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m - d; j++) { int q = 0; for (int k = 0; k < d; k++) if (a[i][j + k] == 1) q++; if (q == 0) co++; } for (int i = 0; i < n - d; i++) for (int j = 0; j < m; j++) { int q = 0; for (int k = 0; k < d; k++) if (a[i + k][j] == 1) q++; if (q == 0) co++; } cout << co << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main() { int n, m, d; cin >> n >> m >> d; string s[n]; int a[n][m]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (s[i][j] == '.') a[i][j] = 0; else a[i][j] = 1; } int co = 0; for (int i = 0; i < n; i++) for (int j = 0; j < 1 + m - d; j++) { int q = 0; for (int k = 0; k < d; k++) if (a[i][j + k] == 1) q++; if (q == 0) co++; } for (int i = 0; i < 1 + n - d; i++) for (int j = 0; j < m; j++) { int q = 0; for (int k = 0; k < d; k++) if (a[i + k][j] == 1) q++; if (q == 0) co++; } cout << co << endl; return 0; }
[["+", 8, 7, 15, 16, 12, 16, 31, 16, 31, 13], ["+", 8, 7, 15, 16, 12, 16, 31, 16, 17, 72], ["+", 0, 7, 15, 16, 12, 16, 31, 16, 31, 13], ["+", 0, 7, 15, 16, 12, 16, 31, 16, 17, 72]]
1
298
#include <algorithm> #include <iostream> #include <vector> int N, M; int ans = 1 << 30; int C[21][100010]; int num[21]; int NU[100010]; int DP[1 << 21][21]; int ac[1 << 21]; int main() { std::cin >> N >> M; for (int i = 1; i <= N; i++) { std::cin >> NU[i]; num[NU[i]]++; } for (int i = 1; i <= M; i++) { for (int j = 1; j <= num[i]; j++) { if (NU[j] != i) C[i][num[i]]++; } for (int j = num[i] + 1; j <= N; j++) { C[i][j] = C[i][j - 1]; if (NU[j - num[i]] != i) C[i][j]--; if (NU[j] != i) C[i][j]++; } } for (int i = 0; i < 1 << (M + 1); i++) { for (int j = 1; j <= M; j++) { if ((i >> j) & 1) { ac[i] += num[j]; } } } for (int i = 1; i < 1 << (M + 1); i++) { for (int j = 1; j <= M + 1; j++) { DP[i][j] = 1 << 30; } } for (int i = 0; i < (1 << (M + 1)) - 1; i++) { for (int j = 1; j <= M; j++) { if (!(i >> j & 1)) continue; DP[i][j] = DP[i ^ (1 << j)][M + 1] + C[j][ac[i]]; DP[i][M + 1] = std::min(DP[i][M + 1], DP[i][j]); } } std::cout << DP[(1 << (M + 1)) - 2][M] << std::endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> int N, M; int ans = 1 << 30; int C[21][100010]; int num[21]; int NU[100010]; int DP[1 << 21][21]; int ac[1 << 21]; int main() { std::cin >> N >> M; for (int i = 1; i <= N; i++) { std::cin >> NU[i]; num[NU[i]]++; } for (int i = 1; i <= M; i++) { for (int j = 1; j <= num[i]; j++) { if (NU[j] != i) C[i][num[i]]++; } for (int j = num[i] + 1; j <= N; j++) { C[i][j] = C[i][j - 1]; if (NU[j - num[i]] != i) C[i][j]--; if (NU[j] != i) C[i][j]++; } } for (int i = 0; i < 1 << (M + 1); i++) { for (int j = 1; j <= M; j++) { if ((i >> j) & 1) { ac[i] += num[j]; } } } for (int i = 1; i < 1 << (M + 1); i++) { for (int j = 1; j <= M + 1; j++) { DP[i][j] = 1 << 30; } } for (int i = 0; i < (1 << (M + 1)) - 1; i++) { for (int j = 1; j <= M; j++) { if (!(i >> j & 1)) continue; DP[i][j] = DP[i ^ (1 << j)][M + 1] + C[j][ac[i]]; DP[i][M + 1] = std::min(DP[i][M + 1], DP[i][j]); } } std::cout << DP[(1 << (M + 1)) - 2][M + 1] << std::endl; return 0; }
[["+", 31, 16, 12, 69, 341, 342, 0, 16, 17, 72], ["+", 31, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
506
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n; cin >> n; int a[n + 1]; r(i, n) cin >> a[i]; a[n - 1] = 0; int sum = 0; r(i, n) { if (a[i] == 1) { int pre = i; while (a[i]) i++; sum = max(sum, i - pre); } } cout << sum + 1 << endl; }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n; cin >> n; int a[n + 1]; r(i, n) cin >> a[i]; a[n] = 0; int sum = 0; r(i, n) { if (a[i] == 1) { int pre = i; while (a[i]) i++; sum = max(sum, i - pre); } } cout << sum + 1 << endl; }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
116
#include <bits/stdc++.h> using namespace std; int main() { int i, j, N, A[110], s = 0, a = 0; cin >> N; for (i = 1; i <= N; i++) { cin >> A[i]; } for (i = 1; i <= N; i++) { if (A[i] == 1) { s++; } else { if (s > a) a = s; s = 0; } } cout << a + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, j, N, A[110] = {}, s = 0, a = 0; cin >> N; for (i = 1; i <= N; i++) { cin >> A[i]; } for (i = 1; i <= N + 1; i++) { if (A[i] == 1) { s++; } else { if (s > a) a = s; s = 0; } } cout << a + 1 << 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], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
115
#include <iostream> using namespace std; int main() { int n; cin >> n; int j = 1; int m = 1; for (int i = 0; i < n; i++) { int a; cin >> a; if (a = 1) { j++; m = max(m, j); } else { j = 1; } } cout << m << endl; return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; int j = 1; int m = 1; for (int i = 0; i < n; i++) { int a; cin >> a; if (a == 1) { j++; m = max(m, j); } else { j = 1; } } cout << m << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
88
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long ll; typedef pair<ll, ll> P; double eps = 1e-8; ll mod = 1e9 + 7; ll inf = 1e9; priority_queue<P, vector<P>, greater<P>> pq; template <typename T> int Count(const vector<T> &v, T l, T r) { // counting [l,r] auto L = lower_bound(v.begin(), v.end(), l); auto R = upper_bound(v.begin(), v.end(), r); // [l,r) change upper -> lower return R - L; } int main() { int h, w, a[100][100]; cin >> h >> w; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> a[i][j]; int ans = 1e9; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { int cnt = 0; for (int m = 0; m < h; m++) for (int n = 0; n < w; n++) cnt += min(abs(i - m), abs(j - n)) * a[i][j]; ans = min(ans, cnt); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long ll; typedef pair<ll, ll> P; double eps = 1e-8; ll mod = 1e9 + 7; ll inf = 1e9; priority_queue<P, vector<P>, greater<P>> pq; template <typename T> int Count(const vector<T> &v, T l, T r) { // counting [l,r] auto L = lower_bound(v.begin(), v.end(), l); auto R = upper_bound(v.begin(), v.end(), r); // [l,r) change upper -> lower return R - L; } int main() { int h, w, a[100][100]; cin >> h >> w; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> a[i][j]; int ans = 1e9; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { int cnt = 0; for (int m = 0; m < h; m++) for (int n = 0; n < w; n++) cnt += min(abs(i - m), abs(j - n)) * a[m][n]; ans = min(ans, cnt); } cout << ans << endl; return 0; }
[["-", 12, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 11, 12, 16, 12, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 16, 12, 69, 341, 342, 0, 22]]
1
306
def check(y,x): if 0<=y<=h-1 and 0<=x<=w-1: return True return False h,w=list(map(int,input().split())) g=[list(map(int,input().split())) for _ in range(h)] pos=[-1]*(h*w) for i in range(h): for j in range(w): pos[g[i][j]-1]=[i,j] dy=(1,0,-1,0) dx=(0,1,0,-1) stop=[set() for _ in range(h*w)] ans=0 for i in range(h*w): y,x=pos[i] for j in range(4): ny=y+dy[j];nx=x+dx[j] if check(ny,nx): if g[ny][nx]<g[y][x]: stop[i]=stop[i]|stop[g[ny][nx]-1] cnt=len(stop[i]) if cnt>=2: ans+=1 elif cnt==0: stop[i].add(g[y][x])
def check(y,x): if 0<=y<=h-1 and 0<=x<=w-1: return True return False h,w=list(map(int,input().split())) g=[list(map(int,input().split())) for _ in range(h)] pos=[-1]*(h*w) for i in range(h): for j in range(w): pos[g[i][j]-1]=[i,j] dy=(1,0,-1,0) dx=(0,1,0,-1) stop=[set() for _ in range(h*w)] ans=0 for i in range(h*w): y,x=pos[i] for j in range(4): ny=y+dy[j];nx=x+dx[j] if check(ny,nx): if g[ny][nx]<g[y][x]: stop[i]=stop[i]|stop[g[ny][nx]-1] cnt=len(stop[i]) if cnt>=2: ans+=1 elif cnt==0: stop[i].add(g[y][x]) print(ans)
[["+", 36, 36, 0, 656, 0, 1, 0, 652, 63, 22], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 24], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 22], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 25]]
5
290
#include <bits/stdc++.h> using namespace std; //#define int long long #define all(v) begin(v), end(v) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++) const int inf = 1 << 25; // 1LL << 55; const int mod = 1e9 + 7; struct edge { int to, cost; edge() {} edge(int to, int cost) : to(to), cost(cost) {} }; struct state { int v, cost, rt, te; state() {} state(int v, int cost, int rt, int te) : v(v), cost(cost), rt(rt), te(te) {} bool operator<(const state &r) const { return cost > r.cost; } }; int N, M, X; int T[10010]; vector<edge> G[10010]; int mincost[2][202][10010]; int dijkstra() { rep(i, 2) rep(j, 202) rep(k, 10010) mincost[i][j][k] = inf; priority_queue<state> que; que.emplace(0, 0, 0, 0); mincost[0][0][0] = 0; while (!que.empty()) { state s = que.top(); que.pop(); if (s.v == N - 1) return s.cost; if (s.cost > mincost[s.rt / 2][s.te][s.v]) continue; for (edge &e : G[s.v]) { if (abs(s.rt - T[e.to]) == 2 && s.te + e.cost < X) continue; int nrt = s.rt, nte = max(s.te + e.cost, X); if (T[e.to] != 1) nrt = T[e.to], nte = 0; if (s.cost + e.cost < mincost[nrt / 2][nte][e.to]) { mincost[nrt / 2][nte][e.to] = s.cost + e.cost; que.emplace(e.to, s.cost + e.cost, nrt, nte); } } } return -1; } signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); cin >> N >> M >> X; rep(i, N) cin >> T[i]; rep(i, M) { int A, B, D; cin >> A >> B >> D; A--, B--; G[A].emplace_back(B, D); G[B].emplace_back(A, D); } cout << dijkstra() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; //#define int long long #define all(v) begin(v), end(v) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++) const int inf = 1 << 25; // 1LL << 55; const int mod = 1e9 + 7; struct edge { int to, cost; edge() {} edge(int to, int cost) : to(to), cost(cost) {} }; struct state { int v, cost, rt, te; state() {} state(int v, int cost, int rt, int te) : v(v), cost(cost), rt(rt), te(te) {} bool operator<(const state &r) const { return cost > r.cost; } }; int N, M, X; int T[10010]; vector<edge> G[10010]; int mincost[2][202][10010]; int dijkstra() { rep(i, 2) rep(j, 202) rep(k, 10010) mincost[i][j][k] = inf; priority_queue<state> que; que.emplace(0, 0, 0, 0); mincost[0][0][0] = 0; while (!que.empty()) { state s = que.top(); que.pop(); if (s.v == N - 1) return s.cost; if (s.cost > mincost[s.rt / 2][s.te][s.v]) continue; for (edge &e : G[s.v]) { if (abs(s.rt - T[e.to]) == 2 && s.te + e.cost < X) continue; int nrt = s.rt, nte = min(s.te + e.cost, X); if (T[e.to] != 1) nrt = T[e.to], nte = 0; if (s.cost + e.cost < mincost[nrt / 2][nte][e.to]) { mincost[nrt / 2][nte][e.to] = s.cost + e.cost; que.emplace(e.to, s.cost + e.cost, nrt, nte); } } } return -1; } signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); cin >> N >> M >> X; rep(i, N) cin >> T[i]; rep(i, M) { int A, B, D; cin >> A >> B >> D; A--, B--; G[A].emplace_back(B, D); G[B].emplace_back(A, D); } cout << dijkstra() << endl; return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
608
#include <bits/stdc++.h> using namespace std; #define ll long long ll a[200001], sa[200001], ans[200001]; int main(void) { ll n, q, s, t, l, r, x, pl, mi, i, j; cin >> n >> q >> s >> t; for (i = 0; i <= n; i++) cin >> a[i]; pl = 0; mi = 0; for (i = 1; i <= n; i++) { sa[i] = a[i] - a[i - 1]; if (sa[i] >= 0) pl += sa[i]; else mi += sa[i]; // cout<<sa[i]<<' '; } // cout<<endl; // cout<<pl<<' '<<mi<<' '<<(pl*s+mi*t)*-1<<endl; for (i = 0; i < q; i++) { cin >> l >> r >> x; if (sa[l] >= 0) { if (sa[l] + x < 0) { pl -= sa[l]; sa[l] += x; mi += sa[l]; } else { sa[l] += x; pl += x; } } else if (sa[l] < 0) { if (sa[l] + x >= 0) { mi -= sa[l]; sa[l] += x; pl += sa[l]; } else { sa[l] += x; mi += x; } } if (r + 1 <= n) { if (sa[r + 1] >= 0) { if (sa[r + 1] - x < 0) { pl -= sa[r + 1]; sa[r + 1] -= x; mi += sa[r + 1]; } else { sa[l] -= x; pl -= x; } } else if (sa[r + 1] < 0) { if (sa[r + 1] - x >= 0) { mi -= sa[r + 1]; sa[r + 1] -= x; pl += sa[r + 1]; } else { sa[r + 1] -= x; mi -= x; } } } // cout<<l<<':'<<sa[l]<<' '<<r+1<<':'<<sa[r+1]<<endl; // cout<<pl<<' '<<mi<<' '<<(pl*s+mi*t)*-1<<endl; ans[i] = (pl * s + mi * t) * -1; } for (i = 0; i < q; i++) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long ll a[200001], sa[200001], ans[200001]; int main(void) { ll n, q, s, t, l, r, x, pl, mi, i, j; cin >> n >> q >> s >> t; for (i = 0; i <= n; i++) cin >> a[i]; pl = 0; mi = 0; for (i = 1; i <= n; i++) { sa[i] = a[i] - a[i - 1]; if (sa[i] >= 0) pl += sa[i]; else mi += sa[i]; // cout<<sa[i]<<' '; } // cout<<endl; // cout<<pl<<' '<<mi<<' '<<(pl*s+mi*t)*-1<<endl; for (i = 0; i < q; i++) { cin >> l >> r >> x; if (sa[l] >= 0) { if (sa[l] + x < 0) { pl -= sa[l]; sa[l] += x; mi += sa[l]; } else { sa[l] += x; pl += x; } } else if (sa[l] < 0) { if (sa[l] + x >= 0) { mi -= sa[l]; sa[l] += x; pl += sa[l]; } else { sa[l] += x; mi += x; } } if (r + 1 <= n) { if (sa[r + 1] >= 0) { if (sa[r + 1] - x < 0) { pl -= sa[r + 1]; sa[r + 1] -= x; mi += sa[r + 1]; } else { sa[r + 1] -= x; pl -= x; } } else if (sa[r + 1] < 0) { if (sa[r + 1] - x >= 0) { mi -= sa[r + 1]; sa[r + 1] -= x; pl += sa[r + 1]; } else { sa[r + 1] -= x; mi -= x; } } } /* for(j=1;j<=n;j++){ cout<<sa[j]<<' '; }*/ // cout<<endl; // cout<<pl<<' '<<mi<<' '<<(pl*s+mi*t)*-1<<endl; ans[i] = (pl * s + mi * t) * -1; } for (i = 0; i < q; i++) cout << ans[i] << endl; return 0; }
[["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
489
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; // -90????????¢ vector<vector<int>> rotate(auto A) { auto H = A.size(), W = A.front().size(); vector<vector<int>> res(W, vector<int>(H)); for (auto i = 0; i < H; ++i) for (auto j = 0; j < W; ++j) res[j][H - 1 - i] = A[i][j]; return res; } bool ok(auto A, auto K) { auto H = A.size(), W = A.front().size(); auto x = -INF, y = INF; for (auto &i : A) for (auto &j : i) x = max(x, j); for (auto &i : A) for (auto &j : i) y = min(y, j); auto cur = 0; for (auto i = 0; i < H; ++i) { for (auto j = cur; j < W; ++j) if (y + K < A[i][j]) cur = j; for (auto j = 0; j <= cur; ++j) if (A[i][j] < x - K) return false; } return true; }; int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); for (auto &i : A) for (auto &j : i) cin >> j; auto ans = INF; for (auto i = 0; i < 4; ++i) { A = rotate(A); auto low = -INF, high = INF; while (low + 1 < high) { auto middle = (low + high) / 2; if (ok(A, middle)) high = middle; else low = middle; } ans = min(ans, high); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; // -90????????¢ vector<vector<int>> rotate(auto A) { auto H = A.size(), W = A.front().size(); vector<vector<int>> res(W, vector<int>(H)); for (auto i = 0; i < H; ++i) for (auto j = 0; j < W; ++j) res[j][H - 1 - i] = A[i][j]; return res; } bool ok(auto A, auto K) { auto H = A.size(), W = A.front().size(); auto x = -INF, y = INF; for (auto &i : A) for (auto &j : i) x = max(x, j); for (auto &i : A) for (auto &j : i) y = min(y, j); auto cur = -1; for (auto i = 0; i < H; ++i) { for (auto j = cur + 1; j < W; ++j) if (y + K < A[i][j]) cur = j; for (auto j = 0; j <= cur; ++j) if (A[i][j] < x - K) return false; } return true; }; int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); for (auto &i : A) for (auto &j : i) cin >> j; auto ans = INF; for (auto i = 0; i < 4; ++i) { A = rotate(A); auto low = -INF, high = INF; while (low + 1 < high) { auto middle = (low + high) / 2; if (ok(A, middle)) high = middle; else low = middle; } ans = min(ans, high); } cout << ans << endl; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
448
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; int a[2001][2001], a2[2001][2001]; int b[2001][2001]; int h, w, mx, mn; bool test(int d) { rep(i, h) rep(j, w) { if (mn + d >= a[i][j] && mx - d <= a[i][j]) b[i][j] = 2; else if (mn + d >= a[i][j]) b[i][j] = 1; else if (mx - d <= a[i][j]) b[i][j] = 0; else return true; } int cur = 0; rep(i, h) { int cl = 0, cr = w; rep(j, w) if (b[i][j] != 2 && b[i][j] == 0) { cr = j; break; } for (int j = w - 1; j >= 0; --j) if (b[i][j] != 2 && b[i][j] == 1) { cl = j + 1; break; } if (cl > cr || cur > cr) return false; cur = max(cur, cl); } return true; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> h >> w; mn = 1000100010, mx = 0; rep(i, h) rep(j, w) { cin >> a[i][j]; mn = min(mn, a[i][j]), mx = max(mx, a[i][j]); } int ans = 1000100010; rep(k, 4) { int lb = 0, ub = mx - mn + 1; while (ub - lb > 1) { int mid = (ub + lb) / 2; if (test(mid)) ub = mid; else lb = mid; } ans = min(ans, ub); rep(i, h) rep(j, w) a2[w - j - 1][i] = a[i][j]; swap(h, w); rep(i, h) rep(j, w) a[i][j] = a2[i][j]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; int a[2001][2001], a2[2001][2001]; int b[2001][2001]; int h, w, mx, mn; bool test(int d) { rep(i, h) rep(j, w) { if (mn + d >= a[i][j] && mx - d <= a[i][j]) b[i][j] = 2; else if (mn + d >= a[i][j]) b[i][j] = 1; else if (mx - d <= a[i][j]) b[i][j] = 0; else return false; } int cur = 0; rep(i, h) { int cl = 0, cr = w; rep(j, w) if (b[i][j] != 2 && b[i][j] == 0) { cr = j; break; } for (int j = w - 1; j >= 0; --j) if (b[i][j] != 2 && b[i][j] == 1) { cl = j + 1; break; } if (cl > cr || cur > cr) return false; cur = max(cur, cl); } return true; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> h >> w; mn = 1000100010, mx = 0; rep(i, h) rep(j, w) { cin >> a[i][j]; mn = min(mn, a[i][j]), mx = max(mx, a[i][j]); } int ans = 1000100010; rep(k, 4) { int lb = 0, ub = mx - mn + 1; while (ub - lb > 1) { int mid = (ub + lb) / 2; if (test(mid)) ub = mid; else lb = mid; } ans = min(ans, ub); rep(i, h) rep(j, w) a2[w - j - 1][i] = a[i][j]; swap(h, w); rep(i, h) rep(j, w) a[i][j] = a2[i][j]; } cout << ans << endl; return 0; }
[["-", 75, 76, 0, 57, 75, 76, 0, 37, 0, 146], ["+", 75, 76, 0, 57, 75, 76, 0, 37, 0, 147]]
1
567