第一发又超时了。。。

题目大意:给你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. edx的ST

    卷积概率论 顺便了解了一下卷积函数 反正以后学CNN还要再看 这个mit老师水平真的不太行 倒是以这个为源头找到不少好的youtube视频 看看她题目出的怎么样吧... cousera今天估计没时间了

  2. 2018-2019 网络对抗技术 20165231 Exp5 MSF基础应用

    实践内容(3.5分) 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践(1分) ms08_067; (失败) MS17-010永 ...

  3. 网站robots.txt & sitemap.xml

    1. 如何查看网站的robots.txt 网址/robots.txt, 比如小米  https://www.mi.com/robots.txt sitemap.xml

  4. Font Awesome,一套绝佳的图标字体库和CSS框架

    http://fontawesome.dashgame.com/ http://www.runoob.com/font-awesome/fontawesome-tutorial.html Font A ...

  5. IntelliJ IDEA安装ideaIU-2019.1

  6. trie字典树:初学

    应用: 1.前缀问题 2.异或问题(转化为前缀问题) 3.查询问题 思想: 将要进行匹配的字符串化为一颗树 字符为边,在结束位置统计该串的全部信息 操作:插入,查询,删除.etc ac: #inclu ...

  7. 饮冰三年-人工智能-Python-25 Django admin

    简介:一个关于后台数据库管理的工具 1:创建一个新的项目 2:设置models,并通过命令生成数据库表 from django.db import models class Book(models.M ...

  8. 关于haproxy

    高性能负载均衡软件 haproxy 一.四层和七层负载均衡的区别: 所谓的四层就是OSI参考模型中的第四层,四层负载均衡也称为四层交换机,他主要是通过分析IP层及TCP/UDP层的流量实现的基于IP加 ...

  9. Celery初识及简单实例

    Celery是一个“自带电池”的任务队列.易于使用,可以轻易入门,它遵照最佳实践设计,使产品可以扩展,或与其他语言集成,并且它自带了在生产环境中运行这样一个系统所需的工具和支持.本文介绍基础部分: 选 ...

  10. 杂记:腾讯暑期实习 Web 后端开发面试经历

    今天面试(一面)腾讯暑期实习 Web 后端开发,一言难尽. 第一部分,常规的自我介绍. 介绍完,面试官问我对人工智能有什么理解?深度学习和机器学习的区别?对调参有什么见解?语音识别中怎样运用了机器学习 ...