COCI 2018/2019 CONTEST #2 T4 T5 Solution

abstract

花式暴力

#2 T5 Sunčanje

题意

按顺序给你1e5个长方形(左下角坐标&&长宽),对于每个长方形询问是否有后面的长方形盖住了它。


题解

暴力几何。不需要线段树维护。

用一个排序剪枝,先按矩形的左下角x坐标排序,对于每一个矩形i,枚举后面的所有矩形j,当矩形j的左下角x坐标大于i的右下角x坐标时,break掉。 数据并没有卡


代码

  1. #include <queue>
  2. #include <vector>
  3. #include<map>
  4. #include <cstdio>
  5. #include <cctype>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <iostream>
  9. #include <algorithm>
  10. #define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
  11. using namespace std;
  12. int cnt = 0;
  13. const int N = 1e5+1;
  14. inline int read()
  15. {
  16. int X = 0, w = 0; char c = 0;
  17. while (c<'0' || c>'9') { w |= c == '-'; c = getchar(); }
  18. while (c >= '0'&&c <= '9') X = (X << 3) + (X << 1) + (c ^ 48), c = getchar();
  19. return w ? -X : X;
  20. }
  21. int n;
  22. struct rec {
  23. int x, y, len, wid, id;
  24. }a[N];
  25. int ans[N];
  26. int main()
  27. {
  28. n = read();
  29. rep(i, 1, n) {
  30. a[i].x = read(); a[i].y = read(); a[i].len = read(); a[i].wid = read();
  31. //a[i].x += a[i].len;
  32. a[i].id = i;
  33. }
  34. sort(a + 1, a + 1 + n, [](rec a, rec b)->bool {return a.x < b.x; });
  35. rep(i, 1, n) {
  36. int j = i + 1;
  37. while (j<=n && (a[i].x + a[i].len>a[j].x)) {
  38. if (a[j].y >= a[i].y + a[i].wid || a[j].y + a[j].wid <= a[i].y);
  39. else
  40. {
  41. if (a[i].id<a[j].id) ans[a[i].id] = 1;
  42. else ans[a[j].id] = 1;
  43. }
  44. j++;
  45. }
  46. }
  47. rep(i, 1, n)puts(ans[i]?"NE":"DA");
  48. cin >> n;
  49. return 0;
  50. }
  1. //按照x的右下坐标排序,反着剪枝,快了100ms吧
  2. int cnt = 0;
  3. const int N = 1e5+1;
  4. inline int read()
  5. {
  6. int X = 0, w = 0; char c = 0;
  7. while (c<'0' || c>'9') { w |= c == '-'; c = getchar(); }
  8. while (c >= '0'&&c <= '9') X = (X << 3) + (X << 1) + (c ^ 48), c = getchar();
  9. return w ? -X : X;
  10. }
  11. int n;
  12. struct rec {
  13. int x, y, len, wid, id;
  14. }a[N];
  15. int ans[N];
  16. int main()
  17. {
  18. n = read();
  19. rep(i, 1, n) {
  20. a[i].x = read(); a[i].y = read(); a[i].len = read(); a[i].wid = read();
  21. a[i].x += a[i].len;
  22. a[i].id = i;
  23. }
  24. sort(a + 1, a + 1 + n, [](rec a, rec b)->bool {return a.x < b.x; });
  25. rep(i, 2, n) {
  26. int j = i - 1;
  27. while (j >= 1 && (a[i].x - a[i].len<a[j].x)) {
  28. if (a[j].y >= a[i].y + a[i].wid || a[j].y + a[j].wid <= a[i].y);
  29. else
  30. {
  31. if (a[i].id<a[j].id) ans[a[i].id] = 1;
  32. else ans[a[j].id] = 1;
  33. }
  34. j--;
  35. }
  36. }
  37. rep(i, 1, n)puts(ans[i]?"NE":"DA");
  38. //cin >> n;
  39. return 0;
  40. }

心路历程

  1. 为什么能暴力啊QAQ ,如果有n个长方形嵌套在一起的,上面程序就是N^N的,必T

T4 Maja

题意

给你一个n*m的矩阵和出发点,你可以上下左右走,最多走k步。你到达任意一点时会获得该点的数值(可重复获得),问最终回到起点的最大收益是多少?


题解

两个结论:

1.路径的前半段和后半段必定是相同的(重复路径)。

证明:若不同,显然取前半段和后半段中较大的重复走两遍,答案显然不会更差。

2.当k很大时,必然是在某两点来回走动。

证明:首先,k较大(k大于n*m)必然是在某个环上绕圈,否则没地方走了。

然后,在某个长度大于2的环上绕圈必然不会比在该环相邻2个之和最大的两个点之间来回走更优。证明:我们把环上的相邻点两两分组,和最大的那组的平均值必然不小于总环的平均值。否则总和小于总和矛盾。

于是我们的路径就是从起点走到某个点,在那个点与相邻的来回走,原路回到起点。

我们可以dp来做,dp[i][j][k]表示第k步走到(i,j)这个点时最大的收益。它可以由dp[i][j][k-1]的上下左右四个点转移而来。

而由于转移第三维只由上一个状态转移而来,所以可以滚动更新。


代码

  1. //用了-inf来代替判边界
  2. # define int long long
  3. #define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
  4. #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
  5. using namespace std;
  6. const int maxn = 1e2 + 5;
  7. const int INF = 1e18;
  8. int n, m, sr, sc,k;
  9. int c[maxn][maxn];
  10. int f[maxn][maxn][2];
  11. int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
  12. signed main()
  13. {
  14. FAST_IO;
  15. cin >> n >> m >> sr >> sc>>k;
  16. k /= 2;
  17. int ans = -INF;
  18. rep(i, 1, n)rep(j, 1, m) {
  19. cin >> c[i][j];
  20. }
  21. rep(i, 0, n + 1)rep(j, 0, m + 1) { f[i][j][0]= f[i][j][1] = -INF; }
  22. f[sr][sc][0] = 0;
  23. rep(r, 1, min(k, n*m)) {
  24. int now = r & 1;
  25. int pst = !now;
  26. rep(i, 1, n)rep(j, 1, m) {
  27. int tmp = -INF; rep(k, 0, 3) { tmp = max(tmp, f[i + dir[k][0]][j + dir[k][1]][pst]); }
  28. f[i][j][now] = max(tmp + c[i][j], -INF);
  29. if (f[i][j][now] < 0) continue;//边界
  30. int dist = f[i][j][now] + tmp;
  31. tmp = 0; rep(k, 0, 3) { tmp = max(tmp, c[i + dir[k][0]][j + dir[k][1]]); }
  32. dist += (k - r)*(c[i][j] + tmp);
  33. ans = max(ans, dist);
  34. }
  35. }
  36. cout << ans << endl;
  37. cin >> n;
  38. return 0;
  39. }
  40. /*
  41. 4 1
  42. 1 3
  43. 1 4
  44. 2 2
  45. 1 4
  46. 2 3
  47. */

心路历程



COCI 2018/2019 CONTEST #2 T4 Maja T5Sunčanje Solution的更多相关文章

  1. COCI 2018/2019 CONTEST #2 Solution

    Problem1 Preokret 第一题一定不是什么难题. 第一个问题在读入的时候判断当前时间是不是在1440及以前就行 第二个问题考虑离线处理,由于每个时刻只能最多发生1个事件那么就弄个桶记录每一 ...

  2. 20172328 2018—2019《Java软件结构与数据结构》第二周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第二周学习总结 概述 Generalization 本周学习了第三章集合概述--栈和第四章链式结构--栈.主要讨论了集合以 ...

  3. 2018 - 2019 CTU Open Contest H. Split Game 【SG函数】

    H. Split Game time limit per test 1.0 s memory limit per test 256 MB input standard input output sta ...

  4. 2018 - 2019 CTU Open Contest E. Locker Room 【后缀数组】

    任意门:http://codeforces.com/gym/101954/problem/E E. Locker Room time limit per test 2.0 s memory limit ...

  5. 工具软件集合 Adobe AE PS Pr CC 2018 2019 破解教程

    来源https://mp.weixin.qq.com/s/zeq1sTmaPsKt7Bsok0Ldrg(若链接失效,请关注软件安装管家公众号) 相关链接 Office 2019破解教程 Adobe 2 ...

  6. [USACO 2018 December Contest]作业总结

    t1 Convention 题目大意 每一头牛都有一个来的时间,一共有\(n\)辆车,求出等待时间最长的那头牛等待的最小时间. 解法 第一眼看到这道题还以为是\(2018noip\)普及组的t3魔鬼题 ...

  7. 2018 – 2019 年前端 JavaScript 面试题

    JavaScript 基础问题 1.使以下代码正常运行: JavaScript 代码: const a = [1, 2, 3, 4, 5]; // Implement this a.multiply( ...

  8. Davor COCI 2018

    当题目中有多组解,但要某值最大,该怎么办? 本文为博客园ShyButHandsome的原创作品,转载请注明出处 题目描述 After successfully conquering the South ...

  9. [USACO 2018 Open Contest]作业总结

    t1-Out of Sorts 题目大意 将最大的数冒泡排序到最后需要多少次操作. 分析 排序后判断距离. ac代码 #include<bits/stdc++.h> #define N 1 ...

随机推荐

  1. webpack学习记录 二

    开发网站 用polyfill(全局污染)  开发框架 用Runtime(局部污染) 在.babelrc文件中

  2. C# - 代码重构

    隐藏更多 只暴露集合中供人使用的单一功能,将关于集合的更多功能隐藏掉. 旧版本 public class Animal{    private List<string> LanguageL ...

  3. 设计模式八: 委派(Delegate)

    简介 委派模式不属于GOF23种设计模式, 主要角色有三种: 抽象任务角色, 委派者角色, 具体任务角色. 实现层面上, 定义一个抽象接口, 它有若干实现类, 他们真正执行业务方法, 这些子类是具体任 ...

  4. torch画散点图

    import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.p ...

  5. Web从入门到放弃<6>

     <1> Canvas. 1,灰度图: js: function showAsGray() { var imgNode = document.getElementById('img'); ...

  6. 使用pytesseract出现的问题

    dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib Referenced from: /usr/local/lib/li ...

  7. tarjan 题目汇总(含解析)

    下面容许我偷个懒,洛谷上写过的blog我就不来再抄一遍了 洛谷P3436 [[POI2006]PRO-Professor Szu](别称:作死的老教授) 洛谷P4306 [[JSOI2010]连通数] ...

  8. selenium——find_element_by_xx 与 find_element(By.XX,'XXXX')

  9. MVC中一般为什么用IQueryable而不是用IList?

    IList(IList<T>)会立即在内存里创建持久数据,这就没有实现“延期执行(deferred execution)”,如果被加载的实体有关联实体(associations),此关联实 ...

  10. Python 爬虫 58同城

    目标站点需求分析 获取各类产品的名字,地区,时间,价格 涉及的库 BeautifulSoup,requests,time,pymongo 获取各大类产品的链接 获取单页源码 解析单页源码 保存到文件中 ...