高效算法——Most financial institutions 贪心 H

时间:2024-09-28 18:04:44

H - 贪心

Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

高效算法——Most financial institutions  贪心 H

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 ( 1高效算法——Most financial institutions  贪心 Hai高效算法——Most financial institutions  贪心 Hi<tex2html_verbatim_mark> ) during i<tex2html_verbatim_mark> -th minute ( 1高效算法——Most financial institutions  贪心 Hi高效算法——Most financial institutions  贪心 Hn<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 高效算法——Most financial institutions  贪心 Haibi = 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> ( 1高效算法——Most financial institutions  贪心 Hn高效算法——Most financial institutions  贪心 H100 000<tex2html_verbatim_mark> ). The second line of the input contains n<tex2html_verbatim_mark> integer numbers -- ai<tex2html_verbatim_mark> ( 1高效算法——Most financial institutions  贪心 Hai高效算法——Most financial institutions  贪心 Hi<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 ;
}

---恢复内容结束---