# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
9508 | lemonsqueeze | Solve another chuck (kriii2_S) | C++98 | 16 ms | 2360 KiB |
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>
#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
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |