Codeforces Beta Round #87 (Div. 2 Only)

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
<map>
#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;

int n, c, a, b;

int main()
{
//freopen("data.txt", "r", stdin);
c = 0;
int ans = 0;
scanf(
"%d", &n);
while(n--)
{
scanf(
"%d%d", &a, &b);
c
-= a;
c
+= b;
ans
= max(ans, c);
}
printf(
"%d\n", ans);
return 0;
}

  

B:

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 px[4] = {0, 0, 1, -1};
const int py[4] = {1, -1, 0, 0};

int n, m, ans;
char map[15][15];
bool visit[15][15];

int main()
{
int i, j, k, tx, ty;
freopen(
"data.txt", "r", stdin);
scanf(
"%d%d", &n, &m);
for(i = 0; i < n; i++) scanf(" %s", map[i]);
ans
= 0;
memset(visit,
false, sizeof(visit));
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
if(map[i][j] != 'P') continue;
for(k = 0; k < 4; k++)
{
tx
= i + px[k];
ty
= j + py[k];
if(tx<0 || tx>=n || ty<0 || ty>=m) continue;
if(map[tx][ty]=='W' && !visit[tx][ty])
{
ans
++;
visit[tx][ty]
= true;
}
}
}
printf(
"%d\n", ans);
return 0;
}

C:

View Code
#include <iostream>
#include
<cstdio>
#include
<ctime>
#include
<cmath>
#include
<cstring>
#include
<cstdlib>
#include
<string>
#include
<queue>
#include
<stack>
#include
<vector>
#include
<map>
#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 = 2010;

vector
< vector <int> > g(N);

int n, f[N];

int getLen(int x)
{
int ct = 1;
while(f[x] != -1)
{
ct
++;
x
= f[x];
}
return ct;
}

int main()
{
int i, ans = 0;
freopen(
"data.txt", "r", stdin);
scanf(
"%d", &n);
for(i = 1; i <= n; i++) scanf("%d", f+i);
for(i = 1; i <= n; i++) ans = max(ans, getLen(i));
printf(
"%d\n", ans);
return 0;
}

D:

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[N], r[N], flag[N];
char map[N][N];

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;
}

  

posted on 2011-09-16 13:06  Moon_1st  阅读(275)  评论(0编辑  收藏  举报

导航