cf1557D. Ezzat and Grid

题意:

有n行,每行有10910^9109列,仅仅由0和1构成
现在给你1的存在位置,(i,l,r)表示第i行的第l列到第r列全为1
你可以删除任意一行i,删除后,第i-1行和第i+1行为相邻
现在我们要求求最多的行数,使得每个相邻两行最少有一列都是1(可以理解成上下相邻1),并输出删除了哪些行

题解:

对于i行,我们考虑前i-1行个是与i行满足要求的(即存在相邻1)。我们用线段树维护一个pair<int,int>sum
sum.first表示以id为结尾所保留的最大行数
sum.second=id:表示以id为结尾的情况
因为1的出现都是连续的,我们想查找与第i行满足情况的行数,就在第i行出现1的区间(例如[l,r]),我们就查看所有[l,r]区间内的值,取最大值得到sum(相当于取之前的最大值接着当前的i)。sum为与第i行满足情况且保留行数最多的某一行。查询完后,要将第i行的情况插入到线段树中,在区间[l,r]中插入我们的ans(ans.second=i,ans.first=sum.first+1)
为了方便输出,我们用一个path来实现记录路径
讲的可以不是很明白,详细可以看看代码

代码:

改了一个多小时,终于改出来了

// Problem: D. Ezzat and Grid
// Contest: Codeforces - Codeforces Round #737 (Div. 2)
// URL: https://codeforces.com/contest/1557/problem/D
// Memory Limit: 256 MB
// Time Limit: 2500 ms
// Data:2021-08-23 15:24:28
// By Jozky#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 1e6 + 9;
vector<PII> vec[maxn];
int num[maxn];
int tot= 0;
struct tree
{int l, r;int lazy= 0;PII maxx;
} tr[maxn << 2];
void pushup(int rt)
{tr[rt].maxx= max(tr[rt << 1].maxx, tr[rt << 1 | 1].maxx);
}
void solve(int rt, PII val)
{tr[rt].maxx= val;tr[rt].lazy= 1;
}
void pushdown(int rt)
{if (tr[rt].lazy == 0)return;solve(rt << 1, tr[rt].maxx);solve(rt << 1 | 1, tr[rt].maxx);tr[rt].lazy= 0;
}
void build(int rt, int l, int r)
{tr[rt].l= l;tr[rt].r= r;tr[rt].lazy= 0;tr[rt].maxx= {0, -1};if (l == r) {return;}int mid= (l + r) >> 1;build(rt << 1, l, mid);build(rt << 1 | 1, mid + 1, r);pushup(rt);
}
void update(int rt, int l, int r, PII x)
{if (tr[rt].r < l || tr[rt].l > r)return;if (tr[rt].l >= l && tr[rt].r <= r) {solve(rt, x);return;}//if (tr[rt].lazy)pushdown(rt);int mid= (tr[rt].l + tr[rt].r) >> 1;update(rt << 1, l, r, x);update(rt << 1 | 1, l, r, x);pushup(rt);
}
PII query(int rt, int l, int r)
{if (tr[rt].r < l || tr[rt].l > r)return {0, -1};if (tr[rt].l >= l && tr[rt].r <= r) {return tr[rt].maxx;}//if (tr[rt].lazy)pushdown(rt);int mid= (tr[rt].l + tr[rt].r) >> 1;PII maxx= {0, -1};maxx= max(maxx, max(query(rt << 1, l, r), query(rt << 1 | 1, l, r)));return maxx;
}
int path[maxn];
int ans[maxn];
int main()
{//rd_test();int n, m;read(n, m);for (int i= 1; i <= m; i++) {int id, l, r;read(id, l, r);vec[id].push_back({l, r});num[++tot]= l;num[++tot]= r;}sort(num + 1, num + 1 + tot);int cnt= unique(num + 1, num + 1 + tot) - num - 1;// cout << "cnt=" << cnt << endl;for (int i= 1; i <= n; i++) {for (int j= 0; j < vec[i].size(); j++) {// printf("vec[i][j]=%d \n", vec[i][j]);vec[i][j].first= lower_bound(num + 1, num + 1 + cnt, vec[i][j].first) - num;vec[i][j].second= lower_bound(num + 1, num + 1 + cnt, vec[i][j].second) - num;// printf("处理后vec[i][j]=%d\n ", vec[i][j]);}}build(1, 1, cnt);//cout << "--" << endl;PII maxx;for (int i= 1; i <= n; i++) {maxx= {0, -1};//保存的数量  编号for (auto it : vec[i]) {// printf("l=%d r=%d\n", it.first, it.second);maxx= max(maxx, query(1, it.first, it.second));}path[i]= maxx.second;PII ans= {maxx.first + 1, i};for (auto it : vec[i]) {// printf("l=%d r=%d\n", it.first, it.second);update(1, it.first, it.second, ans);}}maxx= query(1, 1, cnt);printf("%d\n", n - maxx.first);int now= maxx.second;while (now != -1) {ans[now]= 1;now= path[now];}for (int i= 1; i <= n; i++) {if (!ans[i])printf("%d ", i);}return 0;//Time_test();
}

cf1557D. Ezzat and Grid相关推荐

  1. CodeForces - 1557D Ezzat and Grid(线段树+dp)

    题目链接:点击查看 题目大意:给出 nnn 个 010101 串,现在问最少需要删掉多少个串,才能使得剩下的串拼起来是连通的 规定两个 010101 串是连通的,当且仅当存在至少一列,在两个串中都为 ...

  2. Codeforces Round #737 (Div. 2) D. Ezzat and Grid 线段树动态开点

    传送门 文章目录 题意: 思路: 题意: 思路: 比较套路的一个题,我们维护一个dp[i]dp[i]dp[i]表示到了第iii行能保留的区间最多是多少. 转移比较明显:dp[i]=max(dp[j]) ...

  3. 线段树 ---- Codeforces 737 Div2 D. Ezzat and Grid 维护dp

    题目链接 题目大意: 就是给你很多行的010101串,长度是1e91e91e9,每一行都有若干段的连续的111,对于给定的串集合是美丽的美丽的美丽的的条件是任意相邻两行的串至少有一个列是同时有111的 ...

  4. cf737d Ezzat and Grid

    题面 题意 给定 n n n 行 1 0 9 10^9 109 列的 01 矩阵.第 i i i 行和 i + 1 i+1 i+1 行是相邻的当且仅当至少存在一列,这两行这一列的数都是 1.问最少删掉 ...

  5. Codeforces Round #737 (Div. 2)

    Codeforces Round #737 (Div. 2) 题号 题目 知识点 A Ezzat and Two Subsequences 思维(略) B Moamen and k-subarrays ...

  6. 使用NVIDIA GRID vPC支持视频会议和算力工具

    使用NVIDIA GRID vPC支持视频会议和算力工具 随着2020年的发展,远程工作解决方案已成为许多人的新常态.企业正在寻找行之有效的解决方案,如虚拟桌面基础设施(VDI),以使他们的团队能够在 ...

  7. CSS grid 的用法

    grid 的用法 加三个宽度为 200px 的列. .container {display: grid;grid-template-columns: 200px 200px 200px; } 用 fr ...

  8. mvc4 ajax grid,mvc4中用上一种grid

    view 视图@modelIEnumerable@using(Html.Configurator("The grid should...").PostTo("FirstL ...

  9. datagrid底部显示水平滚动_DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...

最新文章

  1. 如何学习iphone游戏开发
  2. C++11- const, const expression和constexpr
  3. java 图片上传非jsp_java问题用java代码在后台如何将jsp页面上的图片上传(是 爱问知识人...
  4. PHP设计模式(6)迭代器模式
  5. jQuery选择器遇上一些特殊字符
  6. 无法安装 DotNetCore.1.0.0-VS2015Tools.Preview2解决方法
  7. 最新Activity与Fragment完全理解
  8. POJ2187 Beauty Contest
  9. centos 解决不在 sudoers 文件中。此事将被报告的问题
  10. matlab遗传工具箱ga,用遗传算法工具箱(GA)识别Bouc-Wen模型微分方程参数
  11. Excel/WPS之粘贴可见内容
  12. 面板数据,面板数据的三种基本模型
  13. 滤波笔记四:扩展卡尔曼滤波
  14. 【Usaco2008 Mar】土地购买
  15. 细胞穿膜肽( CPPs)偶联肽核酸Tat-modified-PNA|C-myc tag-PNA|SSBP(I)-PNA|Tp-10-PNA|PTD-4-PNA
  16. 国内开源镜像网站列表
  17. revit二开之过滤族(Family)
  18. 如何向oracle中导入数据,Oracle导入导出数据库的语法_Oracle_Oracle语法_Oracle数据库_课课家...
  19. 怎么把录音转文字?快把这些方法收好
  20. java毕设项目婚纱摄影网站(附源码)

热门文章

  1. 现代女性都有哪些烦恼?
  2. 史上最被低估的两个学科!它们远比你想的更重要!
  3. 我女朋友让我删前任,我明明删了她还是要分手...
  4. 奥林匹克数学竞赛教练员汇编,最牛奥数资料全集!
  5. 西游记里学化学,请收下我的膝盖~ | 今日最佳
  6. 看TensorFlow如何玩转深度学习
  7. 程序员江湖鄙视链大全,看看你处于链条的哪一级?
  8. 为什么技术与产品沟通起来总是那么痛苦
  9. 触发键盘_雷蛇这款光轴机械键盘开箱评测,光速触发,颜值爆表
  10. 6计算机系统的组成是,计算机系统的组成(范文)(6页)-原创力文档