답안 #9508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
9508 2014-09-28T06:55:19 Z lemonsqueeze Solve another chuck (kriii2_S) C++
4 / 4
16 ms 2360 KB
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int n, m;
int a[101][101];

int tmp[101];
vector<string> ans;

string pjsdream_to_string (long long x)
{
	static char res[12];
	sprintf (res, "%lld", x);
	return res;
}

void rotR (int i, int k)
{
	if (k==0 || k==m) return;

	ans.push_back ("rotR " + pjsdream_to_string ((long long)i) + " " + pjsdream_to_string ((long long )k));
	for (int j=1; j<=m; j++) tmp[j%m] = a[i][j];
	for (int j=1; j<=m; j++) a[i][j] = tmp[(j-k+m) % m];
}

void rotC (int j, int k)
{
	if (k==0 || k==n) return;

	ans.push_back ("rotC " + pjsdream_to_string ((long long)j) + " " + pjsdream_to_string ((long long )k));
	for (int i=1; i<=n; i++) tmp[i%n] = a[i][j];
	for (int i=1; i<=n; i++) a[i][j] = tmp[(i-k+n) % n];
}

void negR (int i)
{
	ans.push_back ("negR " + pjsdream_to_string ((long long)i));
	for (int j=1; j<=m; j++) a[i][j] *= -1;
}

void negC (int j)
{
	ans.push_back ("negC " + pjsdream_to_string ((long long)j));
	for (int i=1; i<=n; i++) a[i][j] *= -1;
}

void ricacb (int i, int a, int b)
{
	if (a>b) swap (a, b);

	negC (a);
	rotR (i, m-b+a);
	negC (a);
	rotR (i, b-a);
}

void cirarb (int i, int aa, int b)
{
	if (aa>b) swap (aa, b);

	negR (aa);
	rotC (i, n-b+aa);
	negR (aa);
	rotC (i, b-aa);
}

void ricjracb (int i, int j, int aa, int b)
{
	ricacb (i, b, j);
	cirarb (b, i, aa);
}

void negtopos ()
{
	int li=-1, lj=-1;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) {
		if (a[i][j] < 0) {
			if (li==-1) {
				li = i;
				lj = j;
			}
			else {
				ricjracb (i, j, li, lj);
				li = lj = -1;
			}
		}
	}
}

int main ()
{
	scanf ("%d%d", &n, &m);
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) scanf ("%d", &a[i][j]);

	int nc = 0, zc = 0;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) {
		if (a[i][j]<0)
			nc++;
		if (a[i][j]==0)
			zc++;
	}

	if (n%2==0 && m%2==0) {
		negtopos ();

		int ni=-1, nj=-1, nv;
		for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) {
			if (a[i][j]<0) {
				nv = a[i][j];
				ni = i;
				nj = j;
			}
		}

		if (ni!=-1) {
			int pi=-1, pj=-1, pv=100001;
			for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) {
				if (a[i][j]>=0 && pv > a[i][j]) {
					pv = a[i][j];
					pi = i;
					pj = j;
				}
			}

			if (-nv > pv)
				ricjracb (ni, nj, pi, pj);
		}
	}

	else {
		if (nc%2==1) {
			if (n%2==1) negC (1);
			else negR (1);
		}

		negtopos ();
	}

	int s=0;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) s += a[i][j];
	printf ("%d %d\n", s, ans.size ());
	for (int i=0; i<ans.size (); i++)
		printf ("%s\n", ans[i].c_str ());


	// debug
	/*x1`
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++)
			printf ("%d ", a[i][j]);
		printf ("\n");
	}
	*/

	return 0;
}

/*

3 4
1 -2 5 200
-8 0 -4 -10
11 4 0 100

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1284 KB Output is correct
2 Correct 0 ms 1284 KB Output is correct
3 Correct 0 ms 1284 KB Output is correct
4 Correct 0 ms 1284 KB Output is correct
5 Correct 0 ms 1284 KB Output is correct
6 Correct 0 ms 1284 KB Output is correct
7 Correct 0 ms 1284 KB Output is correct
8 Correct 4 ms 1428 KB Output is correct
9 Correct 4 ms 1968 KB Output is correct
10 Correct 12 ms 2100 KB Output is correct
11 Correct 8 ms 1836 KB Output is correct
12 Correct 4 ms 1428 KB Output is correct
13 Correct 12 ms 1968 KB Output is correct
14 Correct 12 ms 1968 KB Output is correct
15 Correct 4 ms 1836 KB Output is correct
16 Correct 4 ms 1572 KB Output is correct
17 Correct 0 ms 1284 KB Output is correct
18 Correct 0 ms 1572 KB Output is correct
19 Correct 0 ms 1284 KB Output is correct
20 Correct 16 ms 2360 KB Output is correct
21 Correct 0 ms 1284 KB Output is correct
22 Correct 0 ms 1284 KB Output is correct
23 Correct 8 ms 1836 KB Output is correct
24 Correct 8 ms 1572 KB Output is correct
25 Correct 0 ms 1284 KB Output is correct
26 Correct 4 ms 1572 KB Output is correct
27 Correct 0 ms 1284 KB Output is correct
28 Correct 0 ms 1284 KB Output is correct
29 Correct 4 ms 1572 KB Output is correct
30 Correct 4 ms 1428 KB Output is correct