zoukankan      html  css  js  c++  java
  • 2018蓝桥编程题6-9+ 50%的10

    6.给定三个整数数组
    A = [A1, A2, ... AN], 
    B = [B1, B2, ... BN], 
    C = [C1, C2, ... CN],
    请你统计有多少个三元组(i, j, k) 满足:
    1. 1 <= i, j, k <= N  
    2. Ai < Bj < Ck  

    【输入格式】 
    第一行包含一个整数N。
    第二行包含N个整数A1, A2, ... AN。
    第三行包含N个整数B1, B2, ... BN。
    第四行包含N个整数C1, C2, ... CN。

    对于30%的数据,1 <= N <= 100  
    对于60%的数据,1 <= N <= 1000 
    对于100%的数据,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000 

    【输出格式】
    一个整数表示答案

    【样例输入】
    3
    1 1 1
    2 2 2
    3 3 3

    【样例输出】
    27 

    二分

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<set>
    #include<map>
    #include<cmath>
     
    const int  maxn=1e5+5;
    typedef long long ll;
    
    using namespace std
    
    ll a[maxn],b[maxn],c[maxn],d[maxn],e[maxn];
    
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
    	{
    	scanf("%lld",&a[i]);
    	d[i]=-a[i];
    	}
        for(int i=1;i<=n;i++)
    	scanf("%lld",&b[i]);
        for(int i=1;i<=n;i++)
    	scanf("%lld",&c[i]);
        sort(d+1,d+1+n);
        sort(b+1,b+1+n);
        sort(c+1,c+1+n);
        ll ans=0;
        for(int i=1;i<=n;i++)
    	{
            e[i]=n-(upper_bound(d+1,d+n+1,(-b[i]))-(d+1));
        }
        for(int i=1;i<=n;i++)
    	{
            ans+=e[i]*(n-(upper_bound(c+1,c+n+1,(b[i]))-(c+1)));
        }
        cout<<ans<<endl;
        return 0;
    }

    7.
    如图p1.png所示的螺旋折线经过平面上所有整点恰好一次。  
    对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。  

    例如dis(0, 1)=3, dis(-2, -1)=9  

    给出整点坐标(X, Y),你能计算出dis(X, Y)吗?

    【输入格式】
    X和Y  

    对于40%的数据,-1000 <= X, Y <= 1000  
    对于70%的数据,-100000 <= X, Y <= 100000  
    对于100%的数据, -1000000000 <= X, Y <= 1000000000  

    【输出格式】
    输出dis(X, Y)  


    【样例输入】
    0 1

    【样例输出】
    3

    应该算是数学题,看每一圈的终止点,然后根据坐标去判断各种情况

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue> 
    #include<stack>
    #include<set>
    #include<vector>
    #include<cmath>
    #include<map>
    
    const int maxn=1e5+5;
    
    typedef long long ll;
    using namespace std;
    int main()
    {
    	ll x , y;
    	ll ans;
    	cin>>x>>y;
    	if(y > 0)
    	{	
    		if(abs(x)<=y)
    			ans = 3*y+(y*y-y)/2*8+x;	
    		else
    		{
    			if(x>0)
    				ans=3*x+(x*x-x)/2*8+2*x-y;
    
    			else
    				ans=3*-x+(x*x+x)/2*8+2*x+y;
    		}
    	}
    	else
    	{
    		if(y-1<= x &&x <= -y)
    			ans=7*-y+(y*y+y)/2*8-x;
    		else
    		{
    			if(x > 0)
    				ans=7*x+(x*x-x)/2*8-2*x-y;
    			else
    				ans=-7*x-7+(x*x+3*x+2)/2*8-2*x+y-1;
    		}
    	}
    	cout<<ans;
    	return 0;
    } 
    

    8.小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:

    ts id  

    表示在ts时刻编号id的帖子收到一个"赞"。  

    现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。  

    具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。  

    给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。  

    【输入格式】
    第一行包含三个整数N、D和K。  
    以下N行每行一条日志,包含两个整数ts和id。  

    对于50%的数据,1 <= K <= N <= 1000  
    对于100%的数据,1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000  

    【输出格式】
    按从小到大的顺序输出热帖id。每个id一行。  

    【输入样例】
    7 10 2  
    0 1  
    0 10    
    10 10  
    10 1  
    9 1
    100 3  
    100 3  

    【输出样例】
    1  
    3  
    思维?,差不多吧,就是每次按照id为第一关键字从小到大排,如果相同就按照ts排,然后看每一个的t+k项是否满足

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue> 
    #include<stack>
    #include<set>
    #include<vector>
    #include<cmath>
    #include<map>
    
    const int maxn=1e5+5;
    
    typedef long long ll;
    using namespace std;
    int vis[maxn];
    
    struct node
    {
    	int ts,id;
    }p[maxn];
    bool cmp(node x,node y)
    {
    	if(x.id!=y.id)
    	return x.id<y.id;
    	else
    	{
    		return x.ts<y.ts;
    	}
    }
    int main()
    {
    	int n,d,k;
    	cin>>n>>d>>k;
    	for(int t=0;t<n;t++)
    	{
    		scanf("%d%d",&p[t].ts,&p[t].id);
    	}
    	sort(p,p+n,cmp);
    	for(int t=0;t<n-k+1;t++)
    	{
    		if(p[t].id==p[t+k-1].id&&p[t+k-1].ts-p[t].ts<=d-1)
    		{
    			vis[p[t].id]=1;
    		}
    	}
    	for(int t=0;t<=100000;t++)
    	{
    		if(vis[t]==1)
    		{
    			cout<<t<<endl;
    		}
    	}
    	
    	return 0;
    }
    

    9.你有一张某海域NxN像素的照片,"."表示海洋、"#"表示陆地,如下所示:

    .......
    .##....
    .##....
    ....##.
    ..####.
    ...###.
    .......

    其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。  

    由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。  

    例如上图中的海域未来会变成如下样子:

    .......
    .......
    .......
    .......
    ....#..
    .......
    .......

    请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。  

    【输入格式】
    第一行包含一个整数N。  (1 <= N <= 1000)  
    以下N行N列代表一张海域照片。  

    照片保证第1行、第1列、第N行、第N列的像素都是海洋。  

    【输出格式】
    一个整数表示答案。

    【输入样例】

    .......
    .##....
    .##....
    ....##.
    ..####.
    ...###.
    .......  

    【输出样例】
    1  

    bfs,基本裸的bfs s2表示联通的有多少块,s1表示会消失的多少块,如果相等就表示这个联通的部分都消失了,所以s+1

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue> 
    #include<stack>
    #include<set>
    #include<vector>
    #include<cmath>
    #include<map>
    
    const int maxn=1e3+5;
    
    typedef long long ll;
    using namespace std;
    struct node
    {
    	int x,y;
    }p[maxn];
    char Map[maxn][maxn];
    int vis[maxn][maxn];
    int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
    ll s;
    void bfs(int x,int y)
    {  
          int s1=0;
          int s2=1;
          node start;
          start.x=x;
          start.y=y;
          vis[x][y]=1;
          queue<node>q;
          q.push(start);
          while(!q.empty())
          {
          	  node now;
          	  now=q.front();
          	  for(int t=0;t<4;t++)
          	  {
          	     if(Map[now.x+dir[t][0]][now.y+dir[t][1]]=='.')
    			   {
    			          s1++;
    					  break;	   
    			   }	
    		  }
          	  q.pop();
          	  for(int t=0;t<4;t++)
          	  {
          	  	node next;
          	  	next.x=now.x+dir[t][0];
          	  	next.y=now.y+dir[t][1];
          	    if(vis[next.x][next.y]==0&&Map[next.x][next.y]=='#')
          	    {
          	    	s2++;
          	      vis[next.x][next.y]=1;
    			  q.push(next);	
    			}
          	  	
    		  }
    	  }
    	  if(s2-s1==0)
    	  {
    	  	s++;
    	  }
    	
    }
    int main()
    {
    	int n;
    	cin>>n;
    	getchar();
    	s=0;
    	for(int t=0;t<n;t++)
    	{
    		scanf("%s",Map[t]);
    	}
    	for(int t=0;t<n;t++)
    	{
    	    for(int j=0;j<n;j++)
    	    {
    	    	if(Map[t][j]=='#'&&vis[t][j]==0)
    	    	{
    	    		bfs(t,j);
    			}
    		}
    	}
    	cout<<s<<endl;
    	return 0;
    }

    10.给定N个整数A1, A2, ... AN。请你从中选出K个数,使其乘积最大。  

    请你求出最大的乘积,由于乘积可能超出整型范围,你只需输出乘积除以1000000009的余数。  

    注意,如果X<0, 我们定义X除以1000000009的余数是负(-X)除以1000000009的余数。
    即:0-((0-x) % 1000000009)

    【输入格式】
    第一行包含两个整数N和K。  
    以下N行每行一个整数Ai。  

    对于40%的数据,1 <= K <= N <= 100  
    对于60%的数据,1 <= K <= 1000  
    对于100%的数据,1 <= K <= N <= 100000  -100000 <= Ai <= 100000  

    【输出格式】
    一个整数,表示答案。


    【输入样例】
    5 3 
    -100000   
    -10000   
    2   
    100000  
    10000  

    【输出样例】
    999100009

    再例如:
    【输入样例】
    5 3 
    -100000   
    -100000   
    -2   
    -100000  
    -100000

    【输出样例】
    -999999829

    不太懂怎么做,绝对值从大到小排了下,选了前k个,水过了%50,感觉自己也搞不出来

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue> 
    #include<stack>
    #include<set>
    #include<vector>
    #include<cmath>
    #include<map>
    
    const int maxn=1e5+5;
    
    typedef long long ll;
    using namespace std;
    int a[maxn];
    
    
    bool cmp(int x,int y)
    {
    	return fabs(x)>fabs(y);
    }
    int main()
    {
        int n,k;
        cin>>n>>k;
        for(int t=0;t<n;t++)
        {
        	scanf("%d",&a[t]);
    	}
    	sort(a,a+n,cmp);
    	ll sum=1;
    	for(int t=0;t<k;t++)
    	{
    	
    		sum=sum*a[t]%1000000009;
    	}
    	cout<<sum<<endl;
    	
    	
    	
    	return 0;
    }
  • 相关阅读:
    设计模式-策略模式
    设计模式-建造者模式
    js助手函数
    Javascript使用函数做命名空间
    腾讯云scf云函数,python依赖安装与上传的正确姿势
    nodejs爬虫,服务器经常返回ECONNRESET或者socket hang up错误的解决方法
    http请求,服务器回复内容为乱码的一种可能解决方法
    js在动态定义的新对象中,属性为变量时,如何动态定义
    nodejs中使用request时出现unable to verify the first certificate的一种解决办法
    Ubuntu偶然出现在待机解锁时,密码输入肯定正确但却无法进入的一种可能性
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781787.html
Copyright © 2011-2022 走看看