Gym 100851AAdjustment Office 解题报告
来源:互联网 发布:中级经济师题库软件 编辑:程序博客网 时间:2024/06/07 18:42
Problem A. Adjustment Office
ACM ICPC 2015–2016, Northeastern European Regional Contest.
Input file: adjustment.in
Output file: adjustment.out
Garrison and Anderson are working in a company named “Adjustment Office”. In competing companies workers change the reality, in this company they try to predict the future.
They are given a big square board n ×n. Initially in each cell (x, y) of this board the value of x + y is written (1≤ x, y ≤ n). They know that in the future there will be two types of queries on the board:
• “R r” — sum up all values in row r,print the result and set all values in row r to zero;
• “C c” — sum up all values in column c, print the result and set all values in column c to zero.
They have predicted what queries and results there will be. They need to ensure that they have correctly predicted the results. Help them by computing the results of the queries.
Input
The first line of the input contains two integers n and q (1 ≤ n ≤ 106 , 1 ≤ q ≤ 105 ) — the size of the square and the number of queries.
Each of the next q lines contains the description of the query. Each query is either “R r” (1 ≤ r ≤ n) or “C c” (1 ≤c ≤ n).
Output
The output file shall contain q lines.The i-th line shall contain one integer — the result of the i-th query.
Sample input and output
adjustment.in
3 7
R 2
C 3
R 2
R 1
C 2
C 1
R 3
adjustment.out
12
10
0
5
5
4
0
每次操作输出对应的格子中数值的和。
如果这个操作已经进行过,依题意和一定是零。如果没有,可先假设该操作不受之前操作的干扰,使用加法结合律与等差数列求和公式求出一个和值,而后减去由于之前操作已经数值清零的格子的原值的和值。被清零的格子原值的和,可用加法结合律分成两部分。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>#define maxn 1000005typedef long long LL;using namespace std;LL rsum=0,csum=0;LL rnum=0,cnum=0;LL r[maxn],c[maxn];int main(){ LL n,q; freopen("adjustment.in", "r", stdin); freopen("adjustment.out", "w", stdout); //重定向标准输入输出 scanf("%lld%lld",&n,&q); memset(r, 0, sizeof(r)); memset(c, 0, sizeof(c)); getchar(); for(LL i=1;i<=q;i++) { char w=getchar(); LL o; scanf("%lld",&o); getchar(); LL ans=0; if(w=='R'&&!r[o]) { ans=n*o+n*(n+1)/2-o*cnum-csum; //减去由于之前的操作,已经清零的格子的值 rnum++; rsum+=o; r[o]=1; //标记已对此行进行过R操作 } else if (w=='C'&&!c[o]) { ans=n*o+n*(n+1)/2-o*rnum-rsum; //减去由于之前的操作,已经清零的格子的值 cnum++; csum+=o; c[o]=1; //标记已对此列进行过C操作 } printf("%lld\n",ans); } return 0;}
- Gym 100851AAdjustment Office 解题报告
- Gym 100851EEasy Problemset 解题报告
- Gym 100694 解题报告
- 1160 Post Office 解题报告
- pku1160 Post Office 解题报告
- POJ 1060 Post Office 分析&解题报告
- Gym 100851A - Adjustment Office(模拟)
- 【模拟】NEERC15 A Adjustment Office (Codeforces GYM 100851)
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- Pku acm 1160 post office 动态规划题目解题报告(十八)
- 四边形不等式优化dp应用------pku 1160 post office 解题报告
- Antiprime解题报告
- 大二入学感慨
- JAVA基础--LinkedHashSet 和 LinkedHashMap
- Wormholes(负圈)
- Java深入 - Java内存区域详解
- 操作系统概述
- Gym 100851AAdjustment Office 解题报告
- Six Degrees of Cowvin Bacon(最短路)
- windows server 2008R2自动关机解决方法
- 总结六条对我们学习Linux系统有用的忠告
- 也写一个变色方块的游戏
- POJ 1789
- 南京某外企开发面经
- Pessimistic Locking ——悲观锁
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建