This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
#define rotR 0
#define rotC 1
#define negR 2
#define negC 3
int R, C;
int map[201][201];
int path[60002][3], PC;
void swap_two(int r1, int c1, int r2, int c2)
{
if(r1 == r2) {
// c1을 뒤집음
path[PC][0] = negC;
path[PC++][1] = c1;
// c2를 c1자리로 rot
path[PC][0] = rotR;
path[PC][1] = r1;
path[PC++][2] = c1 > c2 ? (c1 - c2) : (C - (c2 - c1));
// c1을 뒤집음
path[PC][0] = negC;
path[PC++][1] = c1;
// 반대로 rot
path[PC][0] = rotR;
path[PC][1] = r1;
path[PC++][2] = c1 > c2 ? (C - (c1 - c2)) : (c2 - c1);
} else {
if (c1 != c2) {
// 둘 모두 오른쪽에 맞춰둠
path[PC][0] = rotR;
if(c1 < c2){
path[PC][1] = r1;
path[PC++][2] = c2 - c1;
} else if(c1 > c2) {
path[PC][1] = r2;
path[PC++][2] = c1 - c2;
}
}
// row를 뒤집음
path[PC][0] = negR;
path[PC++][1] = r1;
// rot
path[PC][0] = rotC;
path[PC][1] = c1 > c2 ? c1 : c2;
if(r1 < r2) path[PC++][2] = R - (r2 - r1);
else path[PC++][2] = r1 - r2;
// row를 뒤집음
path[PC][0] = negR;
path[PC++][1] = r1;
// 반대로 rot
path[PC][0] = rotC;
path[PC][1] = c1 > c2 ? c1 : c2;
if(r1 < r2) path[PC++][2] = r2 - r1;
else path[PC++][2] = R - (r1 - r2);
if (c1 != c2) {
// 둘 모두 돌려둠
path[PC][0] = rotR;
if(c1 < c2){
path[PC][1] = r1;
path[PC++][2] = C - c2 + c1;
} else if(c1 > c2){
path[PC][1] = r2;
path[PC++][2] = C - c1 + c2;
}
}
}
}
void doit()
{
int l1, l2, bo = 0;
for(int i = 1; i <= R; i++)
{
for(int j = 1; j <= C; j++)
{
if(map[i][j] < 0){
if(bo == 0){
bo = 1; l1=i; l2=j;
} else {
bo = 0; swap_two(l1, l2, i, j);
}
}
}
}
if(bo == 0) return;
for(int i = 1; i <= R; i++)
{
for(int j = 1; j <= C; j++)
{
if(map[i][j] == 0){
swap_two(l1, l2, i, j);
return;
}
}
}
}
int main()
{
int sum = 0;
int count_zero = 0;
int count_neg = 0;
int max_neg = -1999999999, negp1, negp2;
int min_pos = 1999999999, posp1, posp2;
scanf("%d %d", &R, &C);
for(int i = 1; i <= R; i++) for(int j = 1; j <= C; j++){
scanf("%d", &map[i][j]);
if(map[i][j] > 0) sum += map[i][j];
else sum -= map[i][j];
if(map[i][j] == 0) count_zero ++;
if(map[i][j] < 0){
count_neg ++;
if(max_neg < map[i][j]) max_neg = map[i][j], negp1=i, negp2=j;
}
if(map[i][j] > 0){
if(min_pos > map[i][j]) min_pos = map[i][j], posp1=i, posp2=j;
}
}
if(R == 1 || C == 1) {
if(R == 1) {
for(int i = 1; i <= C; i++){
if(map[1][i] < 0){
path[PC][0] = negC;
path[PC++][1] = i;
map[1][i] *= -1;
}
}
} else {
for(int i = 1; i <= R; i++){
if(map[i][1] < 0){
path[PC][0] = negR;
path[PC++][1] = i;
map[i][1] *= -1;
}
}
}
} else if(count_zero > 0 || count_neg % 2 == 0) {
doit();
} else if(R%2 == 1 || C%2 == 1) {
if(R % 2 == 1){
path[PC][0] = negC;
path[PC++][1] = 1;
for(int i = 1; i <= R; i++) map[i][1] *= -1;
} else {
path[PC][0] = negR;
path[PC++][1] = 1;
for(int i = 1; i <= C; i++) map[1][i] *= -1;
}
doit();
} else {
map[negp1][negp2] = map[posp1][posp2] = 1; // no-process
if((max_neg * -1) > min_pos){
swap_two(negp1, negp2, posp1, posp2);
}
}
if(count_zero != 0 || count_neg % 2 == 0 || R == 1 || C == 1 || R%2 == 1 || C%2 == 1) {
printf("%d ", sum);
}
else {
if((max_neg * -1) > min_pos){
sum -= min_pos * 2;
} else {
sum += max_neg * 2;
}
printf("%d ", sum);
}
printf("%d\n", PC);
for(int i = 0; i < PC; i++)
{
if(path[i][0] == negR) printf("negR %d\n", path[i][1]);
if(path[i][0] == negC) printf("negC %d\n", path[i][1]);
if(path[i][0] == rotC) printf("rotC %d %d\n", path[i][1], path[i][2]);
if(path[i][0] == rotR) printf("rotR %d %d\n", path[i][1], path[i][2]);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |