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 ...
随机推荐
- asp.net webservice 返回json数据乱码解决方法
[WebMethod] public void QueryRiskNotice(string phone) { try { var data = _riskNoticeDal.QueryRiskNot ...
- 通过HTML5实现发送声音
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- [转载]linux sed命令详解
简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...
- mac上安装port
今天和大家分享下 在mac os x 上安装port 本人在 OS X El Capitan 版本的操作系统上进行安装并测试. --------------------------------- 第 ...
- 【POJ】【2601】Simple calculations
推公式/二分法 好题! 题解:http://blog.csdn.net/zck921031/article/details/7690288 这题明显是一个方程组……可以推公式推出来…… 然而这太繁琐了 ...
- Swift-2-基本操作符
// Playground - noun: a place where people can play import UIKit // 基本运算符 // 运算符有3种: 单目运算符(如 -a),二目运 ...
- Static、final、abstract、接口、构造方法及java语法总结
Static:定义类的时候一般不用static来修饰,在一定意义上,用static修饰的字段可以作为全局变量,static修饰的字段和方法存储在类的内存区域,所有实例共享.static字段和方法都是属 ...
- PHP event 事件机制
PHP event 事件机制 <?php /* * PHP 事件机制 */ class baseClass{ private $_e; public function __set($name ...
- Java开发WebService的几种方法--转载
webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...
- iScroll.js 用法参考
本文原文链接:http://www.cnblogs.com/duanhuajian/archive/2013/04/08/3008323.html 概要 iScroll 4 这个版本完全重写了iScr ...