HDU3434 Sequence Adjustment
题意:给你含有n个数的序列,每次你可以选一个子序列将上面所有的数字加1或者减1,目标是把所有数字变成相同的,问最少步数,和那个相同的数字有多少种可能。
将原序列转化为差分序列,即a[2] - a[1], a[3] -a[2]……, a[n] - a[n -1]
将原序列l,到r增加1,相当于差分序列l处加1,r + 1减1,讲从l处到尾加1,相当于差分序列l处加1
减法与此类似
故将差分序列中元素正负可以相消,总的次数为正数和与负数和绝对值的最大值。
最终变换后的序列值可取a[1]到a[n]的每个数(还没想到怎么证明)
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 1000008, INF = 0x3F3F3F3F;
int a[N];
int main(){
int t;
cin>>t;
for(int cas = 1; cas <= t; cas++){
int n;
scanf("%d", &n);
LL s1 = 0, s2 = 0;
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
if(i){
if(a[i] > a[i - 1]){
s1 += a[i] - a[i - 1];
}else{
s2 += a[i - 1] - a[i];
}
}
}
printf("Case %d: %I64d %I64d\n", cas, max(s1, s2), abs((LL)a[0] - a[n - 1]) + 1ll);
} return 0;
}
HDU3434 Sequence Adjustment的更多相关文章
- [C7] Andrew Ng - Sequence Models
About this Course This course will teach you how to build models for natural language, audio, and ot ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 亲自动手用HTK实现YES NO孤立词识别
很久以前的发在研学论坛的帖子了,再重新整理了一下,希望对新手有用. 完整版链接:http://yun.baidu.com/s/1hapcE 第一步 创建语音文件 录音 命令:HSLab any_nam ...
- ubuntu14.04 archive sources.list
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive. ...
- SQL查询表中的有那些索引
方法1. 使用系统表 -- 查询一个表中的索引及索引列 USE AdventureWorks2008 GO SELECT indexname = a.name , tablename = c. n ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- C#跨线程操作控件
1.首先通过按键创建子线程: 创建子线程,子线程调用changeText方法. private void btnOK_Click(object sender, EventArgs e) { Threa ...
- PowerDesigner16.5 连64位MySQL,出错:SQLSTATE = IM014。原因及解决方案
- svn上传报Authorization failed错误解决办法
svn上传文件时没有弹出用户登录界面,而是直接报Authorization failed错误.出现该问题基本都是三个配置文件的问题,下面把这个文件列出来 svnserve.conf配置文件中的 [ge ...
- 使用swift 中的注意,不断完善中
1. 应该充分利用swfit的新特性 比如如果按照oc里的习惯,调用一个delegate中都optional函数应该这样写 if delegate != nil && delegate ...
- winrt反射
第一步引用扩展类. using System.Reflection.IntrospectionExtensions; 第二步反射. gridView是我定义的GridView控件.ItemClick是 ...
- codeforces 501C. Misha and Forest 解题报告
题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...