第一发又超时了。。。

题目大意:给你n个点,然后给你n-1的数,表示两两距离,然后让你输出n个答案,第i个答案表示从这n个点里面挑i个点,然后这i个点两两之间会有一个距离,答案要求这些距离和的最大值。

第一次思路:n个点,给你距离,我们可以得到在坐标轴上表示n个点的坐标,然后我们可以先设每个点坐标为xi

可以发现:当n>=2的时候

n=2,i=2 距离为x2-x1

n=3,i=2距离为x3-x1 i=3距离为 x2-x1+x3-x1+x3-x2=2*x3-2*x1

n=5,i=2距离为x5-x1,i=5距离为4*x5-4*x1+2*x4-2*x3

以此类推我发现了对于每种情况下当n固定时比如n是5。i从2开始,i为2答案为(i-1)*两边第一个数之差,i=3时,答案为(i-1)*两边第一个数之差,i为4时,答案为(i-1)*第一个数之差+(i-3)*第二个数之差。至此找到规律。n固定时,第i个答案为(i-1)*两边第一个数之差+(i-3)*两边第二个数之差+(i-5)*两边第三个数之差......

这样一直加到i-x这个数<=0停止。于是就敲出了代码但超时了。。。

思路重新梳理,可以发现对于每种i之间是有联系的,对于每个i是前一个i的答案的基础上多加了两边之差的前(i/2)项的前缀和。

表述难以表达清楚,还是得好好推推想想。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
long long a[maxn];
long long c[maxn];
long long sum[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
memset(c,,sizeof(c));
a[]=;
for(int i=; i<=n-; i++)
{
int d;
cin>>d;
a[i+]=a[i]+d;
}
for(int i=; i<=n/; i++)
c[i]=a[n+-i]-a[i];
for(int i=; i<=n/; i++)
sum[i]=sum[i-]+c[i];
printf("");
long long tt=;
int cnt=;
for(int i=; i<=n; i++)
{
tt=tt+sum[i/];
printf(" %lld",tt);
}
printf("\n");
} }

icpc2018焦作-I. Distance的更多相关文章

  1. ICPC 2018 焦作区域赛

    // 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...

  2. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  4. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  5. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  6. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  7. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  8. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  9. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

随机推荐

  1. 使用nc传输文件和目录【转】

    方法1,传输文件演示(先启动接收命令) 使用nc传输文件还是比较方便的,因为不用scp和rsync那种输入密码的操作了 把A机器上的一个rpm文件发送到B机器上 需注意操作次序,receiver先侦听 ...

  2. 通过 iis或者本地IP 调试代码

    首先说下这个操作的意义,做微信开发每次需要将代码部署后才能调试.现在设置了Nginx服务器,生产环境可以指向正式服务器地址,调试时可以将Nginx指向自己的PC,但是vs调试启动的默认地址是[loca ...

  3. Python Django-入门到进阶

    web应用 Python-web应用 +HTTP协议 +web框架 第二篇:Djangon简介 Diango 框架起步 Python-Django基础 第三篇:路由控制 Python-Django 路 ...

  4. JAVA 封装的简单运用

    package Code425;class person{ private String name ; String place; String school; String habits; int ...

  5. Unity中各种格式计时器

    问题背景: 在开发游戏过程中,很多地方需要倒计时,但是各种地方要的倒计时格式不同,倒计时都会写,在这里不详细介绍,写的目的就是为了记录一下,方便复用(为了在开发过程中不为了小问题浪费不必要脑细胞) 1 ...

  6. selenium+python编写自动化脚本时,定位frame中对象操作

    在web应用中经常会出现frame嵌套的应用,假设页面上有A,B两个frame,其中B在A内,那么定位B中的内容则需要先到A,再到B.switchTo().frame方法可以把当前定位的主题切换到fr ...

  7. 将Go的main包拆分为多个文件

    将Go的main包拆分为多个文件的写法和普通包是完全一致的,其使用规则也相同.如编写main包结构如下: main |----main.go |----show.go 在main.go中编写了main ...

  8. CAS5.3.0安装部署

    部署环境:JDK1.8.x maven-3.5.2 tomcat-8.x.x 1.下载地址 https://github.com/apereo/cas-overlay-template/tree/5. ...

  9. HashMap 学习 (JDK8)

    1.hashmap中hash函数的实现中,异或运算操作的结果是什么,为什么要做这样的异或运算 static final int hash(Object key) { int h; return (ke ...

  10. tensorflow用dropout解决over fitting-【老鱼学tensorflow】

    在机器学习中可能会存在过拟合的问题,表现为在训练集上表现很好,但在测试集中表现不如训练集中的那么好. 图中黑色曲线是正常模型,绿色曲线就是overfitting模型.尽管绿色曲线很精确的区分了所有的训 ...