zoukankan      html  css  js  c++  java
  • 长沙理工大学第十二届ACM大赛-重现赛I 主持人的烦恼 (sort)

    链接:https://ac.nowcoder.com/acm/contest/1/I
    来源:牛客网

    主持人的烦恼
    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 131072K,其他语言262144K
    64bit IO Format: %lld
    题目描述
    一天zzq主持一项游戏,共n位同学,需要两两同学为一组来上台来玩一项游戏。
    但是,众所周知,玩游戏的时候,如果两个人的颜值差距>=m,就会互相嫌弃。
    所以,为了游戏能够好玩。在游戏开始前,zzq已经调查了所有n个同学的颜值。
    但是现在问题又来了,zzq想知道,最多能凑出多少组同学一起上台?
    需注意一人只能出现在一个组中。
    输入描述:
    多组输入
    第一行两个正整数n m(n<=1e5,m<=1e9),意义见描述
    第二行有n个由空格分开的正整数xi(xi<=1e9),第i个同学的颜值
    输出描述:
    每一行输出一个数,表示最多能凑出多少组。
    示例1
    输入
    复制
    4 3
    1 3 3 2
    4 2
    1 4 6 2
    输出
    复制
    2
    1
    说明
    第二组样例中,编号为1的同学(颜值是1)与编号为4的同学(颜值是2),颜值差距为1,可以组成一组

    题意:

    思路:
    直接排个序然后对于每一个数a[i] 和它右边的数做差值比较即可 ,或者用双指针扫一遍也可以。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=100010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int n,m;
    int a[maxn];
    int vis[maxn];
    int main()
    {
         // freopen("D:\code\text\input.txt","r",stdin);
    	//freopen("D:\code\text\output.txt","w",stdout);
    	while(~scanf("%d %d",&n,&m))
    	{
    		repd(i,1,n)
    		{
    			gg(a[i]);
    		}
    		sort(a+1,a+n+1);
    		repd(i,1,n)
    		{
    			vis[i]=0;
    		}
    		int ans=0;
    		if(n>1)
    		{
    			int l=n-1;
    			int r=n;
    			while(l>=1)
    			{
    				while(vis[r])
    				{
    					r--;
    				}
    				while(vis[l])
    				{
    					l--;
    				}
    				while(l>=r)
    				{
    					l--;
    				}
    				if(l<1)
    				{
    					break;
    				}
    				if(a[r]-a[l]<m)
    				{
    					ans++;
    					vis[r]=vis[l]=1;
    					r--;
    					l--;
    				}else
    				{
    					r--;
    					l--;
    				}
    			}
    		}
    		printf("%d
    ",ans );
    		// cout<<ans<<endl;
    	}
    	// 1 2 4 6
    
    
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    
    
    #include<bits/stdc++.h>
    using namespace std;
     
    int a[100005];
     
    int main(){
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            for(int i=1;i<=n;i++)
                scanf("%d",&a[i]);
            sort(a+1,a+1+n);
            int sum=0;
            for(int i=2;i<=n;i++){
                if(a[i]-a[i-1]<m) sum++,i++;
            }
            printf("%d
    ",sum);
        }
    }
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    org.tinygroup.ehcache-EhCache缓存解决方案
    org.tinygroup.validate-验证框架
    org.tinygroup.context-上下文环境
    org.tinygroup.commons-常用工具类
    org.tinygroup.context2object-参数对象构建
    org.tinygroup.config-统一应用配置
    org.tinygroup.binarytree-二叉树
    org.tinygroup.service-服务
    flask之wtform与flask-session组件
    Flask信号源码流程
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10996363.html
Copyright © 2011-2022 走看看