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 ...
随机推荐
- FFmpeg时间戳详解
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10584910.html 1. I帧/P帧/B帧 I帧:I帧(Intra-coded pi ...
- Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...
- (转)分享一个技巧,利用批处理调用ruby脚本(可能你为路径苦恼)
#关闭命令显示 @echo off #提示信息 echo Now,listing the controller,please not shutdown the DOS File! #切换到当前路径,. ...
- WPF 使用RPC调用其他进程
如果在 WPF 需要用多进程通信,一个推荐的方法是 WCF ,因为 WCF 是 RPC 计算.先来讲下 RPC (Remote Procedure Call) 远程过程调用,他是通过特定协议,包括 t ...
- 【Mysql】mysql乐观锁总结和实践
乐观锁介绍: 乐观锁( Optimistic Locking ) 相对悲观锁而言,乐观锁假设认为数据一般情况下不会造成冲突,所以在数据进行提交更新的时候,才会正式对数据的冲突与否进行检测,如果发现冲突 ...
- Centos7 firewalld 基本使用
Centos7 的防火墙 firewalld比较常见 简单介绍使用 详细介绍链接推荐: https://blog.csdn.net/buster_zr/article/details/806049 ...
- 一张图弄懂opengl的诸多库gl glu glut freeglut glew glfw之间关系
开始学习opengl,但是看opengl编程指南不同版本之间使用了一堆不同的库,概念名称全都搅起的,越看越糊涂,遂整理的一下opengl相关的一些库的名词, 才发现是不同时期不同版本不断发展的结果. ...
- 每个JavaScript工程师都应懂的33个概念
摘要: 基础很重要啊! 原文:33 concepts every JavaScript developer should know 译文:每个 JavaScript 工程师都应懂的33个概念 作者:s ...
- windows使用笔记-google-chrome下载地址
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! google-chrome下载地址:https://www.google.cn/intl/zh-CN/chrome/
- JS日期Date详解与实例扩展
一:Date类型介绍 要创建一个日期对象,使用new操作符和Date构造函数即可: var now = new Date(); Date.parse()方法 其中Date.parse()方法接收一个表 ...