Codeforces Round #159 (Div. 2) D sum
来源:互联网 发布:腾讯软件大全 编辑:程序博客网 时间:2024/04/25 22:42
Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai ≤ ai + 1 ≤ 2·ai for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an(0 ≤ ai ≤ 109) — the original array.
It is guaranteed that the condition ai ≤ ai + 1 ≤ 2·ai fulfills for any positive integer i (i < n).
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a1. If there are multiple solutions, you are allowed to print any of them.
41 2 3 5
+++-
33 3 5
++-
设数据a1,a2,a3......a(n - 2),a(n - 1),a(n);因为数据间满足a(i - 1) <= a(i) <= 2 * a (i - 1);所以a(n) 是最大的;所以从a(n)开始减;因为a(n - 1) <= a(n) <= 2 * a (n - 1);所以设k = a(n) - a (n - 1);0 <= k <= a(n - 1);因为 a(n - 2) <= a(n - 1) <= 2 * a (n - 2);且k > 00 <= k <= 2 * a(n - 2);所以 k = k - a(n - 2);所以 -a(n - 2) <= k <= a(n - 2);所以 0 <= abs (k) <= a(n - 2);所以当n > 2 时 abs(k)一定在0到 a(n - 2)之间;当K < 0 时, -k就大于0了;在判断n == 2,1也符合;所以做法很简单;代码:
#include <stdio.h>__int64 num[100100];char ch[100100];int main (){int n,i,j,k;scanf ("%d",&n);for (i = 0 ; i < n ; i ++)scanf ("%I64d",&num[i]);k = num[n - 1];//k 就是 sum; ch[n - 1] = '+';for (i = n - 2 ; i >= 0 ; i --){if (k >= 0){ch[i] = '-';k -= num[i];}else{ch[i] = '+';k += num[i];}}if (k < 0) //当K 小于0 所有符合都要相反,k就边正了 {for (i = 0 ; i < n ; i ++)printf ("%c",ch[i] == '+' ? '-' : '+');printf ("\n");}elseprintf ("%s\n",ch);}
- Codeforces Round #159 (Div. 2) D sum
- Codeforces Beta Round #67 (Div. 2) D. Big Maximum Sum
- Codeforces Round #365 (Div. 2)D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2)D - Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) [D] Mishka and Interesting sum
- Codeforces Round #365 (Div. 2)D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(离线树状数组)
- Codeforces Round #365 (Div. 2) Problem D.Mishka and Interesting sum 解题报告
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(离线线段树)
- Codeforces Round #365 (Div. 2) D Mishka and Interesting sum (离线树状数组)
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(树状数组)
- HDU-4825 Xor Sum &&Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (Trie树)
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(精简的代码)
- Codeforces Round #103 (Div. 2) D
- Codeforces Round #104 (Div. 2) D
- PHP QQ 登录接口应用
- 慎用进程间条件变量pthread_cond_t
- FTP上传常见错误详解
- ANT学习1
- 启动mac 的apach
- Codeforces Round #159 (Div. 2) D sum
- WIN7 VMware Ubuntu虚拟机连接secureCRT及UltraEdit ftp传输文件
- PowerDesigner(PowerDesigner15.1.0.2850)下载、安装以及破解
- C#使用BinaryReader类读取二进制文件
- 在js中去获得内联和连接和行内的方式 各个浏览器兼容
- Qt中向导类 QWizard 修改 next finish 按钮事件方法
- S3C2440对Nand Flash操作和电路原理(基于K9F2G08U0A)
- BCLOJ-1334:元素插入
- Android:数据存储方式的比较