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 ...
随机推荐
- Maven教程1(介绍安装和配置)
官网地址:http://maven.apache.org/ 1.Maven介绍 1.1为什么需要使用Maven 之前学Spring和SpringMVC的时候我们需要单独自己去找相关的jar. 这些ja ...
- HAProxy(二):HAProxy的ACL规则实现智能负载均衡详解与示例
一.HAProxy的ACL的功能 ACL(Access Control List)访问控制列表,HAProxy中的ACL的匹配条件和控制条件有许多种,功能很强大,可以通过源地址.源端口.目标地址.目标 ...
- 一张 JVM 相关的思维脑图(4.4M)
楼主学习 JVM 总结的知识点,用思维脑图串起来,温故而知新,其中含有类加载器,内存布局,GC(右侧). 最多的就是 GC 的内容了. 内容有错误之处,还请指正. 大图地址
- MVC基础篇—控制器与视图数据的传递
Viewdata,Viewbag,Tempdata 1 Vewdata:简单来说就是数据字典,通过键值对的形式来存放数据.举例如下: //后台控制器代码: public ActionResult V ...
- .net导出excle无需任何插件,直接通过一个tablehtml实现
项目背景: 项目需要导出样式复杂的excl表格,主要是一些样式布局比较复杂 技术分析: 目前比较通用的实现方式有 1.借助微软的excle插件 2.通过NPOI插件实现 3.直接导出一个html(ta ...
- c# 匿名方法几种表现形式
delegate int del(int a); static void Main(string[] args) { //匿名方法的几种表现形式 del del = delegate (int x) ...
- IQuerable与IEnumable的区别
核心区别: IQueryable该接口会把查询表达式先缓存到表达式树Expression 中,只有当真正用到数据的时候(例如 遍历 ),才会由IQueryProvider解析表达式树,生成sql语句执 ...
- 具体CAS操作实现(无锁算法)
具体CAS操作 上一篇讲述了CAS机制,这篇讲解CAS具体操作. 什么是悲观锁.乐观锁?在java语言里,总有一些名词看语义跟本不明白是啥玩意儿,也就总有部分面试官拿着这样的词来忽悠面试者,以此来找优 ...
- 有道云笔记链接——JAVA面向对象的学习
http://note.youdao.com/noteshare?id=cf39a0e493a6b3c7ad5d22204a7e7843
- 【20】策略者模式(Strategy Pattern)
一.引言 本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方法的抽象,下面具体分享下我对策略模式的理解. 二.策略者模式介绍 2.1 策略模式的定义 在现实生活中,策略模式的 ...