zoukankan      html  css  js  c++  java
  • 2020牛客国庆集训派对day7 J. Adjustment Office(思维)

    题目描述

    Garrison and Anderson are working in a company named “Adjustment Office”. In competing companies workers change the reality, in this company they try to predict the future.

    They are given a big square board n × n. Initially in each cell (x, y) of this board the value of x + y is written (1 ≤ x, y ≤ n). They know that in the future there will be two types of queries on the board:

    • “R r” — sum up all values in row r, print the result and set all values in row r to zero;
    • “C c” — sum up all values in column c, print the result and set all values in column c to zero.

    They have predicted what queries and results there will be. They need to ensure that they have correctly predicted the results. Help them by computing the results of the queries.

    输入描述:

    The first line of the input contains two integers n and q (1 ≤ n ≤ 106, 1 ≤ q ≤ 105) — the size of the square and the number of queries.
    Each of the next q lines contains the description of the query. Each query is either “R r” (1 ≤ r ≤ n) or “C c” (1 ≤ c ≤ n).
    

    输出描述:

    The output file shall contain q lines. The i-th line shall contain one integer — the result of the i-th query.
    

    示例1

    输入

    复制

    3 7
    R 2
    C 3
    R 2
    R 1
    C 2
    C 1
    R 3
    

    输出

    复制

    12
    10
    0
    5
    5
    4
    0
    

    大意是给定一个n阶方阵,其中a[x] [y]的值为x + y,每次查询的时候先输出查询的行 or 列的和,然后把这行/列的值全部置0,要求输出每次查询的结果。

    注意到每个单元的初始值很特殊。当我们删去一行r的时候,先假设之前的操作没有影响,那么答案就是一个等差数列求和,然后考虑之前删除列操作的累积效果,不妨用ncol表示之前删去的总的列数,col表示之前删去的列标的和,那么累积的影响就是ncol * r + col(不懂的话可以写一下这个矩阵模拟一下),用等差数列的和减去累积的影响输出即可,不要忘记更新ncol和col,同时注意如果之前已经删过这一行的话就不要再删了(vis数组判断记录即可)。

    删去一列的时候同理。

    #include <iostream>
    using namespace std;
    #define int long long
    int row = 0, col = 0, nrow = 0, ncol = 0, n, q;
    bool visc[1000005] = {0}, visr[1000005] = {0};
    signed main()
    {
    	//freopen("data.txt", "r", stdin);
    	cin >> n >> q;
    	for(int i = 1; i <= q; i++)
    	{
    		char c;
    		int num;
    		//scanf("%c%d", &c, &num);
    		cin >> c >> num;
    		int ans = 0, tmp = 0;
    		if(c == 'R')
    		{
    			if(visr[num]) 
    			{
    				cout << 0 << endl;
    				continue;
    			}
    			tmp += n * num + (1 + n) * n / 2;
    			tmp -= (ncol * num + col);
    			ans = tmp;
    			nrow++;
    			row += num;
    			visr[num] = 1;
    		}
    		else
    		{
    			if(visc[num])
    			{
    				cout << 0 << endl;
    				continue;
    			}
    			tmp += n * num + (1 + n) * n / 2;
    			tmp -= (nrow * num + row);
    			ans = tmp;
    			ncol++;
    			col += num;
    			visc[num] = 1;
    		}
    		cout << ans << endl;
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    【原创翻译】The Free Lunch Is Over
    【原创】基于ZYNQ7000的交叉编译工具链Qt+OpenCV+ffmpeg等库支持总结(二)
    【原创】基于ZYNQ7000的交叉编译工具链Qt+OpenCV+ffmpeg等库支持总结(一)
    实现内容提供者步骤
    为什么需要内容提供者
    aidl的应用场景
    aidl介绍
    百度音乐盒框架
    通过接口方式调用服务里面的方法
    通过bindservice方式调用服务方法里面的过程
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13777630.html
Copyright © 2011-2022 走看看