【打CF,学算法——三星级】CodeForces 701B Cells Not Under Attack (分析)
来源:互联网 发布:python 异常处理 编辑:程序博客网 时间:2024/05/22 17:27
【CF简介】
题目链接:CF 701B
题面:
Vasya has the square chessboard of size n × n andm rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which arenot under attack after Vasya puts it on the board.
The first line of the input contains two integers n andm (1 ≤ n ≤ 100 000,1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integersxi andyi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after firsti rooks are put.
3 31 13 12 2
4 2 0
5 21 55 1
16 9
100000 1300 400
9999800001
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
题意:
一个n*n的棋盘,每次放入一颗棋子(每次棋子位置不同),则该棋子所在行和列都会受影响,共m次询问,问每次放入新的棋子后,未被影响的方格数量。
解题:
初拿到这道题没什么想法,因为m为10^5,且时限为2s,估算应该是O(1)/O(nlogn)的解法比较合适。仔细分析,我们需要知道的是,插入新的棋子,有几个空位置被影响。为此,我们需呀知道原来棋盘的状态,看似好像很复杂,但实际上,我们只需要知道新插入位置所在行和所在列对棋盘的影响即可。分类讨论:如果,新行原来就是被影响的,那么这一行都不会有变化,同理,列也是如此,所以我们需要2个数组分别标记行和列的受影响情况,以及两个计数值记录受影响行数和列数。原来未受影响的列,产生的新影响数为(n-已受影响行数),行也是同理,注意下交叉点的判断即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <map>#define LL long longusing namespace std;bool row[100005],col[100005];int main(){ int m,rowc=0,colc=0,x,y;LL n,ans;scanf("%lld%d",&n,&m);ans=n*n;for(int i=0;i<m;i++){scanf("%d%d",&x,&y);if(row[x]&&col[y]); else if(!row[x]&&!col[y]){ans--;ans=ans-(n-colc)+1;colc++;ans=ans-(n-rowc)+1;rowc++;}else if(row[x]){ans=ans-(n-rowc);colc++;}else{ans=ans-(n-colc);rowc++;}row[x]=col[y]=1;printf("%lld\n",ans);}return 0;}
- 【打CF,学算法——三星级】CodeForces 701B Cells Not Under Attack (分析)
- codeforces--701B Cells Not Under Attack
- CodeForces 701B Cells Not Under Attack
- CodeForces 701B Cells Not Under Attack
- 【打CF,学算法——三星级】CodeForces 216B Forming Teams (图论)
- Codeforces 701B Cells Not Under Attack(模拟)
- 【Codeforces】-701B-Cells Not Under Attack(思维,好)
- 【CodeForces】701B - Cells Not Under Attack(思维)
- 【打CF,学算法——三星级】CodeForces 615B Longtail Hedgehog (DFS/拓扑排序)
- 【打CF,学算法——三星级】CodeForces 689B Mike and Shortcuts (最短路+spfa)
- codeforces B. Cells Not Under Attack (数学)
- CodeForces 701B Cells Not Under Attack 思维题
- 【打CF,学算法——三星级】CodeForces 701C They Are Everywhere
- 【打CF,学算法——三星级】Codeforces 9C Hexadecimal's Numbers (解法汇总)
- 【打CF,学算法——三星级】CodeForces 689C Mike and Chocolate Thieves (二分)
- 【打CF,学算法——三星级】CodeForces 550D Regular Bridge (构造)
- 【打CF,学算法——三星级】CodeForces 645C Enduring Exodus (二分+贪心)
- CF #364 (Div. 2) (B. Cells Not Under Attack 标记)
- 柚子的collection
- HDU---1269
- iOS 【程序启动原理/手动创建UIWindow/UIApplicationMain做了些什么/makeKeyAndVisible底层实现】
- 数据库增删改查的两种方式
- PAT B1026 程序运行时间
- 【打CF,学算法——三星级】CodeForces 701B Cells Not Under Attack (分析)
- 使用dubbo+maven搭建消费者跟提供者(附源码)
- HDU---1689
- 杭电-2035 人见人爱A^B
- poj2187
- 通过数据库连接池连接数据库方式
- okhttp3.0之快速入门教程
- POJ 3087Shuffle'm Up 简单的模拟题
- QSDK art分区地址映射