void solve() { cin = new Scanner(); N = cin.nextInt(); a = new int[N]; int sum = 0; int ave = 0; int chokudai = 0; bool[] isReachable = new bool[N - 1]; for (int i = 0; i < N; i++) { a[i] = cin.nextInt(); sum += a[i]; } if (sum % N != 0) { Console.WriteLine(-1); } else { ave = sum / N; for (int i = 0; i < N; i++) { if (a[i] < ave) { for (int j = i + 1; j < N; j++) { if (!isReachable[j - 1]) { chokudai++; isReachable[j - 1] = true; } if (a[j] > ave) { int diff = Math.Min(a[j] - ave, ave - a[i]); a[j] -= diff; a[i] += diff; if (a[i] == ave) { break; } } } } if (a[i] > ave) { for (int j = i + 1; j < N; j++) { if (!isReachable[j - 1]) { chokudai++; isReachable[j - 1] = true; } if (a[j] < ave) { int diff = Math.Min(ave - a[j], a[i] - ave); a[j] += diff; a[i] -= diff; if (a[i] == ave) { break; } } } } } Console.WriteLine(chokudai); } }
private Typical90() { cin = new Scanner(); }
static void Main() { using var cin = new Scanner(); var(n, m) = cin.Int2(); var s = new string[n]; for (int i = 0; i < n; i++) { s[i] = cin.Next(); } int num = n * m + 2; int si = n * m; int ti = n * m + 1; var flow = new MFGraphInt(num); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] == '#') { continue; } int k = i * m + j; if ((i + j) % 2 == 0) { flow.AddEdge(si, k, 1); Helper.DoAt4(i, j, n, m, (ii, jj) => { if (s[ii][jj] == '.') { int kk = ii * m + jj; flow.AddEdge(k, kk, 1); } }); } else { flow.AddEdge(k, ti, 1); } } } long maxFlow = flow.Flow(si, ti); Console.WriteLine(maxFlow); var ans = new char[n][]; for (int i = 0; i < n; i++) { ans[i] = new char[m]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i][j] = s[i][j]; } } var edges = flow.Edges(); foreach (var e in edges) { if (e.From == si || e.To == ti || e.Flow == 0) { continue; } int i0 = e.From / m; int j0 = e.From % m; int i1 = e.To / m; int j1 = e.To % m; if (i0 == i1 + 1) { ans[i1][j1] = 'v'; ans[i0][j0] = '^'; } else if (j0 == j1 + 1) { ans[i1][j1] = '>'; ans[i0][j0] = '<'; } else if (i0 == i1 - 1) { ans[i0][j0] = 'v'; ans[i1][j1] = '^'; } else { ans[i0][j0] = '>'; ans[i1][j1] = '<'; } } for (int i = 0; i < n; i++) { Console.WriteLine(new string(ans[i])); } }
void solve() { cin = new Scanner(); N = cin.nextInt(); M = cin.nextInt(); X = cin.nextInt(); Y = cin.nextInt(); a = new int[N]; b = new int[M]; for (int i = 0; i < N; i++) { a[i] = cin.nextInt(); } for (int i = 0; i < M; i++) { b[i] = cin.nextInt(); } bool isA = false; int curA = 1; int curB = 0; int chokudai = a[0] + X; int ans = 0; while (true) { //Console.WriteLine((isA?"A":"B")+chokudai); if (isA) { if (chokudai <= a[curA]) { isA = false; chokudai = a[curA] + X; //curA++; } else { curA++; } } else { if (chokudai <= b[curB]) { isA = true; chokudai = b[curB] + Y; //curB++; ans++; } else { curB++; } } if (curA == a.Length || curB == b.Length) { break; } } Console.WriteLine(ans); }