Codeforces Round #505 (Div 1 + Div 2) (A~D)
Codeforces 1025
为什么我room里的都不hack别人。。那么明显的错。。
A.Doggo Recoloring
//只要能找到一个出现次数大于等于2的颜色就可以了。注意n=1特判。。
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=1e5+5;
int tm[300];
char s[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
int n=read(); scanf("%s",s+1);
if(n==1) return puts("Yes"),0;
for(int i=1; i<=n; ++i) ++tm[s[i]];
for(int i='a'; i<='z'; ++i) if(tm[i]>1) return puts("Yes"),0;
puts("No");
return 0;
}
B.Weakened Common Divisor
任意一对总要有个满足的,任找一对分解质因数就可以了。
分解质因数的过程很zz。。不用筛直接枚举到sqrt(n)就行了。。
分解完后要判一次。
//61ms 2400KB
#include <cmath>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=2e5+5;
int n,A[N],B[N],P[N],cnt;
bool not_P[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
void Make_Table(int n)
{
for(int i=2; i<=n; ++i)
{
if(!not_P[i]) P[++cnt]=i;
for(int j=1; j<=cnt&&i*P[j]<=n; ++j)
{
not_P[i*P[j]]=1;
if(!(i%P[j])) break;
}
}
}
bool Check(int x)
{
for(int i=1; i<=n; ++i) if(A[i]%x && B[i]%x) return 0;
return 1;
}
int main()
{
n=read();
for(int i=1; i<=n; ++i) A[i]=read(), B[i]=read();
Make_Table(sqrt(std::max(A[1],B[1])));
int now=A[1];
for(int i=1; i<=cnt&&P[i]<=now; ++i)
if(!(now%P[i]))
{
if(Check(P[i])) return printf("%d\n",P[i]),0;
if(P[i]*P[i]!=now && now!=P[i] && Check(now/P[i])) return printf("%d\n",now/P[i]),0;
while(!(now%P[i])) now/=P[i];
}
if(now!=1 && Check(now)) return printf("%d\n",now),0;
now=B[1];
for(int i=1; i<=cnt&&P[i]<=now; ++i)
if(!(now%P[i]))
{
if(Check(P[i])) return printf("%d\n",P[i]),0;
if(P[i]*P[i]!=now && now!=P[i] && Check(now/P[i])) return printf("%d\n",now/P[i]),0;
while(!(now%P[i])) now/=P[i];
}
if(now!=1 && Check(now)) return printf("%d\n",now),0;
puts("-1");
return 0;
}
C.Plasticine zebra
找些串试试可以发现(串只有两种字符!真不懂我为什么拿abcd试半天),不管怎么切都相当于将串循环右移某些长度。
所以在循环表示里找最长的就可以了。注意答案对原串长度取min!
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=2e5+5;
char s[N];
int main()
{
scanf("%s",s+1);
int l=strlen(s+1),res=0;
for(int i=1; i<=l; ++i) s[i+l]=s[i];
l<<=1;
for(int now=1,i=2; i<=l; ++i,res=std::max(res,now))
if(s[i]!=s[i-1]) ++now;
else now=1;
printf("%d\n",std::min(res,l>>1));
return 0;
}
比赛结束后
D.Recovering BST(DP (bitset))
\(f[l][r][rt]\)表示\(l\sim r\)以\(rt\)为根是否可行,转移时枚举\(l,r,rt\),再枚举左右子树的根。当存在\(f[l][rt-1][rt_{left}]==1\ \&\&\ f[rt+1][r][rt_{right}]==1\ \&\&\ rt_{left},rt_{right}均可与rt连边\) 时,\(f[l][r][rt]=1\)。
最后一层枚举总共是\(O(r-l)\)的,一共\(O(n^4)\)。
但是一个区间是否合法,我们只需要知道左右能否拼起来。我们用\(L[l][k]/R[k][r]\)表示以\(k\)为根往左/右是否能延伸到\(l/r\)。
那么区间\([l,r]\)合法 当且仅当存在k满足 \(L[l][k]==1\ \&\&\ R[k][r]==1\)。当\([l,r]\)合法后就可以根据\(k\)更新\(L[l][r+1]\)和\(R[l-1][r]\)了。
这样只需枚举区间和子树的根,复杂度\(O(n^3)\)。
可以用bitset优化。
数据感觉好水啊。。L,R更新都错了还是在51个点WA。
另外果然有更神奇的做法。。贪心么。。?
//202ms 1800KB
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
const int N=707;
int n,A[N];
bool ok[N][N],L[N][N],R[N][N],ans[N][N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int Gcd(int a,int b){
return b?Gcd(b,a%b):a;
}
int main()
{
n=read();
for(int i=1; i<=n; ++i) A[i]=read(), L[i][i]=R[i][i]=1;
for(int i=1; i<n; ++i)
for(int j=i+1; j<=n; ++j) ok[i][j]=(Gcd(A[i],A[j])>1);
for(int len=0; len<n; ++len)//得先用长度为1的区间更新一次。。
for(int l=1,r; (r=l+len)<=n; ++l)
for(int k=l; k<=r; ++k)
if(L[l][k] && R[k][r])
ans[l][r]=1, L[l][r+1]|=ok[k][r+1], R[l-1][r]|=ok[l-1][k];
puts(ans[1][n]?"Yes":"No");
return 0;
}
bitset:
//93ms 100KB
#include <cstdio>
#include <cctype>
#include <bitset>
#include <algorithm>
#define gc() getchar()
const int N=707;
int n,A[N];
std::bitset<N> ok[N],L[N],R[N],ans[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int Gcd(int a,int b){
return b?Gcd(b,a%b):a;
}
int main()
{
n=read();
for(int i=1; i<=n; ++i) A[i]=read(), L[i][i]=R[i][i]=1;
for(int i=1; i<n; ++i)
for(int j=i+1; j<=n; ++j) ok[i][j]=ok[j][i]=(Gcd(A[i],A[j])>1);
for(int len=0; len<n; ++len)
for(int l=1,r; (r=l+len)<=n; ++l)
{
ans[l]=L[l]&R[r];
if((ans[l]&ok[r+1]).count()) L[l][r+1]=1;
if((ans[l]&ok[l-1]).count()) R[r][l-1]=1;//l-1是第二维啊
}
puts(ans[1].count()?"Yes":"No");
return 0;
}
Codeforces Round #505 (Div 1 + Div 2) (A~D)的更多相关文章
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- hdu 2433 Travel
http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...
- 雨林木风ghostwin7纯净版系统下载
雨林木风ghostwin7纯净版系统下载 关于easyuidatagrid的问题,跪求老司机带带我..... 关于cst_modesys/stat.h一个问题求解答谢谢 [程序]STM32使用SPI接 ...
- expect 交互 之shell执行命令操作
shell 执行命令操作 /usr/bin/expect -c " proc jiaohu {} { send_user expect_start expect { password { s ...
- js基础知识:闭包,事件处理,原型
闭包:其实就是js代码在执行的时候会创建变量对象的一个作用域链,标识符解析的时候会沿着作用域链一级一级的网上搜索,最后到达全局变量停止.所以某个函数可以访问外层的局部变量和全局变量,但是访问不了里层的 ...
- Jenkins的安装及使用(一)
操作环境:Windows7 一.环境准备 1 安装JDK 本文采用jdk-8u111-windows-x64.exe: 安装完成后配置环境变量. 2 配置tomcat 本文采用tomcat8,免安装版 ...
- UART中的硬件流控RTS与CTS【转】
转自:http://blog.csdn.net/zeroboundary/article/details/8966586 5/23/2013 5:13:04 PM at rock-chips insh ...
- sort命令的k选项大讨论【转】
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- eclipse安装阿里巴巴java开发规范插件
阿里巴巴java开发规范插件 作为JAVA开发人员,始终没有一个明确的规范,何为好代码,何为坏代码,造成不同人的代码风格不同,接手别人代码后改造起来相当困难.前不久,阿里巴巴发布了<阿里巴巴Ja ...
- 数据库索引和SQL语句使用经验
1.如果检索数据量超过30%的表中记录数,使用索引将没有显著的效率提高 2.在特定情况下,使用索引也许会比全表扫描慢,但这是同一个数量级上的差距:而通常情况下,使用索引比全表扫描要快几倍乃至几千倍! ...
- 008_MAC 终端使用技巧
一.常用终端命令. <1>reset 的作用很简单——将目前「终端」屏幕上的内容清空,就好像刚刚打开终端一样. <2>如果你在一条终端命令中发现有输入错误的话,那么用 cont ...