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 ...
随机推荐
- 软件测试自动化…python学习到什么程度?代码好不好学!
软件测试自动化…python学习到什么程度?代码好不好学! 如下:
- Python自动化中的键盘事件
1) from selenium import webdriver 加载模块 2) b = webdriver.Friefox() 打开浏览器 3) b.get(‘http://xxxxxxx’) 打 ...
- LeetCode第二十一题-对两个有序链表排序
Merge Two Sorted Lists 问题简介:合并两个已排序的链表并将其作为新链表返回 举例: 输入: 1->3->5, 1->2->4 输出: 1->1-&g ...
- HDOJ 6508 Problem I. Spell Boost (01背包/DP)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6508 题目: Problem Description Shadowverse is a funny car ...
- 7个提升Python程序性能的好习惯
原文作者:爱coding,会编程的核电工程师. 个人博客地址:zhihu.com/people/zhong-yun-75-63 掌握一些技巧,可尽量提高Python程序性能,也可以避免不必要的资源浪费 ...
- kubenetes_V1.14.0 安装部署
k8s的安装有多种方式,如yum安装,kubeadm安装,kubemini安装,二进制安装(生产环境多采用此方式精确控制安装)等.本文是入门系列验证,之前进行过yum安装,可以查看文章<k8s入 ...
- memcached单机或热备的安装部署
一.部署准备 1.安装Java 不建议使用系统默认Open JDK版本,需要手工另行安装.JDK版本建议为1.7+,若Java已安装完毕,则无需重复安装. 安装过程如下: (1)获取JDK安装包: ( ...
- Helloworld——SpringMVC
搭建环境:eclipse 这里需要配置Server runtime environment——Apache Tomcat 到官网下载 解压 在eclipse中: Window perferences ...
- 个人NABCD
采用NABCD模型对我们的团队项目大学生失物招领平台进行了详细的需求分析说明,其中N指(Need需求),A (Approach 做法),B (Benefit好处),C (Competitors 竞争) ...
- matlab 写文件
fid = fopen('data.txt','w');for oo=1:1:i if mod(oo,10) == 0 fprintf(fid,'%f,%f,\n',sI1(oo),sQ1(oo)); ...