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就行了。

具体操作见代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int q;
  5. ll l,r,d;
  6.  
  7. int main(){
  8. cin>>q;
  9. while(q--){
  10. cin>>l>>r>>d;
  11. if(d<l || d>r) cout<<d<<endl;
  12. else{
  13. ll now = r/d;
  14. cout<<(now+)*d<<endl;
  15. }
  16. }
  17. return ;
  18. }

B. Accordion

题意:

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

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

题解:

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

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

最后找中间的 |

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

代码如下:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 5e5+;
  5. char s[N];
  6. int main(){
  7. scanf("%s",s);
  8. int len = strlen(s);
  9. int j=len,ans=;
  10. for(int i=;i<len;i++){
  11. if(j==len){
  12. if(s[i]=='[') ans++,j--;
  13. continue ;
  14. }else if(j==len-){
  15. if(s[i]==':'){
  16. ans++;
  17. j=i;
  18. break ;
  19. }
  20. }
  21. }
  22. int k=;
  23. for(int i=len-;i>=;i--){
  24. if(k==){
  25. if(s[i]==']') ans++,k++;
  26. continue ;
  27. }else if(k==){
  28. if(s[i]==':'){
  29. ans++;
  30. k=i;
  31. break ;
  32. }
  33. }
  34. }
  35. if(j>=k) puts("-1");
  36. else{
  37. for(int i=j+;i<k;i++){
  38. if(s[i]=='|') ans++;
  39. }
  40. cout<<ans<<endl;
  41. }
  42. return ;
  43. }

C. Division and Union

题意:

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

题解:

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

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

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

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

代码如下:

  1. #include<bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. using namespace std;
  4. typedef long long ll;
  5. const int N = 2e5+;
  6. int n;
  7. struct Seg{
  8. int l,r,id;
  9. bool operator < (const Seg &A)const{
  10. return l<A.l;
  11. }
  12. }seg[N];
  13. int T;
  14. int ans[N],a[N];
  15. int main(){
  16. cin>>T;
  17. while(T--){
  18. scanf("%d",&n);
  19. for(int i=;i<=n;i++){
  20. scanf("%d%d",&seg[i].l,&seg[i].r);
  21. seg[i].id=i;
  22. a[i]=;
  23. }
  24. sort(seg+,seg+n+);
  25. int t=;
  26. int max_r = ;
  27. a[]=;
  28. for(int i=;i<n;i++){
  29. max_r = max(max_r,seg[i].r);
  30. if(!a[i]) a[i]=a[i-];
  31. if(max_r<seg[i+].l){
  32. t++;
  33. a[i+]=t;
  34. }
  35. }
  36. if(!a[n]) a[n]=a[n-];
  37. if(t<) puts("-1");
  38. else{
  39. for(int i=;i<=n;i++){
  40. if(a[i]<t) ans[seg[i].id]=;
  41. else ans[seg[i].id]=;
  42. }
  43. for(int i=;i<=n;i++) printf("%d ",ans[i]);
  44. printf("\n");
  45. }
  46. }
  47. return ;
  48. }

D. GCD Counting

题意:

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

题解:

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

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

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

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

代码如下:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 2e5+;
  5. int a[N],vis[N];
  6. int ans,n;
  7. vector <int> g[N],vec[N];
  8. int dfs(int u){
  9. vis[u]=;
  10. int mx = ;
  11. for(int v:g[u]){
  12. if(vis[v]){
  13. int now = dfs(v);
  14. ans = max(ans,now++mx);
  15. mx = max(mx,now);
  16. }
  17. }
  18. ans=max(ans,);
  19. return mx+;
  20. }
  21. int main(){
  22. scanf("%d",&n);
  23. for(int i=;i<=n;i++){
  24. scanf("%d",&a[i]);
  25. for(int j=;j*j<=a[i];j++){
  26. if(a[i]%j) continue ;
  27. vec[j].push_back(i);
  28. if(j*j!=a[i]) vec[a[i]/j].push_back(i);
  29. }
  30. }
  31. for(int i=;i<n;i++){
  32. int u,v;
  33. scanf("%d%d",&u,&v);
  34. g[u].push_back(v);
  35. g[v].push_back(u);
  36. }
  37. for(int i=;i<=2e5;i++){
  38. for(int v:vec[i]) vis[v]=;
  39. for(int u:vec[i]) if(vis[u]) dfs(u);
  40. }
  41. printf("%d",ans);
  42. return ;
  43. }

E. Polycarp's New Job

题意:

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

题解:

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

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

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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 5e5+;
  5. int n;
  6. int main(){
  7. cin>>n;
  8. int l = ,r = ;
  9. getchar();
  10. while(n--){
  11. char c;
  12. int x,y;
  13. scanf("%c %d %d",&c,&x,&y);
  14. if(x>y) swap(x,y);
  15. if(c=='+'){
  16. l=max(l,x);
  17. r=max(r,y);
  18. }else{
  19. if(x<l || y<r) puts("NO");
  20. else puts("YES");
  21. }
  22. getchar();
  23. }
  24.  
  25. return ;
  26. }

G. (Zero XOR Subset)-less

题意:

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

题解:

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

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

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

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

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

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

代码如下:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N = 2e5+;
  5. int a[N],p[N],sum[N];
  6. int n;
  7. int main(){
  8. scanf("%d",&n);
  9. for(int i=;i<=n;i++){
  10. scanf("%d",&a[i]);
  11. sum[i]=sum[i-]^a[i];
  12. }
  13. if(!sum[n]){
  14. cout<<-;
  15. return ;
  16. }
  17. for(int i=;i<=n;i++){
  18. for(int j=;j>=;j--){
  19. if(!((<<j)&sum[i])) continue ;
  20. if(!p[j]){
  21. p[j]=sum[i];
  22. break ;
  23. }
  24. sum[i]^=p[j];
  25. }
  26. }
  27. int ans = ;
  28. for(int i=;i<;i++) if(p[i]) ans++;
  29. cout<<ans;
  30. return ;
  31. }

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. python学习之常用模块

  2. ecshop漏洞修复 以及如何加固ecshop网站安全?

    由于8月份的ECSHOP通杀漏洞被国内安全厂商爆出后,众多使用ecshop程序源码的用户大面积的受到了网站被篡改,最明显的就是外贸站点被跳转到一些仿冒的网站上去,导致在谷歌的用户订单量迅速下降,从百度 ...

  3. Leecode刷题之旅-C语言/python-20.有效的括号

    /* * @lc app=leetcode.cn id=20 lang=c * * [20] 有效的括号 * * https://leetcode-cn.com/problems/valid-pare ...

  4. EXKMP学习笔记QAQ

    因为一本通少了一些算法,所以我就自行补充了一些东西上去. EXKMP也就是扩展KMP,是一种特别毒瘤的东西 EXKMP确实很难,我理解他的时间与AC机的时间差不多,而且还很难记,因此一学会就马上写博客 ...

  5. STL——list

    1.关键概述 list 是定义在 namespace::std 的模板,声明在 <list> ,存储结构是 双向链表, 提供的 正向和反向迭代器. 2.构造list对象 list<i ...

  6. (数据科学学习手札11)K-means聚类法的原理简介&Python与R实现

    kmeans法(K均值法)是麦奎因提出的,这种算法的基本思想是将每一个样本分配给最靠近中心(均值)的类中,具体的算法至少包括以下三个步骤: 1.将所有的样品分成k个初始类: 2.通过欧氏距离将某个样品 ...

  7. cadence17.2的OrCAD启动找不到license的问题

    1. cadence17.2的OrCAD每次启动都说找不到license 2. 提示是找不到licence,看下系统变量,licence的路径是在的 3. 估计是读取licence的路径的服务未开启, ...

  8. 基于jersey和Apache Tomcat构建Restful Web服务(二)

    基于jersey和Apache Tomcat构建Restful Web服务(二) 上篇博客介绍了REST以及Jersey并使用其搭建了一个简单的“Hello World”,那么本次呢,再来点有趣的东西 ...

  9. Linux下创建pycharm的快捷方式

    第一步:创建桌面快捷方式文件Pycharm.desktop,并打开 sudo gedit /usr/share/applications/Pycharm.desktop 第二步:在打开的文件Pycha ...

  10. 自动化测试学习之路--java String、StringBuilder

    Java中的String和StringBuilder类: 1.String对象是不可变的.每一个看起来修改了String值的方法,实际上都是创建了全新的String对象.代码示例如下: String ...