zoukankan      html  css  js  c++  java
  • BZOJ 1982 / Luogu SP2021: [Spoj 2021]Moving Pebbles (找平衡状态)

    这道题在论文里看到过,直接放论文原文吧
    在这里插入图片描述
    在这里插入图片描述
    在BZOJ上是单组数据,而且数据范围符合,直接int读入排序就行了.代码:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int MAXN = 100005;
    int n, a[MAXN];
    int main () {
    	scanf("%d", &n);
    	for(int i = 1; i <= n; ++i)
    		scanf("%d", &a[i]);
    	if(n&1) { puts("first player"); return 0; }
    	sort(a + 1, a + n + 1);
    	for(int i = 1; i <= n; i+=2)
    		if(a[i] != a[i+1]) { puts("first player"); return 0; }
    	puts("second player");
    }
    

    但是这个代码加上多组数据后交到洛谷上去却WA了??然后我们打开题解,发现原来数据范围大于int,大家都用了string?然后我们又会神奇的发现,下面这个代码是AC的:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int MAXN = 100005;
    int n, a[MAXN];
    inline void read(int &num) {
        char ch; while((ch=getchar())<'0'||ch>'9');
        for(num=0;ch>='0'&&ch<='9';num=num*10+ch-'0',ch=getchar());
    }
    int main () {
        while(~scanf("%d", &n)) {
            for(int i = 1; i <= n; ++i)
                read(a[i]);
            if(n&1) { puts("first player"); goto there; }
            sort(a + 1, a + n + 1);
            for(int i = 1; i <= n; i+=2)
                if(a[i] != a[i+1]) { puts("first player"); goto there; }
            puts("second player");
            there:;
        }
    }
    

    WTF?为啥呢?

    AC其实是读入优化的功劳,因为就算读入会溢出,但是手写的读入优化相当于是在int范围内自然溢出,于是就相当于hash了.
    那么就是说scanf读入溢出的数据会爆成不同的值…吗?本地windows测的话读入优化和scanf都会得到同样的值啊…无语…难道是评测环境的差异?
    求解.离线等…

  • 相关阅读:
    不可或缺 Windows Native (15)
    不可或缺 Windows Native (14)
    不可或缺 Windows Native (13)
    不可或缺 Windows Native (12)
    不可或缺 Windows Native (11)
    不可或缺 Windows Native (10)
    不可或缺 Windows Native (9)
    不可或缺 Windows Native (8)
    不可或缺 Windows Native (7)
    不可或缺 Windows Native (6)
  • 原文地址:https://www.cnblogs.com/Orz-IE/p/12039390.html
Copyright © 2011-2022 走看看