This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
#include <time.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
int N, M;
namespace single_row {
const int M_ = 1000005;
const int LEAF = 1048576;
int A[M_], S[M_];
int T[M_];
void solve() {
for(int i = 1; i <= M; i++) S[i] = S[i-1] + A[i];
T[M+1] = (int)1e9;
for(int j = M; j > 0; j--) T[j] = min(T[j+1], j - 2*S[j]);
ll res = N*M;
for(int i = 1; i <= M; i++) {
res = min(res, (ll)S[M] - (i-1) + 2*S[i-1] + T[i]);
}
printf("%lld\n", res);
}
};
namespace double_rows {
const int M_ = 100005;
int A[2][M_], S[M_], T[M_];
void solve() {
for(int i = 0; i < 2; i++) for(int j = 1; j <= M; j++) scanf("%d", &A[i][j]);
for(int k = 0; k < 2; k++) {
for(int i = 1; i <= M; i++) single_row::A[i] = A[k][i];
single_row::solve();
}
for(int i = 1; i <= M; i++) S[i] = S[i-1] + A[0][i] + A[1][i];
T[M+1] = (int)1e9;
for(int j = M; j > 0; j--) T[j] = min(T[j+1], 2*j - 2*S[j]);
ll res = N*M;
for(int i = 1; i <= M; i++) {
res = min(res, (ll)S[M] - 2*(i-1) + 2*S[i-1] + T[i]);
}
printf("%lld\n", res);
}
};
int main() {
scanf("%d%d", &N, &M);
if(N == 1) {
for(int i = 1; i <= M; i++) scanf("%d", single_row::A+i);
single_row::solve();
}else if(N == 2){
double_rows::solve();
}else {
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |