SPOJ 274 Johnny and the Watermelon Plantation(TLE)
O(n^3)的时间复杂度,改了半天交了二三十遍,TLE到死,实在没办法了……
跪求指点!!!
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm> using namespace std; const int MAXN = ;
const int INF = << ; int mat[MAXN][MAXN];
int SubSum[MAXN][MAXN];
int tempS[MAXN];
int N, K;
int maxI, maxJ;
int hang[MAXN], cntH;
int lie[MAXN], cntL; //预处理左上角[1,1]右下角[i,j]的所有矩阵和
void init()
{
memset( SubSum, , sizeof(SubSum) );
for ( int i = ; i <= maxI; ++i )
{
int tmps = ;
for ( int j = ; j <= maxJ; ++j )
{
tmps += mat[i][j];
SubSum[i][j] = SubSum[i - ][j] + tmps;
}
}
return;
} void solved()
{
int ans = INF;
for ( int i = ; i < cntH; ++i ) //枚举矩形上边界
for ( int j = i; j < cntH; ++j ) //枚举矩形下边界
{
int stL = hang[i], edL = hang[j];
tempS[] = ;
int st = ;
//扫描法,参考刘汝佳训练指南p.48 LA 2678
for ( int m = ; m <= cntL; ++m )
{
int k = lie[m - ];
tempS[m] = SubSum[edL][k] - SubSum[edL][k-] - SubSum[stL-][k] + SubSum[stL-][k-];
tempS[m] += tempS[m - ];
if ( tempS[st - ] > tempS[m] - K ) continue;
while ( tempS[st] <= tempS[m] - K ) ++st;
ans = min( ans, ( lie[m - ] - lie[st - ] ) * ( edL - stL ) );
}
}
printf( "%d\n", ans );
return;
} int main()
{
//freopen( "in.txt", "r", stdin );
int T;
scanf( "%d", &T );
while ( T-- )
{
memset( mat, , sizeof(mat) );
scanf( "%d%d", &N, &K );
maxI = ;
maxJ = ;
cntL = , cntH = ;
bool flag = false;
for ( int i = ; i < N; ++i )
{
int x, y, f;
scanf("%d%d%d", &x, &y, &f );
hang[cntH++] = x; //行列离散化
lie[cntL++] = y;
maxI = max( maxI, x );
maxJ = max( maxJ, y );
mat[x][y] = f;
if ( f >= K ) //特殊情况,只覆盖一个点
{
flag = true;
}
} if ( N == || flag )
{
puts("");
continue;
} sort( hang, hang + cntH );
sort( lie, lie + cntL );
cntH = unique( hang, hang + cntH ) - hang;
cntL = unique( lie, lie + cntL ) - lie; init();
solved();
}
return ;
}
SPOJ 274 Johnny and the Watermelon Plantation(TLE)的更多相关文章
- Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...
- Codeforces 1361C - Johnny and Megan's Necklace(欧拉回路)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这个题作为 D1C 还是蛮合适的-- 首先不难发现答案不超过 \(20\),所以可以直接暴力枚举答案并 check 答案是否 ...
- 刷题防止Time Limit Exceeded(TLE)技巧
1.C++ 不要使用cin,cout,该使用scanf和printf 2.Java 不要使用Scanner,改用BufferedReader 3.Python 在文件开始的地方加入 import ps ...
- 利用TLE数据确定卫星轨道(1)-卫星轨道和TLE
1.卫星轨道 太空中卫星和天体在各种引力的作用下都在一定的轨道中周期转动着,但实际的轨道是很复杂的,一般的项目也达不到那么精确的需求(其实精确的卫星参数数据也不可能随便公开的),所以采用一阶近似的开普 ...
- LeetCode(62)Unique Paths
题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【SPOJ】QTREE6(Link-Cut-Tree)
[SPOJ]QTREE6(Link-Cut-Tree) 题面 Vjudge 题解 很神奇的一道题目 我们发现点有黑白两种,又是动态加边/删边 不难想到\(LCT\) 最爆力的做法,显然是每次修改单点颜 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
随机推荐
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 安装Netsharp演示插件
阅读本文请先阅读Netsharp下载及环境搭建 Netsharp提供了DEMO程序,DEMO完成的功能是Netsharp文章系列中的Netsharp快速入门系列中介绍的功能,DEMO是以Netshar ...
- ubuntu重置root密码
from: http://mmicky.blog.163.com/blog/static/150290154201398113034698/ 使用ubuntu的时候忘记了root密码该如何重置?我使用 ...
- MySQL 分组
MySQL GROUP BY 语句 GROUP BY 语句根据一个或多个列对结果集进行分组. 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. GROUP BY 语法 SELECT ...
- WEB学习总结 +数据结构
HTML5 <h1>会员注册界面</h1><form action="process.aspx" method="post" n ...
- Codeforces Round #131 (Div. 2) E. Relay Race dp
题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...
- Linux进程栈和线程栈
参考资料: http://blog.csdn.net/xhhjin/article/details/7579145 总结: 1.进程的栈大小是在进程执行的时刻才能指定的,即不是在编译的时候决定 ...
- Leetcode#49 Anagrams
原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...
- java 解决JFrame不能设置背景色的问题 分类: Java Game 2014-08-15 09:48 119人阅读 评论(0) 收藏
这段时间比较多,于是写一写JAVA的一些IT技术文章.如有JAVA高手请加QQ:314783246,互相讨论. 在Java的GUI设计中,Frame和JFrame两者之间有很大差别,上次刚学时编一个窗 ...
- sublime text3 插件安装
安装Package control 先打开安装代码的命令行 按 ctrl+~或者 view -> show console 将下面的代码粘贴到输入框里 按回车 import urllib.re ...