Codeforces Round #542

abstract

I决策中的独立性,

II联通块染色板子

IIIVoronoi diagram O(N^2 logN)

VI环上距离分类讨论加取模,最值中的决定性元素

V代数构造

B

题意

2n个数排成一行,1..n各出现两次,两个人分别从1按顺序走到n(每个数只能被一个人路过),问他们两个人的距离和的最小值(两个数之间的距离等于他们位置(下标)值差)?


题解

将每个数的两个位置用pair存起来(eg : pos[1].x , pos[1].y)。

首先我们发现两个人的选择其实并不独立,如果一个人选择了一条路,另一个人的路也就确定了。其次我们发现每两个数之间的路径是相互独立的。

所以对于每一段 i to i+1 我们只要取min(abs(pos[i + 1].x - pos[i].x) + abs(pos[i + 1].y - pos[i].y), abs(pos[i + 1].x - pos[i].y) + abs(pos[i + 1].y - pos[i].x))即可。 最后求和


代码

  1. //头文件省略
  2. cin >> n;
  3. rep(i, 1, 2 * n) {
  4. int a;
  5. cin >> a;
  6. if (pos[a].x == 0)pos[a].x = i;
  7. else pos[a].y = i;
  8. }
  9. ll ans = pos[1].x + pos[1].y - 2;;
  10. rep(i, 1, n - 1)c[i] = min(abs(pos[i + 1].x - pos[i].x) + abs(pos[i + 1].y - pos[i].y), abs(pos[i + 1].x - pos[i].y) + abs(pos[i + 1].y - pos[i].x));
  11. rep(i, 1, n-1)ans += c[i];
  12. cout << ans;

C

题意

求网格图上联通块之间的最短距离平方。

  1. sample input
  2. 5
  3. 1 1
  4. 5 5
  5. 00001
  6. 11111
  7. 00111
  8. 00110
  9. 00110
  10. sample input
  11. 10

其中0代表陆地,1代表海洋


题解

直接跑一遍联通块染色,然后暴力O(n^4)就能过。

优化的方法是取边界,O(n^3)。

最优解是Voronoi diagram O(N^2 logN)


代码

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<sstream>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<assert.h>
  7. #include<math.h>
  8. #include<stdio.h>
  9. #include<vector>
  10. #include<queue>
  11. #include<string>
  12. #include<ctime>
  13. #include<stack>
  14. #include<map>
  15. #include<set>
  16. #include<list>
  17. using namespace std;
  18. #define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
  19. #define REP(i,j,k) for(int i = (int)j;i < (int)k;i ++)
  20. #define per(i,j,k) for(int i = (int)j;i >= (int)k;i --)
  21. #define debug(x) cerr<<#x<<" = "<<(x)<<endl
  22. #define mmm(a,b) memset(a,b,sizeof(a))
  23. #define pb push_back
  24. #define MD(x) x%=mod
  25. #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
  26. #define precise(x) fixed << setprecision(x)
  27. //#define x first
  28. //#define y second
  29. typedef double db;
  30. typedef long long ll;
  31. const int MAXN = 256;;
  32. const int maxn = 2e5+2;
  33. const int INF = 1e9;
  34. const db eps = 1e-7;
  35. const int mod = 1e9 + 7;
  36. //a[maxn], b[maxn];
  37. int A[maxn];
  38. char mmp[55][55];
  39. int color[55][55];
  40. int n,m;
  41. int dir[4][2] = { 0,1 ,1,0, 0,-1, -1,0 };
  42. int colornum = 0;
  43. void dfs(int x, int y) {
  44. if (color[x][y])return;
  45. color[x][y] = colornum;
  46. rep(i, 0, 3) {
  47. int dx = x + dir[i][0];
  48. int dy = y + dir[i][1];
  49. if (dx<1 || dx>n || dy<1 || dy>n)continue;
  50. if(mmp[dx][dy]=='0')dfs(dx, dy);
  51. }
  52. }
  53. void bfs(int x, int y) {
  54. queue<pair<int, int> >Q;
  55. Q.push({ x,y });
  56. color[x][y] = colornum;
  57. while (!Q.empty()) {
  58. auto v = Q.front();
  59. Q.pop();
  60. rep(i, 0, 3) {
  61. int dx = v.first + dir[i][0];
  62. int dy = v.second + dir[i][1];
  63. if (color[dx][dy])continue;
  64. if (dx<1 || dx>n || dy<1 || dy>n)continue;
  65. if (mmp[dx][dy] == '0')Q.push({ dx,dy }),color[dx][dy]=colornum;
  66. }
  67. }
  68. }
  69. int main() {
  70. FAST_IO;
  71. cin >> n;
  72. int r1, c1, r2, c2;
  73. cin >> r1 >> c1 >> r2 >> c2;
  74. rep(i, 1, n)cin >> mmp[i] + 1;
  75. rep(i, 1, n)rep(j, 1, n) if(mmp[i][j]=='0'){
  76. if (!color[i][j]) {
  77. colornum++;
  78. bfs(i, j);
  79. }
  80. }
  81. int ans = 1e9;
  82. if (color[r1][c1] == color[r2][c2])ans = 0;
  83. else {
  84. rep(i, 1, n)rep(j, 1, n)if (color[i][j] == color[r1][c1]) {
  85. rep(ii, 1, n)rep(jj, 1, n)if (color[ii][jj] == color[r2][c2]) {
  86. ans = min(ans, (i - ii)*(i - ii) + (j - jj)*(j - jj));
  87. }
  88. }
  89. }
  90. cout << ans << endl;
  91. cin >> n;
  92. }

D

题意

有一个n个结点的环,每个结点上有一些糖果,每个糖果都有一个想要到达的结点,你现在从某个结点出发顺时针走,每次经过一个结点只能带一个糖果,问从某点出发最少走多少路程可以把所有糖果运送到它们想要到达的结点? 出发点取遍1到n。


题解

我们发现从某结点出发拿一个糖果,最坏情况下走一圈以后一定可以把它放好。而如果某节点有两颗糖果,那么至少要超过一圈才能放好。

所以 如果记所有结点中糖果数量最大值为mx,最多mx+1圈必定能全部送完。

而对一个结点来说,只有它的最后一个糖果想到达的结点决定了运完该点所有糖果总路程的长短,因此我们贪心地把路程最短的糖果留到最后拿即可。

于是问题就变成了环上求路程了,要用到取模,和分类讨论。(分类很不擅长,讨论了半天orz坑点:candy[now].size()==mx-1&&mx>1如果不加后面这个mx>1就会讨论根本没有糖果的情况,导致wa)


代码

  1. //头文件省略
  2. int n;
  3. int a[maxn], b[maxn];
  4. vector<int> candy[maxn];
  5. int mindis[maxn];
  6. int lastmin[maxn];
  7. int main() {
  8. FAST_IO;
  9. int n, m;
  10. cin >> n >> m;
  11. rep(i, 1, n)mindis[i] = 1e9,lastmin[i]=0;
  12. rep(i, 1, m) {
  13. cin >> a[i] >> b[i];
  14. candy[a[i]].push_back(b[i]);
  15. mindis[a[i]] = min(mindis[a[i]], (b[i] - a[i]+n)%n);
  16. }
  17. int mx = 0;
  18. rep(i, 1, n)mx = max(mx, (int)candy[i].size());
  19. rep(i, 1, n) {
  20. rep(j, 1, n) {
  21. int now = (i - j+n)%n; if (now == 0)now = n;
  22. if(candy[now].size()==mx)
  23. lastmin[i] = max(lastmin[i],
  24. (now - i + n) % n+mindis[now]);
  25. else if(candy[now].size()==mx-1&&mx>1)
  26. lastmin[i] = max(lastmin[i],
  27. (now - i + n) % n + mindis[now]-n);
  28. }
  29. }
  30. //rep(i, 1, n)cout << mindis[i] << ' '; cout << endl;
  31. //rep(i, 1, n)cout << lastmin[i] << ' '; cout << endl;
  32. rep(i, 1, n) {
  33. cout <<n*(mx-1) + lastmin [i] << ' ';
  34. }
  35. cin >> n;
  36. }

E

题意

构造题,给你一个问题,和一个错误算法,让你构造数据使得正确答案与错误代码答案相差k。

问题是有一个数列,a[i]<1e6,n<2e3;定义 f(l,r)=(sum[r]-sum[l-1])*(r-l+1),sum为前缀和,求max{f(l,r)} for l=1 to n, r=l to n;

错误代码是线性滑窗扫一遍,每次维护一个区间窗口,如果小于0就舍弃原区间,重新开一个新区间。如果和大于0就不停加数。


题解

代码很简单,构造很巧妙,证明很数学

考虑形如 -1 , x, x, x,...x,y的数列,共n个x,1个y都为正数(其中x都是1e6,为了满足原数列每个数<=1e6的条件),错误代码给出的解是(x*n+y)*(n+1);而正确答案应是(x*n+y-1)*(n+2)两者差为n*x+y-2 令x=1e6,n=k/x,y=k%x即可。(代数不好推了半天orz)


代码

  1. //头文件省略
  2. int k; cin >> k;
  3. n = 1;
  4. int S = -1;
  5. int last = -1;
  6. while (S-n+1<k) {
  7. n++;
  8. int delta = k-(S - n + 1) ;
  9. if (delta <= 1e6) { last = delta; break; }
  10. else S += 1e6;
  11. }
  12. cout << n << endl;
  13. cout << -1 << ' ';
  14. rep(i,1,n-2)cout<< 1000000 << ' ';
  15. if (last != -1)cout << last << endl;

Codeforces Round #542 题解的更多相关文章

  1. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  2. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  3. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

  4. Codeforces Round #569 题解

    Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...

  5. Codeforces Round #557 题解【更完了】

    Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...

  6. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  7. Codeforces Round #542 (Div. 1) 题解

    开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...

  8. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) 题解

    A. Toy Train 时间限制:2 seconds 内存限制:256 megabytes 题意 有编号111~n(n≤5000)n(n\le 5000)n(n≤5000)的车站顺时针呈环排列,有m ...

  9. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

随机推荐

  1. 用Navicat连接阿里云ECS服务器上的MySQL数据库

    今天用navtive连接阿里云服务器(Linux)的数据库时,老是连接不上,并且报10060错误,要通过以下两个步骤解决: 1.先进入linux连接数据库并输入密码: mysql -uroot -p ...

  2. 网络学习day01_计算机网络与分层思想

    2018.9.1 网络学习day01_计算机网络与分层思想 网络安全 计算机网络 分层思想    LAN与WAN   LAN(Local Area Network)--局域网   局域网(Local ...

  3. JSP标签和JSTL

    Java的5个标签库:核心(c).格式化(fmt).函数(fn).SQL(sql).XML(x) SQL.XML库不推荐使用 核心标签库(c) //taglib指令 <%@ taglib pre ...

  4. Lua中的迭代器与泛型for

    [前言] 迭代器就是一种可以遍历一种集合中所有元素的机制,在Lua中,通常将迭代器表示为函数.每调用一次函数,就返回集合中的“下一个”元素.每个迭代器都需要在每次成功调用之后保存一些状态,这样才能知道 ...

  5. 【原创】大叔经验分享(18)hive2.0以后通过beeline执行sql没有进度信息

    一 问题 在hive1.2中使用hive或者beeline执行sql都有进度信息,但是升级到hive2.0以后,只有hive执行sql还有进度信息,beeline执行sql完全silence,在等待结 ...

  6. linux日志过滤某时间段的日志

    sed -n '/2019-01-10 16:00*/,/2019-01-10 18:48*/p' nohup.out > 111.log

  7. JS中 typeof,instanceof类型检测方式

    在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景: 1.typeof:主要用于检测基本类型. typeof undefined;//=> undefined typeof 'a' ...

  8. 【数据库】MySql分割字符串

    上论坛时看到一个骨骼清奇的分割字符串算法. DROP TABLE IF EXISTS Tmp_AreaCode; CREATE TABLE Tmp_AreaCode( string ) )ENGINE ...

  9. 视频转码成mp4格式,添加关键帧,添加元数据,把元数据放在第一帧,可拖动

    作者测试是在windows下使用,所以下载的页面地址是: http://ffmpeg.zeranoe.com/builds/点击页面上的Download FFmpeg git-738ebb4 64-b ...

  10. tensorflow例子-【老鱼学tensorflow】

    本节主要用一个例子来讲述一下基本的tensorflow用法. 在这个例子中,我们首先伪造一些线性数据点,其实这些数据中本身就隐藏了一些规律,但我们假装不知道是什么规律,然后想通过神经网络来揭示这个规律 ...