zoukankan      html  css  js  c++  java
  • UVA1614(贪心)

    Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu

    []  [Go Back]  [Status]  

    Description

    Download as PDF 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 contracts for i -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 contracts ( 1$ le$ai$ le$i ) during i -th minute ( 1$ le$i$ le$n ), 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 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 when the first bank is buying and bi = - 1 when the second one is buying (and the first one is selling), then the requirement for the trading session is that $ sum_{{i=1}}^{{n}}$aibi = 0 . 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 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 ( 1$ le$n$ le$100 000 ). The second line of the input contains n integer numbers -- ai ( 1$ le$ai$ le$i ).

    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 numbers -- bi .

    Sample Input 

    4
    1 2 3 3
    4
    1 2 3 4
    

    Sample Output 

    No
    Yes
    1 -1 -1 1
    

    Source


    Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 8. Algorithm Design :: Exercises

    []  [Go Back]  [Status]  


    排序之后贪心瞎搞。。。


    /*************************************************************************
        > File Name: c.cpp
        > Author: acvcla
        > QQ: 
        > Mail: acvcla@gmail.com 
        > Created Time: 2014年10月11日 星期六 08时42分28秒
     ************************************************************************/
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    #include<set>
    #include<math.h>
    using namespace std;
    typedef long long LL;
    const int maxn = 1e5 + 10;
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define pb push_back
    int A[maxn],cnt[maxn],f[maxn];
    std::vector<int> v;
    int main(){
    	int n;
    	while(~scanf("%d",&n)&&n){
    		memset(cnt,0,sizeof(cnt));
    		memset(f,0,sizeof f);
    		v.clear();
    		LL sum=0;
    		LL M=0;
    		for(int i=1;i<=n;i++){
    			scanf("%d",A+i);
    			if(A[i]>M)M=A[i];
    			sum+=A[i];
    			cnt[A[i]]++;
    		}
    		if(sum&1){
    			puts("No");
    			continue;
    		}
    		sum/=2;
    		bool ok=false;
    		for(int i=M;!ok&&i>=1;i--){
    			f[i]=min((LL)cnt[i],sum/i);
    			sum-=(LL)f[i]*i;
    			if(sum==0){
    				ok=true;
    				break;
    			}
    		}
    		if(!ok){
    			puts("No");
    		}else{
    			puts("Yes");
    			for(int i=1;i<=n;i++){
    				if(i==1){
    					if(f[A[i]]>0){
    						printf("1");
    						f[A[i]]--;
    					}
    					else printf("-1");
    				}
    				else{
    					if(f[A[i]]>0){
    						printf(" 1");
    						f[A[i]]--;
    					}
    					else printf(" -1");
    				}
    			}
    			puts("");
    		}
    
    	}
    	return 0;
    }


  • 相关阅读:
    SQL将表中某一类型的一列拼接成一行
    javascript中把一个数组的内容全部赋值给另外一个数组
    Socket接口原理及用C#语言实现
    Linq表连接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)
    C#中对DataTable进行全连接后group by,orderby
    循环删除DataTable.Row中的多行问题
    跨库连接报错Server 'myLinkedServer' is not configured for RPC
    执行远程存储过程并插入到临时表中
    无法定位程序输入点_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4findEPKcj于动态链接库上
    Qt 日期时间
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7294739.html
Copyright © 2011-2022 走看看