牛客网暑期ACM多校训练营(第二场)J farm

链接:https://www.nowcoder.com/acm/contest/140/J
来源:牛客网
 

题目描述

White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. The plant in the j-th column of the i-th row belongs the a[i][j]-th type.
White Cloud wants to help White Rabbit fertilize plants, but the i-th plant can only adapt to the i-th fertilizer. If the j-th fertilizer is applied to the i-th plant (i!=j), the plant will immediately die.
Now White Cloud plans to apply fertilizers T times. In the i-th plan, White Cloud will use k[i]-th fertilizer to fertilize all the plants in a rectangle [x1[i]...x2[i]][y1[i]...y2[i]].
White rabbits wants to know how many plants would eventually die if they were to be fertilized according to the expected schedule of White Cloud.

输入描述:

The first line of input contains 3 integers n,m,T(n*m<=1000000,T<=1000000)
For the next n lines, each line contains m integers in range[1,n*m] denoting the type of plant in each grid.
For the next T lines, the i-th line contains 5 integers x1,y1,x2,y2,k(1<=x1<=x2<=n,1<=y1<=y2<=m,1<=k<=n*m)

输出描述:

Print an integer, denoting the number of plants which would die.

示例1

输入

复制

2 2 2
1 2
2 3
1 1 2 2 2
2 1 2 1 1

输出

复制

3

题意十分简单有一个初始矩阵然后改变x1,y1到x2,y2这个小矩阵的值 判断最后有多少个值与初始矩阵不同

看了题解之后发现 随机算法十分神奇 

将矩阵中的值离散化成极大的随机值,那么我们判断是否改变将会十分简单

假设将7随机成了 123456789 那么我们每次将矩阵改变值变成为加上一个值

如果最后的值m%123456789=0那么有极大的可能没有发生改变

这是为什么呢?如果7还是7的话那么有可能7+2+5=14 14%2=0 这会造成错误

如果是123456789那么需要其他相加的值正好拼成123456789的倍数

并且其他的离散值也十分巨大 所以几乎没有可能拼成123456789的倍数

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int n,m,i,j,T,x1,x2,y3,y2,k,x;
vector<int> mp[maxn],sum[maxn];
int ran[maxn],die;
int main()
{
    srand(233);
    scanf("%d%d%d",&n,&m,&T);
    for(i=0;i<=n+1;i++)
    {
          for(j=0;j<=m+1;j++)
        {
          sum[i].push_back(0);
          mp[i].push_back(0);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            scanf("%d",&x);
            mp[i][j]=x;
            ran[(i-1)*m+j]=(rand()<<16)+rand();
        }
    }
    for(i=1;i<=T;i++)
    {
        scanf("%d%d%d%d%d",&x1,&y3,&x2,&y2,&k);
        sum[x1][y3]+=ran[k];
        sum[x1+1][y3]-=ran[k];
        sum[x1][y3+1]-=ran[k];
        sum[x2+1][y3+1]+=ran[k];
    }
    int ans=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
            if(sum[i][j]%ran[mp[i][j]])ans++;
        }
    }
        printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ljq199926/article/details/81213421