题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816

2013长春区域赛的D题。

很简单的几何题,就是给了一条折线。 然后一个矩形窗去截取一部分,求最大面积。

现场跪在这题,最后时刻TLE到死,用的每一小段去三分,时间复杂度是O(n log n) , 感觉数据也不至于超时。

卧槽!!!!代码拷回来,今天在HDU一交,一模一样的代码AC了,加输入外挂6s多,不加也8s多,都可AC,呵呵·····(估计HDU时限放宽了!!!)

现场赛卡三分太SXBK了,我艹!!!! 好好的一个现场赛绝杀错过了。

以下是现场赛源码,在HDU顺利AC! 现场TLE到死!

其实O(n)也可以搞,因为是三分的是一个二次函数,求对称轴就可以了。现场没有想到这个优化,等下简单修改成O(n)代码。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = ;
int x[MAXN],y[MAXN];
int d;
int n;
int L;
int nowx ;
int nextx;
int r1,l2;
const double eps = 1e-;
double solve()
{
double left = nowx,right = nextx;
double ret1,ret2;
for(int cc = ;cc <= ;cc++)
{
double mid = (left + right)/;
double midmid = (mid + right)/;
double h1 = y[r1] + (double)(y[r1-] - y[r1]) * (mid - x[r1])/(x[r1-] - x[r1]);
double h2 = y[l2] + (double)(y[l2+] - y[l2])*(mid + *d - x[l2])/(x[l2 + ] - x[l2]);
ret1 = (double)(x[r1] - mid)*(h1 + y[r1])/ + (double)(mid + *d - x[l2])*(h2 + y[l2])/; h1 = y[r1] + (double)(y[r1-] - y[r1]) * (midmid - x[r1])/(x[r1-] - x[r1]);
h2 = y[l2] + (double)(y[l2+] - y[l2])*(midmid + *d - x[l2])/(x[l2 + ] - x[l2]);
ret2 = (double)(x[r1] - midmid)*(h1 + y[r1])/ + (double)(midmid + *d - x[l2])*(h2 + y[l2])/;
if(ret1 < ret2)
left = mid+eps;
else right = midmid-eps;
}
return ret1;
} int input()
{
char ch;
ch = getchar();
while(ch < '' || ch >'')
{
ch = getchar();
}
int ret = ;
while(ch >= '' && ch <= '')
{
ret *= ;
ret += ch -'';
ch = getchar();
}
return ret;
} int main()
{
//freopen("D.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&L);
for(int i = ;i <= n;i++)
{
//x[i] = input();
//y[i] = input();
scanf("%d%d",&x[i],&y[i]);
}
//scanf("%d%d",&x[i],&y[i]);
scanf("%d",&d);
double ans = ;
r1 = ;
l2 = ;
double tmp = ;
while(l2 < n && x[l2+] < *d)l2++;
for(int i = r1;i < l2;i++)
{
tmp += (double)(x[i+] - x[i])*(y[i] + y[i+])/;
}
if(l2 == )
{
tmp -= (double)(x[] - x[])*(y[] + y[])/;
}
x[n+] = x[n];
y[n+] = y[n];
nowx = ;
//printf("%d %d\n",r1,l2);
while(l2 < n && r1 <= n)
{
int p1 = x[r1];
int p2 = x[l2 + ] - *d;
if(p1 < p2)
nextx = p1;
else nextx = p2;
nextx = min(L- *d,nextx);
//printf("%d %d\n",nowx,nextx);
ans = max(ans,tmp + solve());
if(p1 < p2)
{
nowx = p1;
if(r1 < n)tmp -= (double)(x[r1+] - x[r1])*(y[r1+] + y[r1] )/;
r1++;
}
else
{
nowx = p2;
tmp += (double)(x[l2+] - x[l2])*(y[l2+] + y[l2])/;
l2++;
}
}
printf("%.3lf\n",ans//d);
}
return ;
}

改成了O(n)的解法。

因为三分的那个函数是二次函数。

找最大值,只要找两个端点和对称轴处就足够了!

还是太弱,现场没有想到这个优化,改起来很容易的。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = ;
int x[MAXN],y[MAXN];
int d;
int n;
int L;
int nowx ;
int nextx;
int r1,l2;
const double eps = 1e-;
double solve()
{
double left = nowx,right = nextx;
//求出二次函数的a,b,c系数
double tmp11 = x[r1];
double tmp12 = -;
double tmp13 = (double)y[r1] - (double)x[r1]*(y[r1-] - y[r1])/(x[r1-] - x[r1])/;
double tmp14 = (double)(y[r1-] - y[r1])/(x[r1-] - x[r1])/;
double tmp21 = *d - x[l2];
double tmp22 = ;
double tmp23 = y[l2] + (double)(*d - x[l2])*(y[l2+] - y[l2])/(x[l2+] - x[l2])/;
double tmp24 = (double)(y[l2+] - y[l2])/(x[l2+] - x[l2])/;
//函数Y = (tmp11 + tmp12 * x)*(tmp13 + tmp14 * x) + (tmp21 + tmp22 * x)*(tmp23 + tmp24*x)
double a = tmp12 * tmp14 + tmp22 * tmp24;
double b = tmp11 * tmp14 + tmp12 * tmp13 + tmp21 * tmp24 + tmp22 * tmp23;
double c = tmp11 * tmp13 + tmp21 * tmp23; double x0 = -b /(*a);//对称轴
double ret = max(a*left*left + b*left + c,a*right *right + b * right + c);
if(x0 >= left && x0 <= right)
ret = max(ret,a * x0 * x0 + b*x0 + c);
return ret; /*
double ret1,ret2;
for(int cc = 0;cc <= 30;cc++)
{
double mid = (left + right)/2;
double midmid = (mid + right)/2;
double h1 = y[r1] + (double)(y[r1-1] - y[r1]) * (mid - x[r1])/(x[r1-1] - x[r1]);
double h2 = y[l2] + (double)(y[l2+1] - y[l2])*(mid + 2*d - x[l2])/(x[l2 + 1] - x[l2]);
ret1 = (double)(x[r1] - mid)*(h1 + y[r1])/2 + (double)(mid + 2*d - x[l2])*(h2 + y[l2])/2; h1 = y[r1] + (double)(y[r1-1] - y[r1]) * (midmid - x[r1])/(x[r1-1] - x[r1]);
h2 = y[l2] + (double)(y[l2+1] - y[l2])*(midmid + 2*d - x[l2])/(x[l2 + 1] - x[l2]);
ret2 = (double)(x[r1] - midmid)*(h1 + y[r1])/2 + (double)(midmid + 2*d - x[l2])*(h2 + y[l2])/2;
if(ret1 < ret2)
left = mid+eps;
else right = midmid-eps;
}
return ret1;
*/
} int input()
{
char ch;
ch = getchar();
while(ch < '' || ch >'')
{
ch = getchar();
}
int ret = ;
while(ch >= '' && ch <= '')
{
ret *= ;
ret += ch -'';
ch = getchar();
}
return ret;
} int main()
{
//freopen("D.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&L);
for(int i = ;i <= n;i++)
{
//x[i] = input();
//y[i] = input();
scanf("%d%d",&x[i],&y[i]);
}
//scanf("%d%d",&x[i],&y[i]);
scanf("%d",&d);
double ans = ;
r1 = ;
l2 = ;
double tmp = ;
while(l2 < n && x[l2+] < *d)l2++;
for(int i = r1;i < l2;i++)
{
tmp += (double)(x[i+] - x[i])*(y[i] + y[i+])/;
}
if(l2 == )
{
tmp -= (double)(x[] - x[])*(y[] + y[])/;
}
x[n+] = x[n];
y[n+] = y[n];
nowx = ;
//printf("%d %d\n",r1,l2);
while(l2 < n && r1 <= n)
{
int p1 = x[r1];
int p2 = x[l2 + ] - *d;
if(p1 < p2)
nextx = p1;
else nextx = p2;
nextx = min(L- *d,nextx);
//printf("%d %d\n",nowx,nextx);
ans = max(ans,tmp + solve());
if(p1 < p2)
{
nowx = p1;
if(r1 < n)tmp -= (double)(x[r1+] - x[r1])*(y[r1+] + y[r1] )/;
r1++;
}
else
{
nowx = p2;
tmp += (double)(x[l2+] - x[l2])*(y[l2+] + y[l2])/;
l2++;
}
}
printf("%.3lf\n",ans//d);
}
return ;
}

HDU 4816 Bathysphere (2013长春现场赛D题)的更多相关文章

  1. hdu 4813(2013长春现场赛A题)

    把一个字符串分成N个字符串 每个字符串长度为m Sample Input12 5 // n mklmbbileay Sample Outputklmbbileay # include <iost ...

  2. HDU 4821 String(2013长春现场赛I题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...

  3. HDU 4818 Golden Radio Base (2013长春现场赛B题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 进制转换. 现场根据题目给的两个公式,不断更新!!! 胡搞就可以了. 现场3A,我艹,一次循环开 ...

  4. HDU 4815 Little Tiger vs. Deep Monkey(2013长春现场赛C题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 简单的DP题. #include <stdio.h> #include <st ...

  5. HDU 4815 Little Tiger vs. Deep Monkey 2013 长春现场赛C题

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 [题意] n个题目,每题有各自的分数,A有50%的概率答对一道题目得到相应分数,B想要在至少P的概率 ...

  6. HDU 4764 Stone (2013长春网络赛,水博弈)

    Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. 【转】如何评价 Apple 新推出的编程语言 Swift?

    如何评价 Apple 新推出的编程语言 Swift? 原文地址:http://www.zhihu.com/question/24002984 评价:如果你会Objective-C,你不需要去看它.   ...

  2. sequelize初使用

    官网地址:Sequelize Sequelize is a promise-based ORM for Node.js v4 and up. It supports the dialects Post ...

  3. Centos6.5下升级Python版本

    Cenos6.5升级Python2.6到2.7 1.下载源码包 wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz 2.进行 ...

  4. LOJ 2249: 洛谷 P2305: 「NOI2014」购票

    题目传送门:LOJ #2249. 题意简述: 有一棵以 \(1\) 号节点为根节点的带边权的树. 除了 \(1\) 号节点的所有节点上都有人需要坐车到达 \(1\) 号节点. 除了 \(1\) 号节点 ...

  5. JDK1.8源码hashMap

    一.概念 允许key为null,value为null:线程不安全:不保证映射的顺序:迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数) ...

  6. Windows命令-系统木马取样

    1.前言 工作中偶尔会遇到去现场提取木马样本回公司分析的情况.如果是生产环境下,不方便安装各类抓包.安全软件时.能用系统自带的命令去定位出木马程序相关的信息是最理想不过的状态. 2.Windows常用 ...

  7. artDialog4.1.7 摘自网络

    jquery.artDialog.source.js学习 1 关键的对象关系 art = jQuery = $ function artDialog() {...} artDialog.fn = ar ...

  8. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、

    1.Request.ApplicationPath->当前应用的目录    Jsp中, ApplicationPath指的是当前的application(应用程序)的目录,ASP.NET中也是这 ...

  9. [转] caffe视觉层Vision Layers 及参数

    视觉层包括Convolution, Pooling, Local Response Normalization (LRN), im2col等层. 1.Convolution层: 就是卷积层,是卷积神经 ...

  10. LeetCode(16):最接近的三数之和

    Medium! 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...