codechef Row and Column Operations 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载。
https://blog.csdn.net/kenden23/article/details/25105267
You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid:
- RowAdd R X: all numbers in the row R should be increased by X.
- ColAdd C X: all numbers in the column C should be increased by X.
Now after performing the sequence of such operations you need to find the maximum element in the grid.
Input
The first line of the input contains two space separated integers N and Q denoting the size of the grid and the number of performed operations respectively. Each of the following Q lines describe an operation
in the format described above.
Output
Output a single line containing the maximum number at the grid after performing all the operations.
Constraints
- 1 ≤ N ≤ 314159
- 1 ≤ Q ≤ 314159
- 1 ≤ X ≤ 3141
- 1 ≤ R, C ≤ N
Example
Input:
2 4
RowAdd 1 3
ColAdd 2 1
ColAdd 1 4
RowAdd 2 1 Output:
7
原题:
http://www.codechef.com/problems/ROWCOLOP
非常好的题目,巧妙地计算终于结果。
注意:
1 行列分开计算,最后组合最大值就是答案了, 不用搜索二维表
2 仅仅须要记录行列的终于结果就能够。不用模拟全过程
3 数据量非常大,处理输入问题
以下程序0ms通过。全站点本题最好答案,呵呵。
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class RowAndColumnOperations
{
static const int MAX_BU = 5120;
int st, len;
char buffer[MAX_BU];
char getFromBuffer()
{
if (st >= len)//st <= len?
?? Really?
more careful!!!
{
len = fread(buffer, 1, MAX_BU, stdin);//forget len=??
?
st = 0;
}
return buffer[st++];
}
char getCharFromBuf()
{
char c = getFromBuffer();
while (len)
{
if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') return c;
c = getFromBuffer();
}
return '0';
}
int getInt()
{
char c = getFromBuffer();
while ((c < '0' || '9' < c) && len)
{
c = getFromBuffer();
}
int num = 0;
while ('0' <= c && c <= '9' && len)
{
num = (num<<3) + (num<<1) + (c - '0');
c = getFromBuffer();
}
return num;
}
public:
RowAndColumnOperations() : st(0), len(0)
{
int N, Q, RC, addNum, MaxC = 0, MaxR = 0;
N = getInt()+1;
int *rows = (int *) malloc(sizeof(int) * N);
int *cols = (int *) malloc(sizeof(int) * N);
memset(rows, 0, sizeof(int) * N);
memset(cols, 0, sizeof(int) * N);
Q = getInt();
char Commands[7];
while (Q--)
{
for (int i = 0; i < 6; i++)
{
Commands[i] = getCharFromBuf();
}
RC = getInt();
addNum = getInt();
if (Commands[0] == 'R')
{
rows[RC] += addNum;
MaxC = MaxC < rows[RC] ? rows[RC] : MaxC;
}
else
{
cols[RC] += addNum;
MaxR = MaxR < cols[RC] ? cols[RC] : MaxR;
}
}
printf("%d", MaxC + MaxR);
free(rows);
free(cols);
}
};
int rowAndColumnOperations()
{
RowAndColumnOperations();
return 0;
}
codechef Row and Column Operations 题解的更多相关文章
- LeetCode 947. Most Stones Removed with Same Row or Column
原题链接在这里:https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/ 题目: On a 2D plane ...
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
1: /// <summary> 2: /// Write an algorithm such that if an element in an MxN matrix is 0, it ...
- excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1
编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...
- Flutter 布局(七)- Row、Column详解
本文主要介绍Flutter布局中的Row.Column控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Row A widget that displays its children ...
- params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render
params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render
- Flutter 布局类组件:线性布局(Row和Column)
前言 所谓线性布局,即指沿水平或垂直方向排布子组件.Flutter中通过Row和Column来实现线性布局,并且它们都继承自弹性布局(Flex). 接口描述 Row({ Key key, // 表示子 ...
- 12.Quick QML-QML 布局(Row、Column、Grid、Flow和嵌套布局) 、Repeater对象
1.Row布局 Row中的item可以不需要使用anchors布局,就能通过行的形式进行布局. 并且item可以使用Positioner附加属性来访问有关其在Row中的位置及其他信息. 示例如下所示, ...
- 陈年佳酿之 - Winform ListView 控件 double click 事件中获取选中的row与column
背景 最近收到了一个关于以前项目的维护请求,那时的楼主还是刚刚工作的小青年~~~ 项目之前使用的是.net/winform.今天重新打开代码,看着之前在FrameWork2.0下面的代码, 满满的回忆 ...
- [Swift]LeetCode947. 移除最多的同行或同列石头 | Most Stones Removed with Same Row or Column
On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at ...
随机推荐
- 常见HTTP状态码及URL编码表
常见HTTP状态码 1xx: 信息 (用于表示临时响应并需要请求者执行操作才能继续的状态代码) 消息: 描述: 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有 ...
- 【学习笔记】深入理解HTTP协议
参考:关于HTTP协议,一篇就够了,感谢作者认真细致的总结,本文在理解的基础上修改了内容,加深印象的同时也希望对大家有所帮助 HTTP是什么? HTTP协议是Hyper Text Transfer P ...
- Hive基础之绪论
我本人大概是从2013年12月份开始接触Hadoop,因为公司当时要开始处理一些数据量比较大的数据,现有的通过程序去统计数据的方式在效率方面渐渐不能满足业务需求,所以便开始了Hadoop技术的探索,即 ...
- 分布式计算框架Spark
Apache Spark是一个开源分布式运算框架,最初是由加州大学柏克莱分校AMPLab所开发. Hadoop MapReduce的每一步完成必须将数据序列化写到分布式文件系统导致效率大幅降低.Spa ...
- 菜鸟入门【ASP.NET Core】12:JWT 设计解析及定制
前言 上一节我们讲述的书如何使用jwt token,而且上一节的token是要加Authorization:bearer XXXXXXXXXXXX才能访问. 这一节我们来研究如何自定义类似jwt的to ...
- 网络编程学习二(IP与端口)
InetAddress类 封装计算机的ip地址,没有端口 // 使用getLocalHost方法创建InetAddress对象 InetAddress addr = InetAddress.getLo ...
- Python 单例设计模式
class Foo: def __init__(self, name, age): self.name = name self.age = age def show(self): print(self ...
- pycharm技巧
常用快捷键 1.Ctrl + Enter:在下方新建行但不移动光标: 2.Shift + Enter:在下方新建行并移到新行行首: 3.Ctrl + /:注释(取消注释)选择的行: 4.Ctrl + ...
- spring 开发 Tars
和不使用 Spring 的 tars HelloWord 项目相比,客户端完全一样,服务端两个地方不一样 创建不使用 Spring 的 tars HelloWord 步骤: https://www.c ...
- Oracle 关闭数据库(未使用Oracle Restart)
Oracle关闭数据库(未使用Oracle Restart) by:授客 QQ:1033553122 SHUTDOWN [选项] 选项说明: NORMAL-语句执行后,不允许创建新的连接:等待所有当前 ...