icpc2018焦作-I. Distance
第一发又超时了。。。
题目大意:给你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的更多相关文章
- ICPC 2018 焦作区域赛
// 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
随机推荐
- 20165337《网络对抗技术》week1 Exp0 Kali安装
1.下载kali kali官网:https://www.kali.org 在官网中下载,并且在VMvare里打开 2.修改视图 进去之后虚拟机界面很小,需要修改视图来调整 3.网络设置 4.文件夹共享 ...
- 前端使用moment.js 获取当前时间往前的时间
moment().format("YYYY-MM-DD HH:mm:ss"); //当前时间 moment().subtract(, "days").forma ...
- Harbor修改/data目录位置
由于harbor默认数据存储位置在/data目录,且修改配置文件操作较为复杂,故这里使用软连接的方式将/data目录文件夹内容映射到/app目录下. ln -s /app/harbor/data/ d ...
- iOS里的crc校验问题
首先推荐一篇文章,下面的的文章对crc解释的很好: https://blog.csdn.net/liyuanbhu/article/details/7882789 crc校验算法的本质就是对一个2进制 ...
- CentOs7.5安装Redis
前言 虽说现在都是买服务了,可是自己做的小DEMO偶尔也用用Redis,不会装也不行 变更记录 # 19.4.24 起笔 正文 官网 https://redis.io/ 首先去官网找到最新的链接 截 ...
- Python——字符串2.0(实验)
直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改
- Mysql常用30种SQL查询语句优化方法
出处:http://www.antscode.com/article/12deee70111da0c4.html 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使 ...
- 末学者笔记--NFS服务和DHCP服务讲解
NFS服务端概述 一.概念: NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS:NFS允许一个系统在网络上与他 ...
- Linux ☞ Good good study,day day up
一. 修改桌面程序图标 linux的桌面图标都是在/usr/share/applications 目录下的那些 *.desktop文件,修改桌面程序图标就是修改.desktop图标配置文件中Icon的 ...
- Excel—数学函数
ROUND(四舍五入函数)就是说把一个小数点后多位的数四舍五入小数点位数的函数 函数语法:ROUND(哪个数,要四舍五入到小数点后几位) ROUNDUP(保留小数点几位后进位的函数)就是说要保留一个小 ...