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个折扣的时候,你必须 ...
随机推荐
- Web基础了解版07-EL表达式-运算符-11个隐式对象
EL EL(Expression Language)是JSP内置的表达式语言,用以访问页面的上下文以及不同作用域中的对象 ,取得对象属性的值,或执行简单的运算或判断操作.EL在得到某个数据时,会自动进 ...
- Mybatis 的分页插件 PageHelper
我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 jsqlparser-0.9.4.jar 使用方法: 1.根据Mybatis的版本下载对应版 ...
- MYSQL双查询错误2
一.关键点 MYSQL双查询错误之所以产生,有两个关键点: (1)SQL语句中使用GROUP BY语句时会生成临时表: (2)RAND()在查询和存储时生成的随机数有可能不同. 补充:======== ...
- C语言:计算输出给定数组中每相邻两个元素的平均值的平方根之和。
//计算输出给定数组中每相邻两个元素的平均值的平方根之和. #include <stdio.h> #include <math.h> ]) { double a,b,s=0.0 ...
- maplotlib python 玩具绘图 横向纵向条状图
from matplotlib import font_manager#解决zh-han图形汉字乱码 my_font = font_manager.FontProperties(fname=" ...
- ASP.NET Core搭建多层网站架构【3-xUnit单元测试之简单方法测试】
2020/01/28, ASP.NET Core 3.1, VS2019, xUnit 2.4.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[3-xUnit单元测试 ...
- 理解CART决策树
CART算法 原理 CART全称为Classification and Regression Tree. 回归树 相比ID3,CART遍历所有的特征和特征值,然后使用二元切分法划分数据子集,也就是每个 ...
- Spring Boot Log 日志使用教程
我们编写任何 Spring Boot 程序,可能绕不开的就是 log 日志框架(组件). 在大多数程序员眼中日志是用来定位问题的.这很重要. 本项目源码下载 注意本项目提供的源码已在后期重新编写,有部 ...
- Codeforces Round #611 (Div. 3) E
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past ...
- WPS/office使用技巧系列
一 WPS中如果要对比2个文档,不想以标签形式打开而是以多窗口形式,该怎么操作? 点击WPS左上角的蓝色或绿色框-->选项->->视图->勾选在任务栏中显示所有窗口,恢复书签模 ...