2020年牛客算法入门课练习赛1 1.快排 2.set 3.前缀和+双指针

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

第k小数
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
给你一个长度为n的序列,求序列中第k小数的多少。
输入描述:
多组输入,第一行读入一个整数T表示有T组数据。
每组数据占两行,第一行为两个整数n,k,表示数列长度和k。
第二行为n个用空格隔开的整数。
输出描述:
对于每组数据,输出它的第k小数是多少。
每组数据之间用空格隔开
示例1
输入
复制
2
5 2
1 4 2 3 4
3 3
3 2 1
输出
复制
2
3
备注:
t \leq10 , 1\leq n\leq5\times 10^6,k\leq n,数列里每个数都在int范围内t≤10,1≤n≤5×10
6
,k≤n,数列里每个数都在int范围内
由于输入比较多,请使用快读读取。
例如:
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < ‘0’ || ch > ‘9’){
if (ch == ‘-’)
f = -1;
ch = getchar();
}
while(ch >= ‘0’ && ch <= ‘9’){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
const int maxn=5e6+5;
int a[maxn];
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
int main(){
    int n,k;
    int t;
    t=read();
    while(t--){
    n=read(),k=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
        if(i==k){
            printf("%d\n",a[i]);
            break;
        }
    }
    }

   return 0;
}

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

题目描述
在坐标纸上有N个不重合的点,两两可以连一个线段并延伸成直线,请问在这些直线里最多能选出多少条使得他们两两不平行也不重合。

输入描述:
第1行: 输入1个正整数:N

第2…N+1行:第i+1行是两个用空格隔开的整数,为点i的坐标(Xi,Yi)

输出描述:
输出1个整数,为最多的互不平行的直线数目。
示例1
输入
复制
3
1 0
-2 0
0 0
输出
复制
1
备注:N≤200,−1000≤Xi,Yi≤1000

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <climits>
#include <map>
#include <cmath>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<double,double> pdd;
set<double>s;
const int maxn=250;
double x[maxn],y[maxn];
inline int read()
{
    int  f = 1, x = 0;
    char ch;
    ch = getchar();
    while (!isdigit(ch))
    {
        f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    return x * f;
}

void write(int  x)        
{        
    if(x < 0) {        
        putchar('-');        
        x = -x;        
    }        
    if(x > 9)        
        write(x/10);        
    putchar(x % 10 + '0');        
    return;        
}
bool cmp(string a,string b){
    return a+b>b+a;
} 
int main()
{
   std::ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
  int n;
  cin>>n;
  int f=0;
  for(int i=0;i<n;i++){
      cin>>x[i]>>y[i];
  }
  for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
          if(y[i]==y[j]){
              f=1;
              continue;
          }
          double e=(x[i]-x[j])/(y[i]-y[j]);
          s.insert(e);

      }
  }
  cout<<s.size()+f;
  return 0;
}

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

题目描述
“丢丢手绢,轻轻地放在小朋友的后面,大家不要告诉她,快点快点抓住她,快点快点抓住她。”
牛客幼儿园的小朋友们围成了一个圆圈准备玩丢手绢的游戏,但是小朋友们太小了,不能围成一个均匀的圆圈,即每个小朋友的间隔可能会不一致。为了大家能够愉快的玩耍,我们需要知道离得最远的两个小朋友离得有多远(如果太远的话牛老师就要来帮忙调整队形啦!)。
因为是玩丢手绢,所以小朋友只能沿着圆圈外围跑,所以我们定义两个小朋友的距离为沿着圆圈顺时针走或者逆时针走的最近距离。
输入描述:
第一行一个整数N,表示有N个小朋友玩丢手绢的游戏。
接下来的第2到第n行,第i行有一个整数,表示第i-1个小朋友顺时针到第i个小朋友的距离。
最后一行是第N个小朋友顺时针到第一个小朋友的距离。
输出描述:
输出一个整数,为离得最远的两个小朋友的距离。
示例1
输入
复制
3
1
2
3
输出
复制
3
备注:
2≤N≤100000
距离和(圆圈周长)小于等于2147483647

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main(){
   int n;
   cin>>n;
   for(int i=1;i<=n;i++){
       cin>>a[i];
       a[i]+=a[i-1];
   }
   int sum=a[n];
   int res=-1,l=0,r=1,dis1=0,dis2=0;
   while(l<r&&r<=n){
    dis1=a[r]-a[l];
    dis2=sum-dis1;
    res=max(res,min(dis1,dis2));
      if(dis1<dis2){
          r++;
      }
       else l++;
   }
   cout<<res<<endl;
   return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值