Codeforces Round #620 (Div. 2) 题解
A. Two Rabbits
思路:
很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = +;
int main(){
int k;
scanf("%d",&k);
while(k--){
long long x,y,a,b,ans;
cin>>x>>y>>a>>b;
if((y-x)%(a+b)) ans=-;
else ans=(y-x)/(a+b);
cout<<ans<<endl;
}
}
B. Longest Palindrome
思路:
直接暴力找对于每个串的反串(也就是对于每个i都有a[i]==a[n-i-1]),然后接着再前面没有被找到过的串中找一个自身也是回文串的串放在答案串的中间即可
写的稍稍有一些麻烦
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=;
string s[maxn];
int flag[maxn];
string ans="",temp="";
int main()
{
int n,m,cnt=,x,y;
cin>>n>>m;
for(int i=;i<=n;i++) cin>>s[i],flag[i]=;
for(int i=;i<=n;i++){
if(flag[i]) continue;
for(int j=i+;j<=n;j++){
y=;
for(int k=;k<m;k++){
if(s[i][k]!=s[j][m-k-]){
y=;
break;
}
}
if(!y){
ans+=s[i];
flag[j]=;
flag[i]=;
cnt+=;
break;
}
}
if(!y) continue;
}
for(int i=;i<=n;i++){
if(flag[i]) continue;
int x=;
for(int j=;j<m/;j++){
if(s[i][j]!=s[i][m-j-]){
x=;
break;
}
}
if(!x){
temp+=s[i];
cnt++;
flag[i]=;
break;
}
}
if(cnt==){
cout<<""<<endl<<endl;
return ;
}
cout<<cnt*m<<endl;
cout<<ans<<temp;
for(int i=ans.size()-;i>=;i--) cout<<ans[i];
return ;
}
C. Air Conditioner
思路:
先对顾客到店的时间进行排序,之后对于每一个顾客判断[Li , Ri] 与 [LI - t ,Ri + t] 是否有交集即可
#include <bits/stdc++.h>
#define maxn 1000010
using namespace std;
#define ll long long
struct node{
ll t,l,h;
}costomer[];
int main() {
int t;
scanf("%d",&t);
while(t--){
int n;
ll m;
scanf("%d%lld",&n,&m);
ll mi=m,ma=m,tmp=;
int kk=;
for (int i = ; i <n ; ++i) {
cin>>costomer[i].t>>costomer[i].l>>costomer[i].h;
mi-=(costomer[i].t-tmp);
ma+=(costomer[i].t-tmp);
if(ma<costomer[i].l||mi>costomer[i].h){kk=;}
else {
mi=max(mi,costomer[i].l);
ma=min(ma,costomer[i].h);
tmp=costomer[i].t;
}
}
if(kk)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return ;
}
D. Shortest and Longest LIS
思路:
直接贪心即可,最短序列的话,对于一串大于号,我们把当前未使用过的数尽可能的放在左边
最长序列就是反过来,尽可能的放未使用过的小的数即可
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=2e5+;
int ans[maxn];
int main()
{
int t,n;
cin>>t;
string s;
while(t--){
cin>>n>>s;
int num=n,las=;
for(int i=;i<n;i++){
if(s[i]=='>'||i==n-){
for(int j=i;j>=las;j--)
ans[j]=num--;
las=i+;
}
}
for(int i=;i<n;i++) printf("%d ",ans[i]);
cout<<endl;
num=,las=;
for(int i=;i<n;i++){
if(s[i]=='<'||i==n-){
for(int j=i;j>=las;j--)
ans[j]=num++;
las=i+;
}
}
for(int i=;i<n;i++) printf("%d ",ans[i]);
cout<<endl;
}
return ;
}
E. 1-Trees and Queries
思路:
只要找到,a到b的最短路径跟k的关系就好了
这个关系就是:只要最短路径长度小于K,并且奇偶性跟K相同即可
证明的话很简单:如果走到终点还没到路径长度还没到k的话,就可以不停的往回走一次,再走向终点,这样路程的长度就为dis+2i,要想最后能回到终点很明显x要与k同奇偶
最短只需要通过求两点的LCA就可以求出,当然新加进来的一条边会对路径产生影响
但是我们只要再判断dis(a,x)+dis(b,y)+1 与 dis(a,y)+dis(b,x)+1 即可
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 100005
using namespace std;
struct edge{
int next,v;
}edges[maxn*];
int head[maxn],cnt,dep[maxn],f[maxn][],n,q;
void init()
{
memset(head,-,sizeof(head));
cnt=;
}
void addedge(int u,int v)
{
edges[cnt].next=head[u];
edges[cnt].v=v;
head[u]=cnt++;
}
void dfs(int u,int fa)
{
dep[u]=dep[fa]+;
for(int i=;i<=;i++)
f[u][i+]=f[f[u][i]][i];
for(int i=head[u];i!=-;i=edges[i].next)
{
int v=edges[i].v;
if(v==fa)
continue;
f[v][]=u;
dfs(v,u);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
for(int i=;i>=;i--)
{
if(dep[f[x][i]]>=dep[y])
x=f[x][i];
if(x==y)
return x;
}
for(int i=;i>=;i--)
{
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
}
return f[x][];
}
int getdis(int a,int b){return dep[a]+dep[b]-*dep[lca(a,b)];}
int main()
{
scanf("%d",&n);
init();
int u,v,q,a,b,x,y,k;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v),addedge(v,u);
}
dfs(,);
scanf("%d",&q);
while(q--){
int flag=;
scanf("%d%d%d%d%d",&x,&y,&a,&b,&k);
int len1=getdis(a,b),len2=getdis(a,x)+getdis(b,y)+,len3=getdis(a,y)+getdis(b,x)+;
if(len1<=k&&(k-len1)%==) flag=;
if(len2<=k&&(k-len2)%==) flag=;
if(len3<=k&&(k-len2)%==) flag=;
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
Codeforces Round #620 (Div. 2) 题解的更多相关文章
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- k8s Learning Notes
Kubernetes - 组件介绍 MESOS APACHE 分布式资源管理框架 2019-5 Twitter > Kubernetes Docker Swarm 2019-07 阿里云宣布 D ...
- MySQL双机热备环境搭建
一. 前期准备 准备两台服务器(电脑),接入到同一局域网中,能够使双方可以ping通: 安装MySQL数据库,具体安装方法网上很全面,但是安装的版本需保持一致: 服务器IP地址设置. l A服 ...
- Ansible playbooks(任务、角色、模板、变色器、)
playbooks配置文件: [root@ansible ~]# vim /etc/ansible/hosts [test01] 192.168.200.114 [test02] 192.168.20 ...
- Shiro入门学习之自定义Realm实现授权(五)
一.自定义Realm授权 前提:认证通过,查看Realm接口的继承关系结构图如下,要想通过自定义的Realm实现授权,只需继承AuthorizingRealm并重写方法即可 二.实现过程 1.新建mo ...
- Ubuntu 安装 uWSGI
uWSGI官方网址: https://pypi.org/project/uWSGI/ 使用如下命令安装: pip install uWSGI 报如下错: Collecting uWSGI Using ...
- MySQL主从复制原理的是啥?
主库将变更写binlog日志,然后从库连接到主库之后,从库有一个IO线程,将主库的binlog日志拷贝到自己本地,写入一个中继日志中. 接着从库中有一个SQL线程会从中继日志读取binlog,然后执行 ...
- /var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure world writable dir /home/python/.local in PATH, mode 040777 解决方案
/var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure worl ...
- Navicat for MySQL怎么往表中填数据
只有往表中更新数据,数据库才会起到真正的作用. 工具/原料 仔细阅读 方法/步骤 1.打开数据库,首先连接localhost,如图所示. 2.连接成功后,右侧便会显示已经建成的表,找到要修改的表, ...
- Spring Boot JDBC 使用教程
总是要用到数据库的嘛,曾经我一度以为,写代码,编程就是搞数据库增删改查,甚至你设计一个系统,大部分时候在为如何设计关系型数据库努力,究其原因,是因为关系型数据库是逻辑的主要呈现. 这个系列,主要是对 ...
- Django安装数据库MySQLdb
在使用Django搭建与树莓派智能终端时,使用mysql作为数据库管理,遇到如下问题: django.core.exceptions.ImproperlyConfigured: Error loadi ...