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

题目描述

恬恬有一个nx n的数组。她在用这个数组玩游戏:
开始时,数组中每一个元素都是0。
恬恬会做某些操作。在一次操作中,她可以将某一行的所有元素同时加上一个值,也可以将某一列的所有元素同时加上一个值。
在几次操作后,一个元素被隐藏了。你能帮助她回忆隐藏的数是几吗?

输入描述:

  1. 第一行一个整数n1 n 1000)。
    接下来n行每行n个整数表示数组a
    第(i+1)行的第j个元素表示a

ij

  1. a

ij

  1. =-10 a

ij

  1. 10000)。-1表示隐藏的元素。

输出描述:

  1. 仅一个整数表示答案。

输入例子:
  1. 3
  2. 1 2 1
  3. 0 -1 0
  4. 0 1 0
输出例子:
  1. 1

-->

示例1

输入

复制

  1. 3
  2. 1 2 1
  3. 0 -1 0
  4. 0 1 0

输出

复制

  1. 1
  2.  
-1的位置置为inf  然后跑每一行每一列的最小值,如果不是0,就该行或列减去最小值。然后肯定-1那个位置还有数,其他位置为0. 然后输出inf-a[i][j]。
 
 
标记思维。
 
 
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <stack>
  14. using namespace std;
  15. #define rep(i,a,n) for (int i=a;i<n;i++)
  16. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  17. #define pb push_back
  18. #define mp make_pair
  19. #define all(x) (x).begin(),(x).end()
  20. #define fi first
  21. #define se second
  22. #define SZ(x) ((int)(x).size())
  23. #define FO freopen("in.txt", "r", stdin);
  24. #define debug(x) cout << "&&" << x << "&&" << endl;
  25. #define lowbit(x) (x&-x)
  26. #define mem(a,b) memset(a, b, sizeof(a));
  27. typedef vector<int> VI;
  28. typedef long long ll;
  29. typedef pair<int,int> PII;
  30. const ll mod=;
  31. const int inf = 0x3f3f3f3f;
  32. ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
  33. ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
  34. //head
  35.  
  36. const int maxn=;
  37. int n,a[maxn][maxn];
  38.  
  39. int main() {
  40. scanf("%d",&n);
  41. rep(i,,n) {
  42. rep(j,,n) {
  43. scanf("%d",&a[i][j]);
  44. if(a[i][j]==-) {
  45. a[i][j]=inf;//-1位置设为inf
  46. }
  47. }
  48. }
  49. rep(i,,n) {//找每一行的最小值
  50. int minn=inf;
  51. rep(j,,n) {
  52. if(a[i][j]<minn)
  53. minn=a[i][j];
  54. }
  55. if(minn!=) rep(j,,n) a[i][j]-=minn;//最小值不为0 减
  56. }
  57. rep(i,,n) {//找每一列的最小值
  58. int minn=inf;
  59. rep(j,,n) {
  60. if(a[j][i]<minn)
  61. minn=a[j][i];
  62. }
  63. if(minn!=) rep(j,,n) a[j][i]-=minn;//最小值不为0 减
  64. }
  65. //最后肯定是-1的位置还有数(>0) 其他位置为0
  66. int ans=;
  67. rep(i,,n) {
  68. rep(j,,n) {
  69. if(a[i][j]>)
  70. ans=inf-a[i][j];//差值即原数
  71. }
  72. }
  73. printf("%d",ans);
  74. }

还有这样的解法,但是不知道为什么?纯找规律?

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[][];
  4.  
  5. int main(){
  6. int n,i,j,x,y;
  7. //freopen("in.txt","r",stdin);
  8. scanf("%d",&n);
  9. for(i=;i<=n;i++)
  10. for(j=;j<=n;j++)
  11. {
  12. scanf("%d",&a[i][j]);
  13. if(a[i][j]==-) x=i,y=j;
  14. }
  15. int fx,fy;
  16. if(x==n) fx=x-; else fx=x+;
  17. if(y==n) fy=y-; else fy=y+;
  18. printf("%d\n",a[x][fy]+a[fx][y]-a[fx][fy]);
  19. return ;
  20. }

还有一种思想,大佬说是分块??有一点点明白,就是分成n块,每行每列加一个数,那么n块都加上了,也就是说,这n块加的数是相同的。 (i+j)%n分块

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int ans[],N;
  5. int main()
  6. {
  7. scanf("%d",&N);
  8. int x,t;
  9. for(int i=;i<N;i++)
  10. for(int j=;j<N;j++)
  11. {
  12. scanf("%d",&x);
  13. if(x!=-)
  14. ans[(i+j)%N]+=x;
  15. else t=(i+j)%N;//-1的块
  16. }
  17. printf("%d\n",ans[(t+)%N]-ans[t]);//其他块 - (-1的块)
  18. return ;
  19. }

牛客国庆集训day6 B Board (模拟标记思维或找规律或分块???)的更多相关文章

  1. 牛客国庆集训派对Day6 A Birthday 费用流

    牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...

  2. 2019牛客国庆集训派对day5

    2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...

  3. 牛客国庆集训day5 G 贵族用户 (模拟)

    链接:https://www.nowcoder.com/acm/contest/205/G来源:牛客网 题目描述 终于活成了自己讨厌的样子. 充钱能让你变得更强. 在暖婊这个游戏里面,如果你充了x元钱 ...

  4. 牛客国庆集训派对Day_4~6

    Day_4 A.深度学习 题目描述 小 A 最近在研究深度学习,他自己搭建了一个很牛逼的神经网络,现在他手头一共有 n 组训练数据,一开始他会给自己的神经网络设置一个 batch size,假设为 B ...

  5. 牛客国庆集训派对Day1 L-New Game!(最短路)

    链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  6. 牛客国庆集训派对Day4 J-寻找复读机

    链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  7. 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)

    链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  8. 牛客国庆集训派对Day1-C:Utawarerumono(数学)

    链接:https://www.nowcoder.com/acm/contest/201/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...

  9. 牛客国庆集训派对Day2 Solution

    A    矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...

随机推荐

  1. appium_python 怎样实现参数化自动生成用例

    1.对于一种对同一个页面同一点 要用不同数据测试形成多条测试用例,如果复制的话 会让代码很冗长,并且并不好维护,现在用封装的方法把 不变的代码 和 变化的参数 分别封装,形成动态 生成测试用例 ,主要 ...

  2. 1106SQLserver基础--变量、运算符的使用,if...else,while语句

    数据库---变量(对数据库中的数据没有任何影响) 作用:临时存储数据的作用,起一个衔接的作用,为了方便理解存储过程. 例:Declare @hello varchar(20) Set @hello=’ ...

  3. C# 正规表达式

    在C#中怎么用正则表达式限制文本框内不能输入数字?只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数字:" ...

  4. eclipse安卓模拟器Failed to install on device 'emulator-5554': timeout处理方案

    我们在用模拟器调试的时候,经常会出现Failed to install on device 'emulator-5554': timeout这个错误.其实就是有些虚拟器在部署的时候时间过于长.系统就认 ...

  5. python之Dict和set类型

    Dict就是一种key:value的表格: >>> d = { 'Adam':95, 'Lisa':85, 'Bart':59, 'Paul':75 } >>> p ...

  6. MyBatis总结四:配置文件xml详解

    XML 映射配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 ...

  7. PHP算法

    一,实现快速排序 <?php function quickSort($arr) { $len=count($arr) ; if($len<=1) { return $arr; } $key ...

  8. Ajax01 什么是ajax、获取ajax对象、ajax对象的属性和方法、编程步骤、缓存问题、乱码问题

    目录 1 什么是ajax 2 获取ajax对象 3 ajax对象的属性和方法 4 使用ajax的编程步骤 5 缓存问题 6 乱码问题 1 什么是ajax ajax是一种用来改善用户体验的技术,其本质是 ...

  9. win10获取超级管理员权限脚本实现

    建立一个TXT文件,把下面的脚本贴到里面,然后把后缀改成reg格式,双击添加到注册表就可以了, win10_1703版本亲测可用.... Windows Registry Editor Version ...

  10. 高性能MySQL笔记-第5章Indexing for High Performance-005聚集索引

    一.聚集索引介绍 1.什么是聚集索引? InnoDB’s clustered indexes actually store a B-Tree index and the rows together i ...