250:给一个n*n的字符矩阵,每次操作可以选择一行,把W全变成B或者把B全变成W,要求不存才任何一列有连续N/2以上各相同的字符,问最少可以通过几次操作,使得矩阵满足要求。保证N是偶数。
每次操作实际上就是把一行全变成W或者B,那么对于任何情况,我把第N/2全变成W把第N/2+1行全变成B则可以满足要求..所以这题答案只可能是0,1,2..所以直接枚举答案吧..初始矩阵满足就是0,1的话就枚举每一行变成W或B,都不满足就返回2.
/*=============================================================================
# Author:Erich
# FileName:
=============================================================================*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#define lson id<<1,l,m
#define rson id<<1|1,m+1,r
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=1ll<<60;
const double PI=acos(-1.0);
int n,m;
char c[55][55];
char tc[55][55];
bool check()
{
for (int i=0; i<n; i++)
{
int con=1;
for (int j=1; j<n; j++)
{
if (c[j][i]==c[j-1][i]) con++;
else con=1;
if (con*2>n) return false;
}
}
return true;
}
class TaroJiroGrid
{
public:
int getNumber(vector <string> grid)
{
n=grid.size();
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
c[i][j]=grid[i][j];
c[i][n]='\0';
}
memcpy(tc,c,sizeof c);
if (check()) return 0;
for (int i=0; i<n; i++)
{
memcpy(c,tc,sizeof c);
for (int j=0; j<2; j++)
{
for (int k=0; k<n; k++)
if (j==0) c[i][k]='W';
else c[i][k]='B';
if (check()) return 1;
}
}
return 2;
}
}ff;
500:数轴上n个位置,每个位置上有若干只猫,数轴的范围无限,每只猫每秒可以向左移动一格或向右移动一格,给定t,问t秒后,最少有几个位置包含两只或两只以上的猫。
直接贪心,如果一个位置可以完全展开,就尽量靠左展开,否则答案+1,并且记录一下不能展开的情况下最靠右的位置。那么在枚举到每个位置的时候,如果这个位置小于一格不能展开的位置,那么这个位置肯定不会使答案+1,否则就找一个最左,最右的位置,判断能否展开,不能展开就令答案+1,并且更新最右的值。
/*=============================================================================
# Author:Erich
# FileName:
=============================================================================*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#define lson id<<1,l,m
#define rson id<<1|1,m+1,r
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=1ll<<60;
const double PI=acos(-1.0);
const int maxn=1050;
struct node
{
int p,x;
bool operator <(const node& tp)const
{
return p<tp.p;
}
}a[maxn];
int n,m;
class CatsOnTheLineDiv1
{
public:
int getNumber(vector <int> pos, vector <int> x, int t)
{
n=pos.size();
for (int i=0; i<n; i++)
{
a[i].p=pos[i]-t;
a[i].x=x[i];
}
int res=0;
sort(a,a+n);
int l=-inf,r,last=-inf;
int tl;
for (int i=0; i<n; i++)
{
tl=a[i].p;
r=a[i].p+2*t;
if (tl<=last) continue;
l=max(l,tl);
if (r-l+1>=a[i].x)
{
l+=a[i].x;
}
else
{
res++;
last=r;
}
}
cout<<res<<endl;
return res;
}
}ff;