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 ...
随机推荐
- git知识总结-4.git服务器搭建及迁移git仓库
1. 前言 因为手里有一份代码之前是直接从其它git服务器上克隆下来的,现在想自己搭建一个git服务器把这份代码管起来. 2. 搭建git服务器 1.安装git: $ sudo apt-get ins ...
- 单机多es容器服务部署的网络模式
3.1 Bridge模式的拓扑 当Docker server启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上.虚拟网桥的工作方式和物理交换机 ...
- LightOJ 1348 (树链剖分 + 线段树(树状数组))
题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...
- 从头开始学Maven【仓库】
仓库的分类 本地仓库 改setting.xml 文件中的 <localRepository/> 远程仓库 远程仓库的配置 远程仓库的认证 部署至远程仓库 中央仓库 在$M2_HOME/li ...
- SpringMVC-Helloworld 的归纳理解
前面使用SpringMVC写了Helloworld, 发现理解不是很深刻,很多东西只是跟着教学视频敲才会 现在那Helloworld以及一般的SpringMVC归纳一下: SpringMVC入门Hel ...
- 程序员不能忍996了!全民 fuck ,GitHub来说话
前两天有个Github超级火的一个项目,在一小时之内星标上千. https://github.com/997icu/996.ICU 截至目前 这个项目start数量超过63K.Issues5000 ...
- 【Git】git rebase报错new blank line at EOF.处理
执行git rebase报错如下: First, rewinding head to replay your work on top of it... Applying: 本次提交信息 .git/re ...
- centos防火墙控制与转发端口
一.使用防火墙 systemctl控制防火墙 systemctl status/start/stop/restart firewalld 如开启防火墙: $ systemctl start firew ...
- 解决visual studio不能发现单元测试、无法运行单元测试的方法
问题: 在vs2017里新建空的单元测试后,无法运行测试,即右键菜单的“运行测试”和“调试测试” 不能运行,在测试资源管理中也无法列出这个测试. 解决方法: 将测试项目的引用 Microsoft.Vi ...
- Linux下使用http协议下载文件
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...