题目链接

题目意思很简单nm的矩阵里, 选若干个ab的小矩阵, 定义每个矩阵的值为这个矩阵里的所有数的和-最小值*数的个数。



选小矩阵时, 优先选值最小的,然后次小的.. 知道不能选位置。

输出所有矩阵的左上角那个数的坐标以及这个矩阵的值。

思路很简单, 将所有矩阵的值加到一个优先队列里面, 然后一个一个的删除。 直到矩阵空。

不好做的是求一个矩阵中的最小值。 我先是用二维线段树, tle。 然后用二维st表, mle....

然后看cf上的代码 ,用一个multiset来搞。 具体看代码..

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <complex>
  7. #include <cmath>
  8. #include <map>
  9. #include <set>
  10. #include <string>
  11. #include <queue>
  12. #include <stack>
  13. #include <bitset>
  14. using namespace std;
  15. #define pb(x) push_back(x)
  16. #define ll long long
  17. #define mk(x, y) make_pair(x, y)
  18. #define lson l, m, rt<<1
  19. #define mem(a) memset(a, 0, sizeof(a))
  20. #define rson m+1, r, rt<<1|1
  21. #define mem1(a) memset(a, -1, sizeof(a))
  22. #define mem2(a) memset(a, 0x3f, sizeof(a))
  23. #define rep(i, n, a) for(int i = a; i<n; i++)
  24. #define fi first
  25. #define se second
  26. typedef complex <double> cmx;
  27. typedef pair<int, int> pll;
  28. const double PI = acos(-1.0);
  29. const double eps = 1e-8;
  30. const int mod = 1e9+7;
  31. const int inf = 1061109567;
  32. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  33. const int maxn = 1002;
  34. int vis[maxn][maxn], n, m, c[maxn][maxn], d[maxn][maxn];
  35. ll sum[1002][1002];
  36. struct node
  37. {
  38. ll val;
  39. int x, y;
  40. bool operator < (node a) const{
  41. if(val != a.val)
  42. return val>a.val;
  43. if(x != a.x)
  44. return x>a.x;
  45. return y>a.y;
  46. }
  47. node() {}
  48. node(ll _val, int _x, int _y):val(_val), x(_x), y(_y){}
  49. };
  50. vector <node> ans;
  51. int main()
  52. {
  53. int a, b;
  54. priority_queue <node> q;
  55. cin>>n>>m>>a>>b;
  56. for(int i = 1; i <= n; i++) {
  57. for(int j = 1; j <= m; j++) {
  58. scanf("%I64d", &sum[i][j]);
  59. c[i][j] = sum[i][j];
  60. }
  61. }
  62. for(int i = 1; i <= n; i++) {
  63. multiset <int> tmp;
  64. for(int j = 1; j <= m; j++) {
  65. if(j>b) {
  66. tmp.erase(tmp.find(c[i][j-b]));
  67. }
  68. tmp.insert(c[i][j]);
  69. d[i][j] = *(tmp.begin());
  70. }
  71. }
  72. for(int j = 1; j <= m; j++) {
  73. multiset <int> tmp;
  74. for(int i = 1; i <= n; i++) {
  75. if(i>a) {
  76. tmp.erase(tmp.find(d[i-a][j]));
  77. }
  78. tmp.insert(d[i][j]);
  79. c[i][j] = *(tmp.begin());
  80. }
  81. }
  82. for(int i = 1; i <= n; i++) {
  83. for(int j = 1; j <= m; j++) {
  84. sum[i][j] += sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
  85. }
  86. }
  87. for(int i = 1; i <= n-a+1; i++) {
  88. for(int j = 1; j <= m-b+1; j++) {
  89. int x = i+a-1;
  90. int y = j+b-1;
  91. ll num = sum[x][y]-sum[i-1][y]-sum[x][j-1]+sum[i-1][j-1];
  92. ll res = num - 1LL*c[x][y]*a*b;
  93. q.push(node(res, i, j));
  94. }
  95. }
  96. while(!q.empty()) {
  97. node tmp = q.top(); q.pop();
  98. if(vis[tmp.x][tmp.y])
  99. continue;
  100. ans.pb(tmp);
  101. for(int i = max(1, tmp.x-a+1); i <= min(tmp.x+a-1, n); i++) {
  102. for(int j = max(tmp.y-b+1, 1); j <= min(tmp.y+b-1, m); j++) {
  103. vis[i][j] = 1;
  104. }
  105. }
  106. }
  107. cout<<ans.size()<<endl;
  108. sort(ans.begin(), ans.end());
  109. reverse(ans.begin(), ans.end());
  110. for(int i = 0; i < ans.size(); i++) {
  111. printf("%d %d %I64d\n", ans[i].x, ans[i].y, ans[i].val);
  112. }
  113. return 0;
  114. }

codeforces 15D . Map 优先队列的更多相关文章

  1. CodeForces 15D Map

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译.(注意翻译里有错误,应该是优先选上面的矩阵,在同一行的优先选左边的矩阵) 这题一看就会做啊 (以下设大矩阵是\( ...

  2. CodeForces 912d fishes(优先队列+期望)

    While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...

  3. CodeForces - 721D 贪心+优先队列(整理一下优先队列排序情况)

    题意: 给你一个长度为n的数组,你可以对其中某个元素加上x或者减去x,这种操作你最多只能使用k次,让你输出操作后的数组,且保证这个数组所有元素的乘积尽可能小 题解: 在这之前我们要知道a*b>a ...

  4. codeforces 446B(优先队列)

    题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...

  5. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  6. STL之容器(1)

    STL容器类的模板 容器部分主要由头文件<vector>,<list>,<deque>,<set>,<map>,<stack>和 ...

  7. [luogu1110][报表统计]

    题目链接 思路 set+map+优先队列就可以水过去.可以发现,每插入一个元素,都会使得操作2中原来相邻的那个差值消失,然后多了两个新的差值.对于新的差值,只要直接扔到优先队列里就好了.那么删除呢.可 ...

  8. gym 101911

    A. Coffee Break 题意:每天有m小时,你喝咖啡需要花一小时,你想在n个时刻都喝过一次咖啡,老板规定连续喝咖啡的间隔必须是d以上,求最少需要多少天才能喝够n次咖啡,并输出每个时刻第几天喝. ...

  9. 通过Jedis操作Redis

    package com.yh; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis ...

随机推荐

  1. oc结构

    结构 在oc中只能声明变量 不能声明函数和类 结构声明 struct DateT { int month; int day; int year; }; 结构可以在起最后的分号之后定义结构变量,并且可以 ...

  2. 【IOS学习基础】内存管理

    1.内存几大区域 1> 栈区:局部变量(基本数据类型.指针变量). 2> 堆区:程序运行的过程中动态分配的存储空间(创建的对象). 3> BSS段:没有初始化的全局变量和静态变量. ...

  3. c#部分常用方法

    此文章不断补充 1.判断该字符串是否存在于字符串数组中 string[] arr = {"aaa","bbb","aba","cc ...

  4. STL_函数模板

    #include <iostream>#include <string>using namespace std; #define MAX(T) \ T max_##T (T x ...

  5. TCP/IP详解之:UDP协议

    第11章 UDP协议  UDP首部 UDP的检验和是可选的,而TCP的检验和是必须的: UDP的检验和是端到端的检验和.由发送端计算,由接收端验证: 尽管UDP的检验和是可选的,但总是推荐被使用 IP ...

  6. JNI加载Native Library 以及 跨线程和Qt通信

    Part1 Java Native Interface-JNI-JAVA本地调用 JNI标准是Java平台的一部分, 允许Java代码和其他语言进行交互; 开始实现-> Step 1) 编写Ja ...

  7. Android 相对布局常用属性

    Android 布局属性详解 RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false     android:layout_centerHrizontal  水平居中 ...

  8. php 数组去除空值

    /** * 方法库-数组去除空值 * @param string $num 数值 * @return string */ public function array_remove_empty(& ...

  9. HibernateTransactionManager 和 hibernateTemplate的区别

    在applicationContext.xml中有如下配置: <bean id="hibernateTemplate" class="org.springframe ...

  10. 未能加载文件或程序集 Microsoft.ReportViewer.Common, Version=11.0.0.0

    原文:未能加载文件或程序集 Microsoft.ReportViewer.Common, Version=11.0.0.0 System.IO.FileNotFoundException: 未能加载文 ...