zoukankan      html  css  js  c++  java
  • 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; }





  • 相关阅读:
    【转载】如何写一封有说服力的投稿信
    【实践】IEEE下载Citations并导入EndNote
    【实践】使用NotePad++编写批量添加文件名后缀的java程序
    【实践】如何下载Gurobi的历史版本
    【实践】origin画局部放大图,并和原图在一张图中(附演示视频)
    【实践】Origin设置图的跳点间隔
    【实践】多条曲线在一幅图上,Origin如何对每一条曲线单独设置
    Two classes of spectral conjugate gradient methods for unconstrained optimizations (Numerical testing reports)
    协同ADMM求解考虑碳排放约束直流潮流问题的对偶问题(附文章和程序下载地址)
    【实践】第十五届中国研究生数学建模竞赛之机场登机口调度第一问(附问题数据和程序)
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10743912.html
Copyright © 2011-2022 走看看