博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HNOI2002营业额统计
阅读量:6949 次
发布时间:2019-06-27

本文共 3061 字,大约阅读时间需要 10 分钟。

P1287 - 【HNOI 2002 】营业额统计

Description

Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。

Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况:
该天的最小波动值=min{ |该天以前某一天的营业额-该天营业额| }。

当最小波动值越大时,就说明营业情况越不稳定。

而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。
第一天的最小波动值为第一天的营业额。

Input

第一行为正整数n(n<=32767),表示该公司从成立一直到现在的天数,接下来的n行每行有一个正整数ai(ai<=1000000),表示第i天公司的营业额。

Output

仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。

Sample Input

6

5
1
2
5
4
6

Sample Output

12

Hint

结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12

Source

HNOI 2002

排序,STL

致我的第一个SPlay,Roate(),Splay(),Newnode(),Insert(),get_pre,get_next;

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define rep(i,a,b) for(register int i=a;i<=b;i++) 14 #define il inline 15 #define ll long long 16 #define inf 1<<30 17 using namespace std; 18 const int N=32768; 19 int pre[N*4],key[N*4],ch[N*4][2],n,tot,root; 20 int gi(),gl(); 21 void Newnode(int &p,int fa,int k) { 22 p=++tot; 23 pre[p]=fa; 24 key[p]=k; 25 ch[p][1]=ch[p][0]=0; 26 } 27 void Rotate(int x,int kind) { 28 int y=pre[x]; 29 ch[y][!kind]=ch[x][kind];//you can understand it by moni 30 pre[ch[x][kind]]=y; 31 if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x;//// 看y是右儿子还是左儿子 32 pre[x]=pre[y]; 33 pre[y]=x; 34 ch[x][kind]=y; 35 36 } 37 void Splay(int r,int goal) { 38 while(pre[r]!=goal) { 39 if(pre[pre[r]]==goal) Rotate(r,ch[pre[r]][0]==r);// 如果左儿子==r就右旋,kind==1 右旋,kind==0 ,左旋. 40 else { 41 int y=pre[r]; 42 int kind= ch[pre[y]][0]==y;// 三个转,先转父亲.// bug 43 if(ch[y][kind]==r) { 44 Rotate(r,!kind); 45 Rotate(r,kind); 46 } 47 else { 48 Rotate(y,kind); 49 Rotate(r,kind); 50 } 51 } 52 } 53 root = r;// 根 54 } 55 int get_pre(int x) { 56 int tmp=ch[x][0]; 57 if(tmp == 0) return inf; 58 while(ch[tmp][1]) tmp=ch[tmp][1]; 59 return key[tmp]; 60 } 61 int get_next(int x) { 62 int tmp=ch[x][1]; 63 if(tmp == 0) return inf; 64 while(ch[tmp][0]) tmp=ch[tmp][0]; 65 return key[tmp]; 66 } 67 int Insert(int k) { 68 while(ch[root][ k > key[root]]) { 69 if(key[root]==k) { 70 Splay(root,0); 71 return 0; 72 } 73 root = ch[root][ k > key[root]]; 74 } 75 Newnode(ch[root][ k > key[root]],root,k); 76 Splay(ch[root][ k > key[root]],0); 77 return 1; 78 } 79 int main() { 80 freopen("turnover.in","r",stdin); 81 freopen("turnover.out","w",stdout); 82 n=gi();int ans=0;register int k; 83 rep(i,1,n) { 84 85 if(scanf("%d",&k)==EOF) k=0; 86 if(i==1) {ans +=k,Newnode(root,0,k);continue;} 87 if(Insert(k)==0) continue; 88 int a=get_pre(root); 89 int b=get_next(root); 90 ans += min(abs(k-a),abs(b-k));// abs 91 } 92 cout<
'9')&&ch!='-') ch=getchar();100 if(ch=='-') ch=getchar(),f=-1;101 while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();102 return res*f;103 }

 

 

 

转载于:https://www.cnblogs.com/ypz999/p/6667976.html

你可能感兴趣的文章
const使用摘要
查看>>
1.cocos2dx 3.2环境结构
查看>>
你知道什么是Grunt么?
查看>>
Java堆栈详解
查看>>
Hadoop入门进阶课程6--MapReduce应用案例
查看>>
SQL Server 2014里的IO资源调控器
查看>>
.NET足球赛事资料数据库平台SmartLottery开源发布——全球足球联赛应有尽有
查看>>
Eamcs ditaa基于字符图形产生的图像上
查看>>
Only the original thread that created a view hierarchy can touch its views.
查看>>
LeetCode手记-Add Binary
查看>>
对DNSPOD添加域名解析的一些见解
查看>>
vim添加删除多行注释
查看>>
在caffe中增加和convolution相同的层
查看>>
Java设计模式(四) 装饰 代理模式
查看>>
Filter过滤非法字符
查看>>
嵌入式系统烧写uboot/bootloader/kernel的一般方法
查看>>
PyCharm4注册码--软件安装
查看>>
【转】物业管理与移动互联网科技|微信公众平台,物业app,物业O2O
查看>>
patch与diff的恩怨
查看>>
蓝桥杯——先进的多说好树遍历
查看>>