高效算法——Most financial institutions 贪心 H
H - 贪心
Crawling in process... Crawling failed Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
Most financial institutions had become insolvent during financial crisis and went bankrupt or were bought by larger institutions, usually by banks. By the end of financial crisis of all the financial institutions only two banks still continue to operate. Financial markets had remained closed throughout the crisis and now regulators are gradually opening them. To prevent speculation and to gradually ramp up trading they will initially allow trading in only one financial instrument and the volume of trading will be limited to i<tex2html_verbatim_mark> contracts for i<tex2html_verbatim_mark> -th minute of market operation. Two banks had decided to cooperate with the government to kick-start the market operation. The boards of directors had agreed on trading volume for each minute of this first trading session. One bank will be buying ai<tex2html_verbatim_mark> contracts ( 1aii<tex2html_verbatim_mark> ) during i<tex2html_verbatim_mark> -th minute ( 1in<tex2html_verbatim_mark> ), while the other one will be selling. They do not really care whether to buy or to sell, and the outside observer will only see the volume ai<tex2html_verbatim_mark> of contracts traded per minute. However, they do not want to take any extra risk and want to have no position in the contract by the end of the trading session. Thus, if we define bi = 1<tex2html_verbatim_mark> when the first bank is buying and bi = - 1<tex2html_verbatim_mark> when the second one is buying (and the first one is selling), then the requirement for the trading session is that aibi = 0<tex2html_verbatim_mark> . Your lucky team of three still works in the data center (due to the crisis, banks now share the data center and its personnel) and your task is to find such bi<tex2html_verbatim_mark> or to report that this is impossible.
Input
The input file contains several test cases, each of them as described below. The first line of the input contains the single integer number n<tex2html_verbatim_mark> ( 1n100 000<tex2html_verbatim_mark> ). The second line of the input contains n<tex2html_verbatim_mark> integer numbers -- ai<tex2html_verbatim_mark> ( 1aii<tex2html_verbatim_mark> ).
Output
For each test case, the first line of the output must contain `` Yes'' if the trading session with specified volumes is possible and `` No'' otherwise. In the former option a second line must contain n<tex2html_verbatim_mark> numbers -- bi<tex2html_verbatim_mark> .
Sample Input
4
1 2 3 3
4
1 2 3 4
Sample Output
No
Yes
1 -1 -1 1
解题思路:
这个题目的题思就是输入n个数,判断这n个数是否能分成总和的一半并相等,我们可以定义一个类或者结构体数组存储,并定义两个成员x,y,x用来存放输入的数,y用来记录这个数的位置,因为之后我们要对
x进行排序,再定义一个整形数组,用来记录这个数乘-1还是1,最后要控制好循环条件,当所有数的和为奇数的时候,直接输出No,或者个数为1也直接输出No,否则输出Yes,并输出方案,即b数组
程序代码:
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX=;
long long sum;
int n,i;
int b[MAX];
struct node
{
int x,y; }a[MAX];
bool cmp(node a,node b)
{
return a.x>b.x;
}
bool init()
{
sum=;
for(int i=;i<n;i++)
{
scanf("%d",&a[i].x);
a[i].y=i;
b[i]=-;
sum+=a[i].x;
}
if(sum%) return true;
else return false;
}
void solve()
{
sort(a,a+n,cmp);
long long num=;
for( i=;i<n;i++)
{
if(num+a[i].x<=sum/)
{
num+=a[i].x;
b[a[i].y]=;
if(num==sum/) break;
}
}
}
void print()
{ for( i=;i<n-;i++)
{
printf("%d ",b[i]);
}
printf("%d\n",b[n-]);
}
int main()
{
while( scanf("%d",&n)==)
{
if(init()||n==) printf("No\n");
else
{
printf("Yes\n");
solve();
print();
}
}
return ;
}
---恢复内容结束---
高效算法——Most financial institutions 贪心 H的更多相关文章
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- 集训第四周(高效算法设计)H题 (贪心)
Description Most financial institutions had become insolvent during financial crisis and went bank ...
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- Java 算法(一)贪心算法
Java 算法(一)贪心算法 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 一.贪心算法 什么是贪心算法?是指在对问题进行求 ...
- python常用算法(6)——贪心算法,欧几里得算法
1,贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的的时在某种意义上的局部最优解. 贪心算法并不保证会得到最优解,但 ...
- CVPR2020论文介绍: 3D 目标检测高效算法
CVPR2020论文介绍: 3D 目标检测高效算法 CVPR 2020: Structure Aware Single-Stage 3D Object Detection from Point Clo ...
- 高效算法——G - 贪心
G - 贪心 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Desc ...
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
随机推荐
- GitHub Desktop安装异常解决
为了更好的共同学习,共同进步,哥们推荐我使用GitHub记录自己每天的学习记录,当下很火的提供一个分布式的版本控制系统(Git)服务的网站,GitHub提供GitHub Desktop桌面程序方便协同 ...
- Android 开发笔记——通过 Intent 传递类对象
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...
- Android Studio下添加引用jar文件和so文件
博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 安卓开发中我们常会遇到jar文件和so文件的引用,下面介绍下在as下如何添加使用,这里以百度地图s ...
- ASP.NET网站实现中英文转换(本地化资源)
主要内容: 1. 简单例子 2. 进一步认识Localization 3. 语言转换 4. 解决方案 一. 简单例子 下面通过一个简单的例子来说明利用Localization来实现本地化是那么的简单, ...
- wordpress 当前栏目名,当前栏目的分类名
wordpress在设计主题和做模板时经常会用到调用当前分类栏目名称,常见的有当前栏目页.文章页,详情代码如下: 1.分类名称与链接 <?php the_category(); ?> 2. ...
- 网络编程(学习整理)---1--(Tcp)实现简单的控制台聊天室
1.简单的聊天室(控制台): 功能实现: 客户端和服务端的信息交流: 2.牵扯到的知识点: 这个我大概说一下,详细后面见代码! 1) 网络通讯的三要素 1. IP 2. 端口号. 3. 协议 2) ...
- 大规模字符串检索-压缩trie树
本文使用压缩trie树实现字符串检索的功能.首先将字符串通过编码转化为二进制串,随后将二进制串插入到trie树中,在插入过程中同时实现压缩的功能. 字符编码采用Huffman,但最终测试发现不采用Hu ...
- Problem 1183 - 排列
#include<iostream> #include<vector> #include<algorithm> using namespace std; int c ...
- smarty中判断一个变量是否存在于一个数组中或是否存在于一个字符串中?
smarty支持php的系统函数可以直接使用{if in_array($str, $arr) || strpos($str, $string)} yes {else} no{/if}
- 前端html+css之第十四天
一.HTML 1.HTML是什么? Hypertext Markup Language, 中文也就是超文本链接标示语言. HTML是一套规则,一套浏览器认识的规则. 2.开发者: (1)学习Html规 ...