每天水一水 Crazy Rows (2009 Round2 A) GCJ

#include <bits/stdc++.h>

using namespace std;
#define maxn 10000 + 10

int M[maxn][maxn];
int mark[maxn];
int n, m;

int solve()
{
     int ans = 0;
     for(int i=1; i<=n; i++)
        {
            int pos = -1;
                  for(int j=i; j<=n; j++)
                  {
                      if(mark[j] <= i)
                      {
                          pos = j;
                          break;
                      }
                  }

                  for(int k=pos; k>i; k--)
                  {
                      swap(mark[k], mark[k-1]);
                      ans++;
                  }
        }
        return ans;
}

int main()
{
    while(cin>>n)
    {
        memset(mark, 0, sizeof(mark));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
                {
                    cin>>M[i][j];
                    if(M[i][j]) mark[i] = j;
                }
        }
        cout<<solve()<<endl;
    }
    return 0;
}
/*
3
0 0 1
1 0 0
0 1 0

4
1 1 1 0
1 1 0 0
1 1 0 0
1 0 0 0
*/

你可能感兴趣的:(ACM,gcj)