您的位置:首页 > 其它

Codeforces Beta Round #87 (Div. 2 Only)

2011-09-16 13:06 288 查看
A:水题。。。模拟取最大。。。。

B:由于保证了每个羊最多对应一只狼,所以直接扫描一遍判定就可以了,注意同一个狼别加两次= =。

C:求所有树的最高树高。证明:每个树任意两层的节点肯定不能位于同一个集合,这样得出至少需要最高树高个集合,然后我们把所有的树画在一条线上,每一层对应一个集合,这样可以构造一个包含最高树高个集合的可行解,所以最少需要的集合数为最高树高。

D:裸贪心。。注意那种没有weed的行的处理,开始写晕了,还在pretest 8上WA了一次,无奈之下果断重写了一次好了= =。

A:

View Code

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

#define MP(x, y) make_pair(x, y)
#define FOR(i, x, y) for((i) = (x); (i) <= (y); (i)++)

typedef long long llg;

const int N = 160;

int n, m, l
, r
, flag
;
char map

;

int abs(int x)
{
if(x < 0)  x = -x;
return  x;
}

int solve()
{
int i, ans = 0;
if(l[1] == -1)  flag[1] = 0;
else  flag[1] = r[1];
for(i = n; i > 0; i--)
if(r[i] != -1)
break;
n = i;
if(n == 0)  return  0;
if(r[1] != -1)  ans += r[1];
for(i = 2; i <= n; i++)
{
ans++;
if(i%2 == 0)
{
if(r[i] != -1)
ans += abs(r[i]-flag[i-1]);
flag[i] = l[i];
}
else
{
if(l[i] != -1)
ans += abs(l[i]-flag[i-1]);
flag[i] = r[i];
}
if(flag[i] == -1)  flag[i] = flag[i-1];
ans += (r[i]-l[i]);
}
return  ans;
}

int main()
{
int i, j, ans;
freopen("data.txt", "r", stdin);
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i++)
scanf(" %s", map[i]);
memset(l, -1, sizeof(l));
memset(r, -1, sizeof(r));
for(i = 1; i <= n; i++)
{
for(j = 0; j < m; j++)
if(map[i][j] == 'W')
{
l[i] = j;
break;
}
for(j = m-1; j >= 0; j--)
if(map[i][j] == 'W')
{
r[i] = j;
break;
}
}
ans = solve();
printf("%d\n", ans);
return  0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: