UVALive 6275 Joint Venture(查找等值)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题意:

大意:问是否存在两块乐高的长度等于上面给出的长度;

一、二分查找版;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,num[1000047];
int main()
{
	int i, j, x;

	while(scanf("%d",&x)!=EOF)
	{
		memset(num,0,sizeof(num));
		scanf("%d",&n);
		for( i = 1; i <= n; i++)
		{
			scanf("%d",&num[i]);
		}
		sort(num+1,num+n+1);
		int ll = x*10000000, t,a,b;
		int l, r, mid;
		int flag = 0;
		for(i = 1; i <= n; i++)
		{
			l = i+1, r = n, mid = 0;
			t = ll-num[i];
			while(l<=r)
			{
				mid = (l+r)/2;
				if(t > num[mid])
					l = mid+1;
				else if(t < num[mid])
					r = mid-1;
				else
				{
					flag = 1;
					a = num[i];
					b = num[mid];
					break;
				}
			}
			if(flag == 1)
				break;
		}
		if(flag == 0)
			printf("danger\n");
		else
			printf("yes %d %d\n",a,b);
	}
	return 0;
}

二、非二分查找版:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,num[1000047];
int main()
{
	int i, j, x;

	while(scanf("%d",&x)!=EOF)
	{
		memset(num,0,sizeof(num));
		scanf("%d",&n);
		for( i = 1; i <= n; i++)
		{
			scanf("%d",&num[i]);
		}
		sort(num+1,num+n+1);
		int ll = x*10000000, t,a,b;
		int l, r, mid;
		int flag = 0;
		j = n;
        i = 1;
        while(i<j)
        {
            if(num[i] + num[j] == ll)
            {
                flag = 1;
                a = num[i];
                b = num[j];
                break;
            }
            else if(num[i] + num[j] < ll)
                i++;
            else if(num[i] + num[j] > ll)
                j--;
        }
		if(flag == 0)
			printf("danger\n");
		else
			printf("yes %d %d\n",a,b);
	}
	return 0;
}

UVALive 6275 Joint Venture(查找等值),布布扣,bubuko.com

时间: 2024-11-10 01:29:00

UVALive 6275 Joint Venture(查找等值)的相关文章

投资银行学词汇

主页 > 专业术语 A A,B shares A,B???br/> ABS(asset-backed securities) 资产支持证券 accounts receivable 应收账款 acquisition 收购 acting in concert 一致行???br/> active market 活跃市场 additional float 扩股 administration structure 治理结构 ADR(American Depository Receipt??? 美国存

英汉互译教程---生词

------------------1-4------------------ proliferation dominated assemblysuperstructuresuccessivetensionmilitarilyinterdependenceapartheidagonyinterventionsuccessiverespectiveconsiderateexhaustiveindefinitelyorphanedhaulaggressorsdeprivingaffairstabil

Privacy Policy

MINE Privacy Policy This unit values your privacy. You are using our service, we may collect and use your information. We hope that through this "personal information protection statement shows that when you use our service to you, how do we collect,

How to design of Yeezy Boost by Kanye West?

It's been a long journey In January 2014, a video surfaced of Kanye West talking through his second capsule collection with French denim label A.P.C. (It’s since been scrubbed from the Internet.) Standing in a Paris showroom, West spent nearly fiftee

manganese mining quarry machine

Have we missed some critical information from this article? Are you a company, organisation or research group operating in this region and feel you warrant inclusion on this page? Also please feel free to help us keep this page up to date with the la

General Motors China

General Motors Co., one of the world's largest automakers, trace its root back to 1908. The General Motors-China relationship dates back more than nine decades. GM China's vision is together with its partners to be the best automotive group in China.

银行英语词汇

account number 帐号 ATM转账:ATM Transfer  现存: Cash deposit 转存: Transfer deposit depositor 存户 转取:  Transfer withdrawal 现取: Cash withdrawal  短信服务费: Short message fee pay-in slip 存款单 开户机构:城区支行 A/C with Bank: Urban Sub-branch  扣收年费: Annual fee 通兑: Circulate

MCC 和 MNC

http://www.cnblogs.com/inteliot/archive/2012/08/22/2651666.html 常见MCC:代码(MCC)    ISO 3166-1    国家202     GR     希腊204     NL     荷兰206     BE     比利时208     FR     法国212     MC     摩纳哥213     AD     安道尔214     ES     西班牙216     胡     匈牙利218     BA   

Free Man

Three years ago, i joined zhongran company, but now the contract is over, i perhaps stay here or leave .I love my workmate, they are friendly to me, i learned a lot in the work. During the past three years, i am married and had a baby, her name is we