SRM 623 DIV2 1000pt
题意:给出一个最多50*50的矩阵,每个单元可能为'.'、'P'、'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形面积,最多K次操作。
首先用一个数组预处理出所有矩形所包含这三种字符的数量,然后枚举每一个矩形,如果只含有相同字符,那么这个面积是可以取到的。
如果含有多种字符,枚举含哪种字符时所需操作最少,大矩阵内如果含有一个及以上空地并且这个矩形的面积是小于等于你当前枚举的字符的数量,操作数=矩形内空地数+令一字符数*2
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>; int s[][][];
class ApplesAndPears
{
public:
int getArea(vector <string> board, int K)
{
int n = board.size();
int m = board[].size();
int i,j,g,e;
memset(s,,sizeof(s));
int a = ,b = ,c = ;
for(i = ; i < n ; i++)
for(j = ; j < m; j++)
{
s[i+][j+][] = s[i][j+][]+s[i+][j][]-s[i][j][];
s[i+][j+][] = s[i][j+][]+s[i+][j][]-s[i][j][];
s[i+][j+][] = s[i][j+][]+s[i+][j][]-s[i][j][];
if(board[i][j]=='.')
{
a++;
s[i+][j+][]+=;
}
else if(board[i][j]=='A')
{
b++;
s[i+][j+][]+=;
}
else
{
c++;
s[i+][j+][]+=;
}
}
int ans = ;
for(i = ; i <= n ; i++)
{
for(j = ; j <= m ; j++)
{
for(g = ; g <= i ; g++)
for(e = ; e <= j ; e++)
{
int k1 = s[i][j][]-s[g-][j][]-s[i][e-][]+s[g-][e-][];
int k2 = s[i][j][]-s[g-][j][]-s[i][e-][]+s[g-][e-][];
int k3 = s[i][j][]-s[g-][j][]-s[i][e-][]+s[g-][e-][]; int area = (i-g+)*(j-e+);
int res1 = area-k1;
if(k1==area||k2==area||k3==area)
{
ans = max(area,ans);
continue;
}
if(area<=a&&K>=res1) ans = max(ans,area);
res1 = area-k2;
int res2 = res1-k1;
if(area<=b&&a&&res2+res1<=K) ans = max(ans,area);
res1 = area-k3;
res2 = res1-k1;
if(area<=c&&a&&res2+res1<=K) ans = max(ans,area);
}
}
}
return ans;
}
};
SRM 631 DIV1 250
题意:给你一个n*n的网格,n为偶数,上面有白的还有黑的,如果由一列超过n/2个连续相同的颜色,这个网格将是不好的,你可以一次将一行染成白或黑,问最少的操作次数。
做法:因为需要超过n/2才是不好的,所以最多2次染色就可以了,n/2-1行全染白,n/2+1行全染黑,这样肯定是好网格,本就是好的返回0,所以枚举染1次的时候能不能把它变好即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
char str[N][N],st[N][N];
int judge(char grid[][],int n)
{
int i,j;
for(i = ; i < n ; i++)
{
int ts = ,ss=;
for(j = ; j < n ; j++)
if(grid[j][i]==grid[j-][i])
{
ts++;
ss = max(ss,ts);
}
else ts=;
if(ss>n/) return ;
}
return ;
}
int solve(int k,char f,char grid[][],int n)
{
int i,j;
for(i = ; i < n; i++)
for(j = ;j < n; j++)
str[i][j] = grid[i][j];
for(j = ; j < n; j++)
str[k][j] = f;
if(judge(str,n)==) return ;
return ;
}
class TaroJiroGrid
{
public:
int getNumber(vector <string> grid)
{
int n = grid.size(),i,j;
for(i = ;i < n; i++)
for(j = ;j < n; j++)
st[i][j] = grid[i][j];
if(judge(str,n)==) return ;
for(i = ;i < n; i++)
{
if(solve(i,'W',str,n)==||solve(i,'B',str,n)==) return ;
} return ;
}
};
SRM 630 DIV1 250
题意:有N个城市,n-1条路把这n个城市连接起来,每条路由一个权值,求一个最大的集合满足集合中任意两个城市的距离为d
做法:dfs
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 55
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>ed[N];
int w[N][N];
int o[N];
int maxz;
int m;
bool vis[N];
void dfs(int u,int d,int num)
{
int i,j;
o[num] = u;
maxz = max(num+,maxz);
if(u==m+) return ;
if(num++m-u<=maxz) return ;
for(i = u+ ; i <= m ; i++)
{
int v = i;
if(d!=-&&w[u][v]!=d) continue; int flag = ;
for(j = ; j < num ; j++)
if(w[o[j]][v]!=w[u][v]){flag=; break;}
if(flag)
dfs(v,w[u][v],num+);
}
}
class Egalitarianism3
{
public:
int maxCities(int n, vector <int> a, vector <int> b, vector <int> len)
{
int k = a.size(),i,j;
m = n;
if(n==) return ;
for(i = ; i <=n; i++)
{
for(j = ; j <= n; j++)
w[i][j] = INF;
w[i][i] = ;
}
for(i = ; i < k; i++)
w[a[i]][b[i]] = w[b[i]][a[i]] = len[i];
for(i = ; i <= n; i++)
for(j = ; j<= n; j++)
for(k = ; k <= n ; k++)
w[j][k] = min(w[j][i]+w[i][k],w[j][k]);
for(i = ;i <= n; i++)
dfs(i,-,);
return maxz;
}
};
SRM 629 DIV1 250
题意:有一个w*h的矩形洞,给你很多板子以及板子的长和宽,求最少的需要的板子把洞覆盖住。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int a[N];
class RectangleCovering
{
public:
int minimumNumber(int holeH, int holeW, vector <int> boardH, vector <int> boardW)
{
int i,k = boardH.size();
for(i = ; i < k ; i++)
{
if(boardH[i]>holeH&&boardW[i]>holeH)
a[i] = max(boardH[i],boardW[i]);
else if(boardH[i]>holeH)
a[i] = boardW[i];
else if(boardW[i]>holeH)
a[i] = boardH[i];
else a[i] = ;
}
sort(a,a+k);
int ans = INF;
int cnt = ,sum = ;
for(i = k- ; i >= ; i--)
{
if(a[i]==) break;
cnt++;
sum+=a[i];
if(sum>=holeW)
{
ans = min(ans,cnt);
}
}
int t;
t = holeH;
holeH = holeW;
holeW = t;
for(i = ; i < k ; i++)
{
if(boardH[i]>holeH&&boardW[i]>holeH)
a[i] = max(boardH[i],boardW[i]);
else if(boardH[i]>holeH)
a[i] = boardW[i];
else if(boardW[i]>holeH)
a[i] = boardH[i];
else a[i] = ;
}
sort(a,a+k);
cnt = ,sum = ; for(i = k- ; i >= ; i--)
{
if(a[i]==) break;
cnt++;
sum+=a[i];
if(sum>=holeW)
{
ans = min(ans,cnt);
}
}
if(ans!=INF) return ans;
return -;
}
};
int main()
{ return ;
}
SRM 628 DIV1 250
题意:给出一个n,让你求出一个最小的x,使得x^(x的约数的个数)=n
做法:枚举开n的多次幂,最多开到64.注意一个非1对的数的约数至少为2.。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<ctime>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
const int S = ;
LL poww(LL a,int n)
{
LL t;
if(n==) return ;
if(n==) return a;
t=poww(a,n/);
t=t*t;
if((n&)==) t=t*a;
return t;
}
int judge(LL x,int k)
{
int i;
int s = ;
int ans = ;
if(x==) return k==;
if(x==) return k==;
for(i = ; i*i<=x ; i++)
{
if(x%i==)
{
s = ;
while(x%i==)
{
x/=i;
s++;
}
ans*=(s+);
}
if(ans>k) return ;
}
if(x!=) ans*=;
if(ans==k) return ;
return ;
}
//int check(LL k,LL n,int i)
//{
// LL s = poww(k,i);
// if(s!=n) return 0;
//
// return 1;
//}
class DivisorsPower
{
public:
long findArgument(long n)
{
int i;
LL k;
for(i= ; i >= ; i--)
{
double a = pow(n*1.0,1.0/i);
k = round(a);
LL s = poww(k,i);
if(s!=n) continue;
if(judge(k,i))
{
return k;
}
}
return -;
}
};
int main()
{ return ;
}
SRM 627 DIV1 250
题意:给出一个字符串,两个不一样的字母可以同时消掉,问消到最后有哪些字母可能会留下。
做法:统计出每个字母的个数,枚举当前字母,若当前字母可以留下,则其它所有字母数量都需满足num[j]>sum-num[j]。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 111
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>q;
int num[];
string s;
class HappyLetterDiv1
{
public:
string getHappyLetters(string letters)
{
int n = letters.length();
int sum = ,i,j;
for(i = ; i < n; i++)
{
int d = letters[i]-'a';
sum+=d;
num[d]++;
}
for(i = ; i < ; i++)
{
if(!num[i]) continue;
if((num[i]==&&sum%==))
{
continue;
}
int tsum = sum,ff=;
if(sum%) tsum-=;
else tsum -= ;
for(j = ; j < ; j++)
if(i==j) continue;
else
{
if(num[j]>tsum)
{
ff = ;
break;
}
}
if(ff) s+=(i+'a');
}
return s;
} }; int main()
{ return ;
}
SRM 626 DIV1 250
题意:两人在玩骰子,小A玩a个b面的,小B玩c个d面的,问小A赢得期望。
做法:求出所有小A赢的可能性,用总分/所有的可能
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 55
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
double dp1[N][N*N],dp2[N][N*N];
class FixedDiceGameDiv1
{
public:
double getExpectation(int a, int b, int c, int d)
{
int i,j,g;
if(a*b<=c) return -;
for(i = ; i <= b; i++)
dp1[][i] = 1.0/b;
for(i = ; i <= d; i ++)
dp2[][i] = 1.0/d;
for(i = ;i <= a ; i++)
for(j = i ; j <= i*b ; j++)
{
for(g = ; g <= b; g++)
if(j<=g) continue;
else
dp1[i][j] += dp1[i-][j-g]/b;
}
for(i = ; i <= d; i++)
dp2[][i] = ;
for(i = ;i <= c ; i++)
for(j = i ; j <= i*d ; j++)
{
for(g = ; g <= d; g++)
if(j<=g) continue;
else
dp2[i][j] += dp2[i-][j-g]/d;
}
double ans = ;
double o = ;
for(i = ; i <= a*b ; i++)
{
if(!dp1[a][i]) continue;
for(j = ; j < i; j++)
if(dp2[c][j])
{
o+=dp1[a][i]*dp2[c][j];
ans+=i*1.0*dp1[a][i]*dp2[c][j];
}
}
return ans/o;
}
};
SRM 625 DIV1 250
题意:给你一个字符串,让你求出子串为回文串的可能性。
做法:组合的问题,如果是回文串的话,只有两种形式,奇数的 和偶数的,分情况讨论,分界点两边的各字母个数相同。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int o[];
LL num[];
LL cal(int x)
{
int i;
LL s = ;
for(i = ; i <= x; i++)
s*=i;
return s;
}
class PalindromePermutations
{
public:
double palindromeProbability(string word)
{
int k = word.length(),i,j;
double s = 1.0;
for(i = ; i < k; i++)
o[word[i]-'a']++;
if(k%==)
{
for(i = ; i < ; i++)
{
if(o[i]%) return ;
}
for(i = ; i< ; i++)
{
if(!o[i]) continue;
for(j = o[i]/+ ; j <= o[i] ; j++)
s*=j;
}
for(i = k/+ ; i <= k; i++)
{
s/=i;
}
}
else
{
double x=-;
for(i = ; i < ; i++)
{
if(o[i]%)
{
if(x!=-) return ;
x = i;
}
}
for(i = ; i< ; i++)
{
if(!o[i]) continue;
for(j = o[i]/+ ; j <= o[i] ; j++)
s*=j;
}
for(i = k/+ ; i <= k; i++)
{
s/=i;
}
}
return s;
}
};
SRM 624 DIV1 250
题意:从左到右有很多高楼,给出他们的高度,问至少有i个高度相同的建筑物需要最少提升多少高度,把这些异或起来。
做法:枚举提升j个与第i个高楼一样高度时候所需要的最小花费,排序即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 4010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int num[N][N],p,a[N];
bool cmp(int a,int b)
{
return p-a<p-b;
}
class BuildingHeights
{
public: int minimum(vector <int> heights)
{
int i,j,k = heights.size();
for(i = ; i < k; i++)
a[i] = heights[i];
sort(a,a+k);
for(i = ; i < k; i++)
{
p = heights[i];
sort(a,a+k,cmp);
int g = ;
for(j = ; j < k ; j++)
if(a[j]>p) {continue;}
else
{
num[i][g+] = num[i][g]+p-a[j];
g++;
}
cout<<p<<endl;
for(j = g+ ; j <= k; j++)
num[i][j] = INF;
}
int ans = ;
for(i = ; i <= k ; i++)
{
int ts = INF;
for(j = ; j < k ; j++)
ts = min(ts,num[j][i]);
cout<<ts<<endl;
ans^=ts;
}
return ans;
}
};
SRM 623 DIV1 250
题意:有一个n*m的矩阵里面种着梨和苹果还有空地,一次只能操作一个原有东西将其放在空地上,不能移除,问在k次操作内能得到的最大的一个只包含苹果的矩阵。
做法:枚举这个矩阵。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 4010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>; class UniformBoard
{
public: int getBoard(vector <string> board, int K)
{
int n = board[].size();
int p = ,a = ,d = ,i,j,x,y,g,e;
for(i = ; i< n; i++)
for(j = ; j < n; j++)
if(board[i][j]=='P')
p++;
else if(board[i][j]=='A') a++;
else d++;
int ans = ;
for(i = ; i < n ; i++)
for(j = ; j < n; j++)
{
for(x = i ; x < n; x++)
for(y = j ; y < n; y++)
{
int ta=,td=,tp = ;
for(g = i ; g <= x ; g++)
for(e = j ; e <= y ; e++)
if(board[g][e]=='P') tp++;
else if(board[g][e]=='A') ta++;
else td++;
int sum = (x-i+)*(y-j+);
if(a<sum) continue;
if(ta==sum) {ans = max(ans,sum);continue;}
else
{
if(d==) continue;
int o = tp+sum-ta;
// if(i==2&&x==2&&j==0&&y==2)
// cout<<o<<endl;
if(K<o) continue;
ans = max(ans,sum);
}
}
}
return ans;
}
};
SRM 620 DIV1 250
题意:对于(x,y)可以变为(x+y,y)和(x,x+y),给你abcd,问是否存在(x,y)可以变为(a,b) 和(c,d) 返回最大x+y的(x,y);
做法:暴力枚举a,b的前N步,vector[x]存y,在cd中二分查找是否出现过y
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 1000010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>ed[N];
int o[N];
class PairGame
{
public:
int maxSum(int a, int b, int c, int d)
{
if(a==c&&b==d) return a+b;
ed[a].push_back(b);
o[] = a;
int g = ,i;
while(a!=b)
{
if(b>a)
b=b-a;
else
{
a = a-b; o[g++] = a;
}
ed[a].push_back(b);
}
for(i = ; i < g; i++)
sort(ed[o[i]].begin(),ed[o[i]].end());
vector<int>::iterator it;
it = lower_bound(ed[c].begin(),ed[c].end(),d);
for(i = ; i < ed[c].size() ; i++)
cout<<ed[c][i]<<" ";
puts("");
if(it!=ed[c].end()&&*it==d)
{
return c+d;
}
while(c!=d)
{
if(d>c)
d=d-c;
else c = c-d;
//cout<<c<<" "<<d<<endl;
it = lower_bound(ed[c].begin(),ed[c].end(),d);
if(it!=ed[c].end()&&*it==d)
{
return c+d;
}
}
return -;
}
};
SRM 619 DIV1 250
题意:有n堆石头,每堆石头有A[i]个小石头,两人在玩游戏,每次将一堆分成两堆放在剩余的其它两堆上面,谁最后无法操作就输了。
做法:判断奇偶性,(n-2)%2为奇数先手赢,经过分类讨论1的个数,发现与1的 个数无关。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
class SplitStoneGame
{
public:
string winOrLose(vector <int> number)
{
int n = number.size();
int a = ,b=,i;
for(i = ; i < n; i++)
if(number[i]==)
a++;
b = n-a;
if(n<||b==) return "LOSE";
if(n%) return "WIN";
else return "LOSE";
}
};
SRM 618 DIV1 250
题意:以前做的,记不清了。。
做法:图的染色问题
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>f[];
int flag;
int vis[];
void dfs(int u,int c)
{
int i;
if(flag) return ;
vis[u] = c;
for(i = ; i < (int)f[u].size() ; i++)
{
int v = f[u][i];
if(!vis[v])
dfs(v,-c);
else if(vis[v]!=-c)
{
flag = ;
break;
}
}
}
class Family
{
public:
string isFamily(vector <int> parent1, vector <int> parent2)
{
int k = parent1.size();
int i;
for(i = ;i < k ; i++)
{
int u1 = parent1[i],u2 =parent2[i] ;
if(u1==u2)
{
if(u2!=-)
{
flag = ;
break;
}
}
else if(u1==-||u2==-)
{
flag = ;
break;
}
else if(u1>=i||u2>=i)
{
flag = ;
break;
}
else
{
f[u1].push_back(u2);
f[u2].push_back(u1);
}
}
for(i = ;i < k ;i++)
{
if(!vis[i])
dfs(i,);
if(flag) break;
}
if(flag)
return "Impossible";
else
return "Possible";
} };
SRM 616 DIV1 250
题意:一个人在睡觉,他有一个睡眠指数k,当k<=0时他会醒,他旁边有n个闹钟,每一个每隔一段时间会响,会使k减少,k值每分钟会增加d,问初始k值最大为多少时,他会在某个时刻醒。
做法:取每个闹钟的周期的LCM,以LCM枚举时间。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 10000000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
bool o[N];
class
WakingUp
{
public:
int maxSleepiness(vector <int> period, vector <int> start, vector <int> volume, int D)
{
int n = period.size(),i; int t = ,tt=;
for(i = ; i <= ; i++)
tt = i*tt/__gcd(i,tt);
int td = ;
int ans = ;
while(t<=tt)
{
td+=D;
for(i = ; i < n; i++)
{
if(t>=start[i]&&(t-start[i])%period[i]==)
td-=volume[i];
}
ans = max(ans,-td);
t++;
}
if(td<) return -;
return ans;
}
};
SRM 615 DIV1 250
题意:顺序给你一些数,有一个变形虫,初始体积不知道,当他遇到与它体积一样的数他就会扩大一倍,问有哪些数不是他最后可能的体积。
做法:枚举他初始的体积,依次向后走,途中的体积不可达,加入set中,走到最后会得到一个体积,抹除这个数。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
#define all(o) (o).begin(),(o).end()
class AmebaDiv1
{
public:
int count(vector <int> X)
{
int n = X.size(),i,j;
set<int>s(all(X));
for(i = ; i< n; i++)
{
int x = X[i];
for(j = ; j< n; j++)
{
if(x==X[j]) x*=;
}
s.erase(x);
}
return s.size();
}
};
SRM 614 DIV1 250
题意:有一个二维坐标平面,上面有些点,让你画出一个平行于坐标轴并且顶点为整数坐标的正方形圈住最少K个点,返回满足条件的最小正方形面积。
做法: 枚举正方形最左、最下、最右的点和最左、最上、最下的点。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 105
#define LL long long const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>tx;
struct point
{
int x,y;
point(int x=,int y=):x(x),y(y){}
}p[N]; bool cmp(point a,point b)
{
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
} class MinimumSquare
{
public:
long long minArea(vector <int> x, vector <int> y, int K)
{
int n = x.size(),i,j,g,e;
for(i = ; i < n; i++)
{
p[i] = point(x[i],y[i]);
}
LL ans = 4000000008000000007LL;
for(i = ; i < n; i++)
{
for(j = ; j < n; j++)
{
if(p[j].x<p[i].x) continue;
if(p[j].y>p[i].y) continue;
for(g = ; g < n; g++)
{
if(p[g].x<p[j].x) continue;
if(p[g].y<p[j].y) continue;
int tn = ;
LL len = max(p[g].x-p[i].x+,max(p[g].y-p[j].y+,p[i].y-p[j].y+));
for(e = ; e < n; e++)
{
if(p[e].y<p[j].y) continue;
if(p[e].x<p[i].x) continue;
if(p[e].y-p[j].y+<=len&&p[e].x-p[i].x+<=len)
tn++;
}
if(tn<K) continue;
ans = min(len*len,ans);
}
for(g = ; g < n; g++)
{
if(p[g].x<p[i].x) continue;
if(p[g].y<p[i].y) continue;
int tn = ;
LL len = max(p[g].y-p[j].y+,max(p[g].x-p[i].x+,p[j].x-p[i].x+));
for(e = ; e < n; e++)
{
if(p[e].y<p[j].y) continue;
if(p[e].x<p[i].x) continue;
if(p[e].y-p[j].y+<=len&&p[e].x-p[i].x+<=len)
tn++;
}
if(tn<K) continue;
ans = min(len*len,ans);
}
}
}
return ans;
}
};
SRM 613 DIV1 250
题意:X轴上有一些点,他们可以向左或者向右移动X,求min(R-L)。
做法:枚举以哪个点为轴进行其他点的移动。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF (2*1e9)
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
vector<int>co;
class TaroFriends
{
public:
int getNumber(vector <int> coordinates, int X)
{
int n = coordinates.size();
int ans = INF,i,j;
sort(coordinates.begin(),coordinates.end());
for(i = ;i < n; i++)
{
co.clear();
int x = coordinates[i]+X;
co.push_back(x);
for(j = ;j < n ; j++)
if(j<i) co.push_back(coordinates[j]+X);
else if(j>i) co.push_back(coordinates[j]-X);
sort(co.begin(),co.end());
ans= min(ans,co.back()-co[]);
co.clear();
x = coordinates[i]-X;
co.push_back(x);
for(j = ;j < n ; j++)
if(j<i) co.push_back(coordinates[j]+X);
else if(j>i) co.push_back(coordinates[j]-X);
sort(co.begin(),co.end());
ans= min(ans,co.back()-co[]);
}
return ans;
}
};
SRM 612 DIV1 250
题意:聊天框里有一个字符,你需要打出k个字符,而且你只能做三种操作,复制当前框里所有字符,粘贴,或者删除一个字符,每秒只能操作一次,问最少的操作。
做法:dp,dp[i] = min(dp[i],dp[j]+1+粘贴次数-删除部分)。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 1010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int dp[N];
class
EmoticonsDiv1
{
public:
int printSmiles(int smiles)
{
int n = smiles,i,j;
dp[] = ;
for(i = ; i <= n; i++)
{
dp[i] = INF;
for(j = ; j < i ; j++)
{
int k = i/j;
if(i%j) k++;
dp[i] = min(dp[i],dp[j]+k+k*j-i);
}
cout<<i<<" "<<dp[i]<<endl;
}
return dp[n];
} };
SRM 611 DIV1 250
题意:给你一个字符串是不是符合这样的条件,一个数字k要么没出现,要么只出现两次并且前后相隔k个数字。
做法:保存位置,判断即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 1010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int dp[N];
vector<int>num[];
class
InterestingNumber
{
public:
string isInteresting(string x)
{
int i,j,n = x.length();
for(i = ; i < n; i++)
{
num[x[i]-''].push_back(i);
}
for(i = ; i <= ; i++)
{
if(num[i].size()==) continue;
if(num[i].size()!=) return "Not interesting";
for(j = ; j < num[i].size() ; j++)
if(num[i][j]-num[i][j-]-!=i) return "Not interesting";
}
return "Interesting";
} };
SRM 610 DIV1 250
题意:给出一个01矩阵,找出最大的01混合的矩形。
做法:枚举以i,j为左下角的符合条件的矩形,x往右走,y往上走,边走边更新ans.
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 1010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int dp[N];
vector<int>num[];
class
TheMatrix
{
public:
int MaxArea(vector <string> board)
{
int n = board.size(),i,j,g,e,m = board[].size();
int ans = ;
for(i = ; i < n; i++)
for(j = ; j <m ; j++)
{
int x = ,y=;
for(g = i- ; g >= ; g--)
if(board[g+][j]==board[g][j]) break;
else y = i-g+;
ans =max(ans,x*y);
for(g = j+; g < m ; g++)
{
if(board[i][g]==board[i][g-]) break;
x = g-j+;
if(i==) continue;
int ty = ;
for(e = i- ; e >= ; e--)
{
if(board[e][g]==board[e+][g]) break;
ty = i-e+;
}
y = min(ty,y);
ans = max(ans,x*y);
}
ans = max(ans,x*y);
}
return ans;
} };
SRM 609 DIV1 250
题意:给你一个由‘<’和'>'组成的字符串,每一个操作可以删除一个字符,问最少操作得到的字符串 ,需符合>><<这种形式,前面k个>后面k个<,返回得到的字符串的长度。
做法:枚举哪个位置为最后字符串的第一个'<'。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
class
MagicalStringDiv1
{
public:
int getLongest(string S)
{
int n = S.length(),i,j;
int len = ;
int minz = INF;
int flag = ;
int ts = ;
int a = ;
for(i = ; i < n; i++)
{
if(S[i]=='>') {flag = ;a++;continue;}
if(S[i]=='<'&&!flag) {ts++;continue;}
int ss = ts,b = ;
for(j = i ; j < n; j++)
if(S[j]=='>') ss++;
else b++;
ss+=abs(a-b);
ts++;
cout<<ss<<" "<<a<<" "<<b<<endl;
if(ss<minz)
{
len = min(a,b)*;
minz = ss;
}
}
return len;
}
};