codechef Row and Column Operations 题解
2014-05-06 10:35
337 查看
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.
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 a single line containing the maximum number at the grid after performing all the operations.
1 ≤ N ≤ 314159
1 ≤ Q ≤ 314159
1 ≤ X ≤ 3141
1 ≤ R, C ≤ N
很好的题目,巧妙地计算最终结果。
注意:
1 行列分开计算,最后组合最大值就是答案了, 不用搜索二维表
2 只需要记录行列的最终结果就可以,不用模拟全过程
3 数据量非常大,处理输入问题
下面程序0ms通过,全网站本题最好答案,呵呵。
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 operationin the format described above.
Output
Output a single line containing the maximum number at the grid after performing all the operations.
Constraints
1 ≤ N ≤ 3141591 ≤ 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; }
相关文章推荐
- generate html code from QTableWidget with row and column span support
- CodeChef Chef and Strange Operations
- Set row and column to 1's
- AC自动机+DP 改变字符串中的‘?’使得在字典中匹配到的次数最多 codechef Lucy and Question Marks
- Chef and Prime Divisors Problem code: CHAPD (GCD问题)
- wpf 自定义Grid RowDefinitions and ColumnDefinitions 赋*值
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
- What the key facts to choose Row Store and Column Store
- codechef Tree and Queries Solved
- [CodeChef-DGTCNT]Chef and Digits
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
- Codechef December Challenge 2014 Chef and Apple Trees 水题
- CodeChef SEAVOTE Sereja and Votes
- codechef Polo the Penguin and the Tree
- [Bluestein's Algorithm DFT] Codechef REALSET. Petya and Sequence
- codechef Chef and The Right Triangles 题解
- ●CodeChef Sereja and Game
- [计数 补集转换][阈值] Codechef SEAARC.Sereja and Arcs
- CodeChef Chef and Churu [分块]