版权声明:本文作者靖心,靖空间地址: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 题解的更多相关文章

  1. 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 ...

  2. 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 ...

  3. excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1

    编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...

  4. Flutter 布局(七)- Row、Column详解

    本文主要介绍Flutter布局中的Row.Column控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Row A widget that displays its children ...

  5. params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render

    params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render

  6. Flutter 布局类组件:线性布局(Row和Column)

    前言 所谓线性布局,即指沿水平或垂直方向排布子组件.Flutter中通过Row和Column来实现线性布局,并且它们都继承自弹性布局(Flex). 接口描述 Row({ Key key, // 表示子 ...

  7. 12.Quick QML-QML 布局(Row、Column、Grid、Flow和嵌套布局) 、Repeater对象

    1.Row布局 Row中的item可以不需要使用anchors布局,就能通过行的形式进行布局. 并且item可以使用Positioner附加属性来访问有关其在Row中的位置及其他信息. 示例如下所示, ...

  8. 陈年佳酿之 - Winform ListView 控件 double click 事件中获取选中的row与column

    背景 最近收到了一个关于以前项目的维护请求,那时的楼主还是刚刚工作的小青年~~~ 项目之前使用的是.net/winform.今天重新打开代码,看着之前在FrameWork2.0下面的代码, 满满的回忆 ...

  9. [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 ...

随机推荐

  1. 基于vue框架手写一个notify插件,实现通知功能

    简单编写一个vue插件,当点击时触发notify插件,dom中出现相应内容并且在相应时间之后清除,我们可以在根组件中设定通知内容和延迟消失时间. 1. 基础知识 我们首先初始化一个vue项目,删除不需 ...

  2. C++异常的几种捕获方式

    捕获指定的类型 这样的话可以对每种异常做出不同的处理,例如: #include <iostream> using namespace std; void A(int n){ int a = ...

  3. MVC中子页面如何引用模板页中的jquery脚本

    MVC中子页面如何引用模板页中的jquery脚本 最近在学习mvc,遇到了一个问题:在html页面中写js代码,都是引用mvc5自带的jquery脚本,虽然一拖(将指定的jquery脚本如 jquer ...

  4. Linux命令行快捷键及vim快捷方式

    Linux命令行快捷键 快捷键: tab键 自动补全路径 目录 名字, 自动不全命令 快捷键: ctrl +l(小写) 清屏 . ctrl +c 取消当前操作 快捷键: ctrl +d(小写) 退出当 ...

  5. asp.net mvc之ActionResult

    Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的, 但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的, ActionR ...

  6. C# 字符串大写转小写,小写转大写,数字保留,其他除外

    又是一道面试题,我只想到两种方式: 第一种:循环字符串,判断每个字符串的类型,再根据类型对该字符进行操作(转大写.转小写.不变或舍弃) static void Main(string[] args) ...

  7. ASP.NET Identity系列教程(目录)转载

    来源:http://www.cnblogs.com/r01cn/p/5179506.html 注:最近看到不少介绍微软ASP.NET Identity技术的文章,但感觉都不够完整深入,本人又恰好曾在A ...

  8. Shaping Regions(dfs)

    Shaping Regions Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 39[Submit][Status][Web B ...

  9. What are the differences between a pointer variable and a reference variable in C++?

    Question: I know references are syntactic sugar, so code is easier to read and write. But what are t ...

  10. Berlekamp-Massey算法学习笔记

    Berlekamp-Massey算法 很久之前就听说过这个算法,当时六校联考的时候Day1T1是一道很有意思的递推,神仙zzx不会做于是就拿BM算法艹出了递推式Orzzzzzzzzzzx 推荐一篇讲的 ...