Educational Codeforces Round 58 (Rated for Div. 2) 

题目总链接:https://codeforces.com/contest/1101

A. Minimum Integer

题意:

多组数据,给你三个数l,r,d,要求在区间[l,r]之外找一个最小的x,使得x%d==0。

题解:

当d<l or d>r的时候,直接输出d就好了。

当l<=d<=r的时候,找到最小的t,使得t*d>r就行了。

具体操作见代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int q;
ll l,r,d; int main(){
cin>>q;
while(q--){
cin>>l>>r>>d;
if(d<l || d>r) cout<<d<<endl;
else{
ll now = r/d;
cout<<(now+)*d<<endl;
}
}
return ;
}

B. Accordion

题意:

给出一个字符串,要求删去一些数后它由以下几个部分组成,[ : .... : ],这其中"...."指的是0个或多个“ | ”。

问满足上面条件时,留下的字符串的最大长度为多少。

题解:

模拟一下就好了,从左往右遍历找 [ 以及 :

然后再从右往左遍历找 ] 以及 :

最后找中间的 |

这只是大体思路,代码还需要判断这些是否存在(我就是没判断这个被hack了...)、是否满足条件。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+;
char s[N];
int main(){
scanf("%s",s);
int len = strlen(s);
int j=len,ans=;
for(int i=;i<len;i++){
if(j==len){
if(s[i]=='[') ans++,j--;
continue ;
}else if(j==len-){
if(s[i]==':'){
ans++;
j=i;
break ;
}
}
}
int k=;
for(int i=len-;i>=;i--){
if(k==){
if(s[i]==']') ans++,k++;
continue ;
}else if(k==){
if(s[i]==':'){
ans++;
k=i;
break ;
}
}
}
if(j>=k) puts("-1");
else{
for(int i=j+;i<k;i++){
if(s[i]=='|') ans++;
}
cout<<ans<<endl;
}
return ;
}

C. Division and Union

题意:

给出n个区间,然后将这些区间分到两个集合S1,S2中,要求S1与S2的交集为空。最后输出区间是属于1集合还是2集合。

题解:

一开始想歪了,用并查集去做,虽然也可以做,但是有点麻烦了。

分析一下就可以发现,相交的区间肯定放在一个集合中,不相交的区间可以放在一个集合中,也可以放在另外一个集合中。

那么我们直接按左端点排序后,贪心地将前n-1个区间放在一个集合中,判断第n个区间放入另一个集合是否满足条件就ok了。

注意的是放入一个集合的时候,需要维护右端点的最大值,这样最后比较的时候才能保证正确性。

代码如下:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n;
struct Seg{
int l,r,id;
bool operator < (const Seg &A)const{
return l<A.l;
}
}seg[N];
int T;
int ans[N],a[N];
int main(){
cin>>T;
while(T--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&seg[i].l,&seg[i].r);
seg[i].id=i;
a[i]=;
}
sort(seg+,seg+n+);
int t=;
int max_r = ;
a[]=;
for(int i=;i<n;i++){
max_r = max(max_r,seg[i].r);
if(!a[i]) a[i]=a[i-];
if(max_r<seg[i+].l){
t++;
a[i+]=t;
}
}
if(!a[n]) a[n]=a[n-];
if(t<) puts("-1");
else{
for(int i=;i<=n;i++){
if(a[i]<t) ans[seg[i].id]=;
else ans[seg[i].id]=;
}
for(int i=;i<=n;i++) printf("%d ",ans[i]);
printf("\n");
}
}
return ;
}

D. GCD Counting

题意:

给出一颗树,每个结点有相应的权值,现在定义g(x,y)为树上x到y的简单路径的所有结点权值的最大公约数,dis(x,y)为x到y路径上面点的个数。求最大的dis(x,y)且满足g(x,y)>1。

题解:

说说比较朴素的方法吧...虽然速度比较慢,但对于我这种蒟蒻比较好懂...

对于2到2e5中的每个数,找出以其为因子的所有点,这些点不一定是连通的,最终呈现出来的应该是多个连通块。

然后我们直接对每个点跑最大直径就行了。

这个算法的具体时间复杂度我也不太清楚,但应该比较高,有方法可以将算法优化到nlogn的级别...

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int a[N],vis[N];
int ans,n;
vector <int> g[N],vec[N];
int dfs(int u){
vis[u]=;
int mx = ;
for(int v:g[u]){
if(vis[v]){
int now = dfs(v);
ans = max(ans,now++mx);
mx = max(mx,now);
}
}
ans=max(ans,);
return mx+;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
for(int j=;j*j<=a[i];j++){
if(a[i]%j) continue ;
vec[j].push_back(i);
if(j*j!=a[i]) vec[a[i]/j].push_back(i);
}
}
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=;i<=2e5;i++){
for(int v:vec[i]) vis[v]=;
for(int u:vec[i]) if(vis[u]) dfs(u);
}
printf("%d",ans);
return ;
}

E. Polycarp's New Job

题意:

在线给出一些矩形的长和宽,以及在线询问:给出一个矩形的箱子,问能否将之前所有给出的矩形装进去。

题解:

这是E题的难度么...感觉比C题还简单点。

对于给出的长和宽,让短边在前,长边在后。然后维护最长短边以及最长长边就行了。因为我们放的时候的最优选择肯定是短边放短边的。

最后询问的时候判断下就ok了...

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+;
int n;
int main(){
cin>>n;
int l = ,r = ;
getchar();
while(n--){
char c;
int x,y;
scanf("%c %d %d",&c,&x,&y);
if(x>y) swap(x,y);
if(c=='+'){
l=max(l,x);
r=max(r,y);
}else{
if(x<l || y<r) puts("NO");
else puts("YES");
}
getchar();
} return ;
}

G. (Zero XOR Subset)-less

题意:

给出n个数,要求尽量多地将这些数划分为连续地几段,并且满足任意子集地异或和不为0。

题解:

只要所有数的异或和都不为0,那么就必然存在一种划分方法,通过这个可以判断可行性。

根据异或的性质,考虑异或前缀和,那么任意一段数的异或和都可以用两个前缀和异或来表示。

现在问题就转化为了:在n个数中选取尽量多的数,使得这些数的任意子集的异或和不为0。

这是个线性基的经典问题,关于线性基,可以参考下这篇博客:https://www.cnblogs.com/ljh2000-jump/p/5869991.html

线性基有几个性质,其中包括:一是线性基的任意子集异或和都不为0;二是线性基的任意子集异或和的值域等于原数的任意子集异或和的值域。

那么,最终的答案就是前缀异或和中线性基的个数。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int a[N],p[N],sum[N];
int n;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]^a[i];
}
if(!sum[n]){
cout<<-;
return ;
}
for(int i=;i<=n;i++){
for(int j=;j>=;j--){
if(!((<<j)&sum[i])) continue ;
if(!p[j]){
p[j]=sum[i];
break ;
}
sum[i]^=p[j];
}
}
int ans = ;
for(int i=;i<;i++) if(p[i]) ans++;
cout<<ans;
return ;
}

Educational Codeforces Round 58 (Rated for Div. 2) 题解的更多相关文章

  1. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  2. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  3. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

  5. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理

    https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...

  6. Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学

    https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...

  7. Educational Codeforces Round 58 (Rated for Div. 2) G 线性基

    https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...

  8. Educational Codeforces Round 58 (Rated for Div. 2)

    A. Minimum Integer 水 #include<bits/stdc++.h> #define clr(a,b) memset(a,b,sizeof(a)) using name ...

  9. Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)

    感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...

随机推荐

  1. CentOS(Linux)安装KETTLE教程 并配置执行定时任务

    1,首先是安装jdk,并设置环境变量 采用yum安装可不设置环境变量 2,下载kettle https://sourceforge.net/projects/pentaho/files/Data%20 ...

  2. STL 一些常用的STL函数(持续更新

    先说一下  一边要用到算法的东西一般要加#include<algorithm>头文件 一.栈和队列 1 栈 :一种线性表 特点  后进先出 头文件  #include<stack&g ...

  3. maven之package与install的区别

    mvn clean package 先看命令的执行过程 mvn clean install 同样先看执行过程 mvn clean package依次执行了clean.resources.compile ...

  4. Centos7下lamp环境搭建的小笔记

    刚刚把校赛弄完,赛前在环境搭建上花了蛮多时间,也正好记一下笔记 0.首先更新源 清华大学开源镜像站的源 https://mirrors.tuna.tsinghua.edu.cn/help/centos ...

  5. Date()日期函数浏览器兼容问题踩坑

    原文:Date()日期函数浏览器兼容问题踩坑 之前用layui做的一项目中,table中用到了日期格式化的问题.直接没多想,撸代码就完了呗,结果最近一段时间客户反馈说显示日期跟录入日期不一样(显示日期 ...

  6. 5-sql语句

    1 [oracle@ocp ~]$ . oraenv # ORACLE_SID = [oracle] ? orcl The Oracle base has been set to /u01/app/o ...

  7. Vue-router使用

    Vue路由:--------------------------------------------------------1 .Vue-rouer入门2 .子路由3 .路由传参4 .多路由区域操作5 ...

  8. Bash中使用MySQL导入导出CSV格式数据[转]

    转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下:   select * from test_inf ...

  9. loadrunner创建测试脚本运行无响应 不记录脚本

    解决一运行User Generator直接程序卡死无响应的办法. (1)“我的电脑”点右键->属性->高级 点选“性能”中的“设置” (2)打开对话框后,进入“数据执行保护”,如果空白框中 ...

  10. 商业地产 招商 招租 CRM 意向 洽谈 合同 复用商铺商户管理系统

    适用场合 本软件适合商业地产的对招商的全流程管理,包括商铺信息,商户信息,洽谈信息,意向签订,合同管理等. 软件有试用版可供下载试用. 联系方式 QQ:2417158658 Tel:130251102 ...