2018百度之星初赛(A) 1003、1002、1001

1001.度度熊的拼三角形

Problem Description

度度熊有 N 根木棒,每根木棒的长度为ai。

现在要挑选其中的三根,问能拼出的三角形的最大周长是多少。

如果不能拼成任何一个三角形,输出 −1。

Input

多组数据(不超过10组),读到EOF结束。

对于每一组数据:

第一行一个数 N 表示木棒数量。

第二行一共 N 个数,描述每一根木棒的长度。

1≤N≤1000

木棒长度都是不超过100000的正整数

Output

对于每一组数据,输出一个数表示答案。

Sample Input

3

1 1 100

7

1 9 9 90 2 2 4

Sample Output

-1

22

思路:签到题,直接上。

代码如下:

#include<cstdio>
#include<cstdlib>
#include<string>
#include<string.h>
#include <algorithm>
using namespace std;
const int MAX_N=10000;
int main()
{
    int n;
    bool isOk=false;
    int a[MAX_N];
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
        sort(a,a+n);      //对数组指定范围进行升序排序
        for(int i=n-1;i>1;i--)
        {
            if(a[i]<a[i-1]+a[i-2])
            {
                isOk=true;
                printf("%d\n",a[i]+a[i-1]+a[i-2]);
                break;
            }
        } 
        if(!isOk)
        printf("-1\n"); 
    }
    return 0;
}

1002.度度熊学队列

度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣。

初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作。

①1 u w val 在编号为 u 的队列里加入一个权值为 val 的元素。(w=0 表示加在最前面,w=1 表示加在最后面)。

②2 u w 询问编号为 u 的队列里的某个元素并删除它。( w=0 表示询问并操作最前面的元素,w=1 表示最后面)

③3 u v w 把编号为 v 的队列“接在”编号为 u 的队列的最后面。w=0 表示顺序接(队列 v 的开头和队列 u 的结尾连在一起,队列v 的结尾作为新队列的结尾), w=1 表示逆序接(先将队列 v 翻转,再顺序接在队列 u 后面)。且该操作完成后,队列 v 被清空。

Input

有多组数据。
对于每一组数据,第一行读入两个数 N 和 Q 。
接下来有 Q 行,每行 3 ~4 个数,意义如上。
N≤150000,Q≤400000
1≤u,v≤N,0≤w≤1,1≤val≤100000
所有数据里 Q 的和不超过500000

Output

对于每组数据的每一个操作②,输出一行表示答案。

注意,如果操作②的队列是空的,就输出−1 且不执行删除操作。

Sample Input

2 10

1 1 1 23

1 1 0 233

2 1 1

1 2 1 2333

1 2 1 23333

3 1 2 1

2 2 0

2 1 1

2 1 0

2 1 1

Sample Output

23

-1

2333

233

23333

思路:模拟,好像可以用双端队列,也能用list,需要注意不能把队列定义在外面,会超时。不知道为什么,大佬说

deque<int> * a = new deque<int>[n+1];这样就不会超时,原理暂时不清楚,我使用的是list,这个容器感觉和这个题完搭,只是需要区分splice与merge的区别。

代码如下:

#include<cstdio>
#include<cstdlib>
#include<string>
#include<string.h>
#include<algorithm>
#include<list>
#include<iostream> 
using namespace std;
int main()
{
    int n,t,i,j,a,u,v,w;
    while(~scanf("%d%d",&n,&t))
    {
        list<int>q[n+1];
        list<int>::iterator iter;
        while(t--)
        {
    	    scanf("%d%d%d",&a,&u,&v);
    	    if(a!=2)
    	    {
    		    scanf("%d",&w);
		    }
		    if(a==1)
		    {
		    	if(v==0) q[u].push_front(w);
		    	if(v==1) q[u].push_back(w);
			}
			if(a==2)
			{
				if(v==0)
				{
					if(!q[u].empty())
					{
					   printf("%d\n",q[u].front());
					   q[u].pop_front();
				    }
					else printf("-1\n");
				}
				if(v==1)
				{
					if(!q[u].empty())
					{
					   printf("%d\n",q[u].back());
					   q[u].pop_back();
				    }
					else printf("-1\n");
				}
			}
			if(a==3)
			{
				iter=q[u].end();
				if(w==0)
				{
					q[u].splice(iter,q[v]);
					q[v].clear();
				}
				if(w==1)
				{
					q[v].reverse();
				    q[u].splice(iter,q[v]);
					q[v].clear();
				}
			}
	    }
	}
	return 0;
}

1003.度度熊剪纸条

度度熊有一张纸条和一把剪刀。

纸条上依次写着 N 个数字,数字只可能是 0 或者 1。

度度熊想在纸条上剪 K 刀(每一刀只能剪在数字和数字之间),这样就形成了 K+1 段。

他再把这 K+1 段按一定的顺序重新拼起来。

不同的剪和接的方案,可能会得到不同的结果。

度度熊好奇的是,前缀 1 的数量最多能是多少。

Input

有多组数据,读到EOF结束。

对于每一组数据,第一行读入两个数 N 和 K 。

第二行有一个长度为 N 的字符串,依次表示初始时纸条上的 N 个数。

0≤K<N≤10000

所有数据 N 的总和不超过100000

Output

对于每一组数据,输出一个数,表示可能的最大前缀 1 的数量

Sample Input

5 1

11010

5 2

11010

Sample Output

2

3

思路:我是参照大佬的做法的,思路方面有点绕,详情:https://blog.csdn.net/King8611/article/details/81588970

大佬用java写的,我把代码改成c++了(时间从400+ms变31ms。。。。。) 

附上题解:

代码如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
using namespace __gnu_cxx;
int p[20000];
int main()
{
	int a,b,i,j;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		memset(p,0,sizeof(p));
		string s;
		cin>>s;
		int l=s.length(),len=0;
		int fag=0;
		if(s[0]=='0')
		{
			len++;
		}
		per(i,0,l-1)
		{
			if(s[i]=='1')
			{
				fag=1;p[len]++;
			}
			else if(s[i]=='0'&&fag==1)
			{
				fag=0;len++;
			}
		}
		if(len==0||b==0)
		{
			printf("%d\n",p[0]);
			continue;
		}
		len++;
		int t=min((b-1)/2,len-2);
		sort(p+1,p+len-1);
		int sum=0;
		per(i,1,t)
		{
			sum+=p[len-1-i];
		}
		if(len-t>2)
		{
			if(b%2==0)
			{
				sum+=p[0]+p[len-1]+p[len-2-t];
				sum-=min(p[0],min(p[len-2-t],p[len-1]));
			}
			else
			{
				sum+=max(p[0]+p[len-1],p[len-t-2]);
			}
		}
		else
		{
			sum+=p[0];
			if(b>=1)
			{
				sum+=p[len-1];
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/81605103