Rikka with Competition

Rikka with Competition

给出一个大小为n的集合\(\{a_i\}\),每次从集合中随机挑出一对数\(a_i,a_j\),如果\(|a_i-a_j|>K\),那么从集合中删掉较小的那个数字,否则随机删掉一个数字,显然会有一个数字保留到最后,现在询问能够有可能保留到最后一个数字的数字个数,\(n\leq 10^5\)。

序列无序,不妨排个序构成一个序列,显然序列最后一个元素必然有机会留到最后,此时考虑倒数第二个元素,如果它和最后一个元素的差的绝对值不超过K那么也有机会保留到最后。

于是我们得到一个做法,从最后一个元素往前扫描,如果它与后一个元素的差超过了K就break掉,那么这个元素后的所有元素都有机会保留到最后。

参考代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#define il inline
#define ri register
#define Size 105000
using namespace std;
int a[Size];
il void read(int&);
int main(){
    int lsy,n,k;read(lsy);
    while(lsy--){
        read(n),read(k);
        for(int i(1);i<=n;++i)
            read(a[i]);
        sort(a+1,a+n+1);
        int ans(1);
        for(int i(n-1);i;--i)
            if(a[i+1]-a[i]<=k)
                ++ans;else break;
        printf("%d\n",ans);
    }
    return 0;
}
il void read(int &x){
    x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

原文地址:https://www.cnblogs.com/a1b3c7d9/p/11408483.html

时间: 2024-10-21 00:16:36

Rikka with Competition的相关文章

HDU 6095: Rikka with Competition

Rikka with Competition ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int>v(n); for 

E - Rikka with Competition

2017-09-22 22:01:19 writer:pprp As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: A wrestling match will be held tomorrow. nn players will take part in it. T

Rikka with Competition hdu 6095

签到题目,排序然后按序清理掉一定会输的结果就可以. ac代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int a[100001]; int main() { int t; cin>>t; while(t--) { int n,k; int sum=0; scanf("%d %d&

HDU6095

Rikka with Competition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 772    Accepted Submission(s): 588 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situa

CIKM Competition数据挖掘竞赛夺冠算法陈运文

CIKM Competition数据挖掘竞赛夺冠算法陈运文 背景 CIKM Cup(或者称为CIKM Competition)是ACM CIKM举办的国际数据挖掘竞赛的名称.CIKM全称是International Conference on Information and Knowledge Management,属于信息检索和数据挖掘领域的国际著名学术会议,由ACM SIGIR分会(ACM Special Interest Group on Information Retrieval)主办.

HDU 5422:Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张nn个点mm条边的无向图,每一条边的长度都是1.现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短

HDU 5423:Rikka with Tree Dijkstra算法

Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有

hdu 5422 Rikka with Graph(简单题)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has a non-direct graph with n vertices and m edges. The length of each edge is 1.

hdu 5423 Rikka with Tree(dfs)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of