COCI 2018/2019 CONTEST #2 T4 T5 Solution

abstract

花式暴力

#2 T5 Sunčanje

题意

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


题解

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

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


代码

#include <queue>
#include <vector>
#include<map>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
using namespace std; int cnt = 0;
const int N = 1e5+1;
inline int read()
{
int X = 0, w = 0; char c = 0;
while (c<'0' || c>'9') { w |= c == '-'; c = getchar(); }
while (c >= '0'&&c <= '9') X = (X << 3) + (X << 1) + (c ^ 48), c = getchar();
return w ? -X : X;
}
int n;
struct rec {
int x, y, len, wid, id;
}a[N];
int ans[N];
int main()
{ n = read();
rep(i, 1, n) {
a[i].x = read(); a[i].y = read(); a[i].len = read(); a[i].wid = read();
//a[i].x += a[i].len;
a[i].id = i;
}
sort(a + 1, a + 1 + n, [](rec a, rec b)->bool {return a.x < b.x; });
rep(i, 1, n) {
int j = i + 1;
while (j<=n && (a[i].x + a[i].len>a[j].x)) {
if (a[j].y >= a[i].y + a[i].wid || a[j].y + a[j].wid <= a[i].y);
else
{
if (a[i].id<a[j].id) ans[a[i].id] = 1;
else ans[a[j].id] = 1;
}
j++;
}
}
rep(i, 1, n)puts(ans[i]?"NE":"DA"); cin >> n;
return 0;
}
//按照x的右下坐标排序,反着剪枝,快了100ms吧
int cnt = 0;
const int N = 1e5+1;
inline int read()
{
int X = 0, w = 0; char c = 0;
while (c<'0' || c>'9') { w |= c == '-'; c = getchar(); }
while (c >= '0'&&c <= '9') X = (X << 3) + (X << 1) + (c ^ 48), c = getchar();
return w ? -X : X;
}
int n;
struct rec {
int x, y, len, wid, id;
}a[N];
int ans[N];
int main()
{ n = read();
rep(i, 1, n) {
a[i].x = read(); a[i].y = read(); a[i].len = read(); a[i].wid = read();
a[i].x += a[i].len;
a[i].id = i;
}
sort(a + 1, a + 1 + n, [](rec a, rec b)->bool {return a.x < b.x; });
rep(i, 2, n) {
int j = i - 1;
while (j >= 1 && (a[i].x - a[i].len<a[j].x)) {
if (a[j].y >= a[i].y + a[i].wid || a[j].y + a[j].wid <= a[i].y);
else
{
if (a[i].id<a[j].id) ans[a[i].id] = 1;
else ans[a[j].id] = 1;
}
j--;
}
}
rep(i, 1, n)puts(ans[i]?"NE":"DA"); //cin >> n;
return 0;
}

心路历程

为什么能暴力啊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]的上下左右四个点转移而来。

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


代码

//用了-inf来代替判边界
# define int long long
#define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
using namespace std;
const int maxn = 1e2 + 5;
const int INF = 1e18;
int n, m, sr, sc,k;
int c[maxn][maxn];
int f[maxn][maxn][2];
int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
signed main()
{
FAST_IO;
cin >> n >> m >> sr >> sc>>k;
k /= 2;
int ans = -INF;
rep(i, 1, n)rep(j, 1, m) {
cin >> c[i][j];
}
rep(i, 0, n + 1)rep(j, 0, m + 1) { f[i][j][0]= f[i][j][1] = -INF; }
f[sr][sc][0] = 0;
rep(r, 1, min(k, n*m)) {
int now = r & 1;
int pst = !now;
rep(i, 1, n)rep(j, 1, m) {
int tmp = -INF; rep(k, 0, 3) { tmp = max(tmp, f[i + dir[k][0]][j + dir[k][1]][pst]); } f[i][j][now] = max(tmp + c[i][j], -INF);
if (f[i][j][now] < 0) continue;//边界 int dist = f[i][j][now] + tmp;
tmp = 0; rep(k, 0, 3) { tmp = max(tmp, c[i + dir[k][0]][j + dir[k][1]]); } dist += (k - r)*(c[i][j] + tmp);
ans = max(ans, dist);
} }
cout << ans << endl;
cin >> n;
return 0;
}
/*
4 1
1 3
1 4
2 2
1 4
2 3
*/

心路历程



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. phpstudy 2016 切换Nginx+php7.0版本所需运行库 vc14 + 安装redis拓展

    去微软官方下载vc14的运行库 链接:https://www.microsoft.com/en-us/download/details.aspx?id=48145 32位运行库 安装成功 切换版本成功 ...

  2. 华为mate10 UA

    Dalvik/2.1.0 (Linux; U; Android 9; ALP-AL00 Build/HUAWEIALP-AL00) "user-agent": "Mozi ...

  3. AWK如何打印从某一列到最后一列的内容

    awk -F " "  '{for (i=4;i<=NF;i++)printf("%s ", $i);print ""}' file

  4. setTimeout 第三个参数秒懂

    好吧,假设你们都是从 ES6 里 promise 发现 setTimeout 还有第三个参数的,下面讲讲到底是干嘛的 setTimeout 第三个及之后的参数作用:定时器启动时候,第三个以后的参数是作 ...

  5. linear-gradient 纯CSS3项目价格表切换代码

    <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8&qu ...

  6. 【python】命令行神器 Click 简明笔记

    全文拷贝自 命令行神器 Click 简明笔记 Click Click 是用 Python 写的一个第三方模块,用于快速创建命令行.我们知道,Python 内置了一个 Argparse 的标准库用于创建 ...

  7. php无限极分类方法

    仅供参考: //控制器 $data = M('category')->select(); $datas = D('Category')->_getTree($data, 0,0,TRUE) ...

  8. spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available

    我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...

  9. RedHat 6配置yum源为网易镜像(转)

    概述 由于版权的问题,RedHat6不能直接使用yum一些指令,需要配置yum源为网易镜像,但是网上谈到很多:整理一下,将有用的信息整理如下,以便于能够为其他的配置服务配置使用:需要卸载掉原理系统自带 ...

  10. scrapy_redis 相关: 将 jobdir 保存的爬虫进度转移到 Redis

    0.参考 Scrapy 隐含 bug: 强制关闭爬虫后从 requests.queue 读取的已保存 request 数量可能有误 1.说明 Scrapy 设置 jobdir,停止爬虫后,保存文件目录 ...