Codeforces 1064

比赛链接

唉 人生啊

during the contest:



system test:



一位博友突然失去了梦想

A.Make a triangle!

不放了。

B.Equations of Mathematical Magic

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=1e6+7; inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
} int main()
{
int T=read();
while(T--)
{
long long a=read(), ans=1;
for(int i=31; i>=0; --i)
if(a>>i&1) ans<<=1ll;
printf("%I64d\n",ans);
}
return 0;
}

C.Oh Those Palindromes

sb了很长时间。。

为什么要把很多字符拼起来呢,同种字符放在一起就行了。

Why so? 注意到一个回文串首尾至少是两个相同字符,所以一个出现x次的字符最多可以(以它为首尾)形成x(x-1)/2个回文串,这和单个放一起的上界是一样的。

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=1e5+5; 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 tm[N];
char s[N],Ans[N]; int main()
{
int n=read();
scanf("%s",s+1);
std::sort(s+1,s+1+n);
s[n+1]='\0', puts(s+1); return 0;
}

比赛结束后

D.Labyrinth(BFS)

  因为BFS一般是可以保证到达一个点时代价最小,所以考虑BFS。

  本题有向左和向右两个代价。显然如果上下没有格子阻碍,我们可以先一直向上下扩展状态。

  然后,如果一直向左或是向右,那么BFS到的点显然代价最小。

  如果要走回头路(即直接向上的路被堵住,但能绕上去),那么假设向左边走要走\(a\)步才能向右走绕回去,向右边走要\(b\)步才能向左走绕回去。那么前者向右也要走\(a\)步,后者向左也要走\(b\)步。所以我们只保证向左走的步数尽量少,就可以保证到达某一个点时向右走的步数也尽量少。好像也很符合BFS性质?直接BFS?

  但是向左走的步数少不代表距离近。比如下图,起点为白点,要到达绿点,显然从白点向左出发到它花费少(花费指向左向右的步数)。但是如果直接BFS,因为左边上上下下浪费了时间,所以实际先到绿点的是向白点右边出发的路径。这当然不优(花费多)。



  因为向上向下是不计代价的(也就是距离为\(0\)),所以BFS到一个点时,要把它上下能直接到的点同时加入队列(代价相同,也就是距离相同,当然在BFS里要同时入队),而不是在把它弹出队首时才在队列中加入上下的点(BFS的意义?后加入的点花费/距离比之前的点高。但实际它们代价是一样的)。

//62ms	4100KB
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=2005;
const int D[]={-1,1}; int n,m,r,c,X,Y;
bool ok[N][N];
struct Node
{
int x,y,r1,r2;
};
std::queue<Node> q; 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 Extend(int x,int y,int r1,int r2)
{
for(int i=x; ok[i][y]; --i)
ok[i][y]=0, q.push((Node){i,y,r1,r2});
for(int i=x+1; ok[i][y]; ++i)
ok[i][y]=0, q.push((Node){i,y,r1,r2});
}
void BFS()
{
int ans=0;
Extend(r,c,X,Y);
while(!q.empty())
{
++ans;
Node tmp=q.front(); q.pop();
int x=tmp.x,y=tmp.y,r1=tmp.r1,r2=tmp.r2;
if(r1&&ok[x][y-1]) Extend(x,y-1,r1-1,r2);
if(r2&&ok[x][y+1]) Extend(x,y+1,r1,r2-1);
}
printf("%d\n",ans);
} int main()
{
n=read(),m=read(),r=read(),c=read(),X=read(),Y=read();
for(int i=1; i<=n; ++i)
{
register char c=gc(); while(c!='*'&&c!='.') c=gc();
ok[i][1]=c=='.';
for(int j=2; j<=m; ++j) ok[i][j]=gc()=='.';
}
BFS(); return 0;
}

E.Dwarves,Hats and Extrasensory Abilities(交互 二分)

容易想到根据点的颜色,不断二分调整询问位置。

但是\(2^{30}>10^9\),\(30\)次二分要出事。。

我们可以先询问一次端点(\(0\)或\(10^9\)),后面的二分再根据这个判断位置,就是\(29\)次二分啦。

pretest直接\(30\)次二分都可以过的吗。。明明大于1e9我怎么当时就觉得可以了呢。。

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=2e6+5; inline char Get(int p)
{
static char s[233];
printf("10 %d\n",p), fflush(stdout);
scanf("%s",s); return s[0];
} int main()
{
int n; scanf("%d",&n);
int s=Get(0),l=0,r=1e9,mid;
for(int i=1; i<n; ++i)
{
mid=l+r>>1;
if(Get(mid)==s) l=mid;
else r=mid;
}
printf("9 %d 11 %d\n",l,l+1), fflush(stdout); return 0;
}

F.Candies for Children

大概是按照\(n\)的大小两种算法(暴力?)结合?

坑了


未AC代码

D.Labyrinth

#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=2005; inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
} const int D[]={-1,1};
int n,m,r,c,X,Y;
char s[N];
bool mp[N][N],vis[N][N];
struct Node
{
int x,y,r1,r2;
};
std::queue<Node> q; void BFS()
{
int ans=0;
q.push((Node){r,c,X,Y});
vis[r][c]=1;
while(!q.empty())
{
++ans;
Node tmp=q.front(); q.pop();
int x=tmp.x,y=tmp.y,r1=tmp.r1,r2=tmp.r2;
for(int i=x-1; i>0; --i)
{
if(!mp[i][y]) break;
if(vis[i][y]) break;//continue;
vis[i][y]=1, q.push((Node){i,y,r1,r2});
}
for(int i=x+1; i<=n; ++i)
{
if(!mp[i][y]) break;
if(vis[i][y]) break;//continue;
vis[i][y]=1, q.push((Node){i,y,r1,r2});
}
for(int i=0; i<2; ++i)
{
int yn=y+D[i];
if(!mp[x][yn]||vis[x][yn]) continue;
vis[x][yn]=1;
if(r1>0&&i==0) q.push((Node){x,yn,r1-1,r2});
if(r2>0&&i==1) q.push((Node){x,yn,r1,r2-1});
}
}
printf("%d\n",ans);
} int main()
{
n=read(),m=read(),r=read(),c=read(),X=read(),Y=read();
for(int i=1; i<=n; ++i)
{
scanf("%s",s+1);
for(int j=1; j<=m; ++j)
if(s[j]=='.') mp[i][j]=1;
else mp[i][j]=0;
}
BFS(); return 0;
}

E.Dwarves,Hats and Extrasensory Abilities

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define mp std::make_pair
#define pr std::pair<int,int>
typedef long long LL;
const int N=2e6+5; struct Node
{
int col,pos;//0:w 1:b
bool operator <(const Node &x)const
{
return col==x.col?pos<x.pos:col<x.col;
}
}p[N]; bool Check(int pos,int now)
{
static char s[233];
p[now].pos=pos;
printf("10 %d\n",pos); fflush(stdout);
scanf("%s",s+1);
if(s[1]=='w') return p[now].col=0,0;
else return p[now].col=1,1;
} int main()
{
int n;
scanf("%d",&n);
int l=1,r=1e9,mid;
for(int i=1; i<=n; ++i)
{
mid=l+r>>1;
if(Check(mid,i)) r=mid;
else l=mid+1;
}
int ans=0;
std::sort(p+1,p+1+n);
for(int i=2; i<=n; ++i)
if(p[i].col!=p[i-1].col)
{
ans=p[i-1].pos+1;
break;
}
// for(int i=1; i<=n; ++i) printf("%d:%d\n",p[i].col,p[i].pos);
if(ans!=0) printf("9 %d 11 %d\n",ans-1,ans), fflush(stdout);
else printf("9 %d 11 %d\n",ans,ans), fflush(stdout); return 0;
}

Codeforces Round #516 (Div. 2) (A~E)的更多相关文章

  1. Codeforces Round #516 (Div. 2)D. Labyrinth(BFS)

    题目链接:http://codeforces.com/contest/1064/problem/D 题目大意:给你一个n*m的图,图中包含两种符号,'.'表示可以行走,'*'表示障碍物不能行走,规定最 ...

  2. Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth

    http://codeforces.com/contest/1064/problem/D 向上/向下加0,向左/右加1, step = 0,1,…… 求的是最少的步数,所以使用bfs. step=k ...

  3. Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth(重识搜索)

    https://codeforces.com/contest/1064/problem/D 题意 给你一个有障碍的图,限制你向左向右走的次数,问你可以到达格子的个数 思路 可以定义状态为vi[x][y ...

  4. Codeforces Round #516 (Div. 2)D. Labyrinth

    D. Labyrinth 题目链接:https://codeforces.com/contest/1064/problem/D 题意: 给出一个n*m的矩阵以及人物的起点,并且给出x,y,分别代表这个 ...

  5. Codeforces Round #516 (Div. 2, by Moscow Team Olympiad)

    题目链接 A. Make a triangle! 题意 让某段最少增加多少使得构成三角形 思路 让较小两段往最长段去凑 代码 #include <bits/stdc++.h> #defin ...

  6. Codeforces Round #516(Div 2)

    比赛链接:传送门 A. Make a triangle!(简单思维) 题目大意: 给你三条边,问你最多加多少长度能使这三条边能构成三角形. 思路: 最大边小于答案加另外两条边的和. #include ...

  7. Codeforces Round#516 Div.1 翻车记

    A:开场懵逼.然后发现有人1min过,于是就sort了一下,于是就过了.正经证明的话,考虑回文串两端点一定是相同的,所以最多有Σcnti*(cnti+1)/2个,cnti为第i种字母出现次数.而sor ...

  8. [Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) ](A~E)

    A: 题目大意:给你$a,b,c$三条边,可以给任意的边加任意的长度,求最少共加多少长度使得可以构成三角形 题解:排个序,若可以组成,输出$0$,否则输出$c-a-b+1(设a\leqslant b\ ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. 算法时间复杂度和NP问题简介

    这里主要简单说一下算法的时间复杂度和NP问题简介,毕竟分析算法的时间复杂度上界有助于分析算法的好坏,分析算法好坏也有助于分析是否还有更好的算法: 一.时间复杂度: 一般关心的还有递归问题中的时间复杂度 ...

  2. Linux命令:pigz多线程压缩工具【转】

    学习Linux系统时都会学习这么几个压缩工具:gzip.bzip2.zip.xz,以及相关的解压工具.关于这几个工具的使用和相互之间的压缩比以及压缩时间对比可以看:Linux中归档压缩工具学习 那么P ...

  3. 018_nginx_proxy死循环问题

    今天线上遇到一个请求一次,触发多次的请求,而且直接把nginx机器压垮了.经排查,经过如下: 一. server{ server www.jyall.com; location /latestrele ...

  4. activiti报错ProcessEngines.getDefaultProcessEngine()为null

    activiti报错ProcessEngines.getDefaultProcessEngine()为null 文件名错误,默认加载classpath下面的activiti.cfg.xml,而不是ac ...

  5. JavaScript的类型自动转换样例集合处

    1.前言 如果Javascript期望使用一个字符串,它会把给定的值转换成字符串:如果Javascript期望使用一个数字,它会把给定的值转化成数字. 2.样例 2.1.字符串拼接时有数字 windo ...

  6. javascript 什么类型没有toString()?

    JS里面任何对象都有toString()方法么?不是! null和undefined就没有!虽然null用typeof看的时候,是object类型的. 另外number对象调用toString()会报 ...

  7. Java EE 8 来了

      作者 李士窑 发布于 2014年9月2日. 估计阅读时间: 不到一分钟 | 自2013年6月Java EE 7发布以来,Java开发团队在这段时间内一直在规划和搜集下一个大版本Java EE 8带 ...

  8. 小技巧css解决移动端ios不兼容position:fixed属性,无需插件

    移动端开发仿app头部底部固定设置position:fixed,android2.2以上已经实现.但是在ios8以下系统,当小键盘激活时,都会出现位置浮动问题.如图: 如何解决: 查阅资料之后想到一下 ...

  9. sql基础笔记备忘

    MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型:tinyint smallint medium ...

  10. cf 1041C双指针

    比赛的时候想着用单调队列做... 打完发现其实就是个双指针 /* 构造双指针解决即可 */ #include<iostream> #include<cstring> #incl ...