抽屉原理/快速读入

概念

十个苹果放进九个抽屉,一定存在一个抽屉中放着至少两个苹果的情况。

算法题

题目

给定一个长度为n的整数数列,你需要判断是否能找到一个长度为(int)sqrt(n-1)的非降序列或者一个长度为(int)sqrt(n-1)的非增序列。

形式上表示,输出答案YES当且仅当你可以找到一个子序列a:他的长度为(int)sqrt(n-1)并且对于每个i,(2≤i≤n),ai≤ai-1 或者你可以找到一个子序列a:他的长度为(int)sqrt(n-1)并且对于每个i,(2≤i≤n),ai-1≤ai。(建议使用快速读入)

输入

第一行包含一个数t (1≤t≤10)——表示t组样例,
然后对于每组样例,第一行给出一个数n(1≤n≤1e7),表示数组长度。 然后接下来的一行,包含n个整数a1,a2,a3,……an (1≤i≤n 1≤ai<=1e18) ai表示第i个数。数据保证∑n≤4e7。

输出

如果能找到就输出YES,否则输出NO。

E.G.

input

2
1 1
1 3

output

YES

YES

题解

根据抽屉原理,可以发现,一定可以找到满足题意的子序列。(题目中没有说明子序列的连续性,则默认可连续可不连续,即任一情况满足就输出YES,做题的时候自己默认了必须连续,愣是没做出来。。。)

#include<cstdio>

inline int read() {
    char ch = getchar();
    int flag = 1;
    int num = 0;
    while (ch < '0' || ch > '9') {
        if (ch == '-') {
            f = -1;
        }
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        num = num * 10 + ch - '0';
        ch = getchar();
    }
    return num * flag;
}

int main() {
    int t = read;
    while (t--) {
        int n = read();
        for (int i = 0; i < n; i++) {
            read();
        }
        printf("YES");
        if (t != 0) printf("\n");
    }
    return 0;
}

快速读入

快速读入,顾名思义,很快地读入数据。

以前做题的时候见到别人这么写,还不知道这是个啥。

inline int read() {
    char ch = getchar();
    int flag = 1;
    int num = 0;
    while (ch < '0' || ch > '9') {
        if (ch == '-') {
            f = -1;
        }
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        num = num * 10 + ch - '0';
        ch = getchar();
    }
    return num * flag;
}

num的赋值可以用位运算num = (num << 1) + (num << 3) + ch - '0'

原理很简单,getchar()速度比scanfcin要快。

对应快读,自然有快写

inline void write(int x) {
    char F[200];
    int temp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (temp > 0) {
        F[cnt++] = temp % 10 + '0';
        temp /= 10;
    }
    while (cnt > 0) putchar(F[- -cnt]);
    //这里 --连起来回和markdown语法冲突了,中间用了个空格
}

不开数组的方法

inline void write(int x) {
    int y = 10;
    int len = 0;
    while (y <= x) {y * =10; len++;}
    while (len--) {y /= 10; putchar(x / y + 48); x %= y;}
}

输入输出的速度比较

cin < cin(关闭同步) < scanf() < read() < fread();

输出同。

cin(关闭同步)

有另一种快速读入的方法(依然比read()慢), 但是用了之后就不能使用printfscanf了。

ios::sync_with_stdio(false);
int a;
cin >> a;

此外

需要知道,这并不是最快的,更快的还有fread(),还有比它还快的······

#define ONLINE_JUDGE
#ifdef ONLINE_JUDGE
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() { return p1 == p2 && (++p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++; }
#define getchar getc
#endif
template <typename T>
inline T read() {
    T a = 0; char c = getchar(), f = 1;
    for(; c < '0' || c > '9'; c = getchar())
        if(c == '-') f = -f;
        else if(c == -1) return -1;
    for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + (c ^ 48);
    return a * f;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值