UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#211075#1445. 理想的正方形FHL100357ms36136kbC++115.1kb2024-08-09 10:17:582024-08-09 10:18:00

answer

#include <cstdio>
#include <algorithm>
#pragma GCC optimize(2)
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar getchar_unlocked
#endif
namespace FastIO {
    char write_cache[40];
    template <class T>
    inline const T read() noexcept {
        T x(0);
        char ch(getchar());
        bool f(0);
        while (ch < '0' || ch > '9') f ^= ch == '-', ch = getchar();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
        return f ? -x : x;
    }
    template <class T>
    inline const void read(T &x) noexcept {
        x = 0;
        char ch(getchar());
        bool f(0);
        while (ch < '0' || ch > '9') f ^= ch == '-', ch = getchar();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
        x = f ? -x : x;
    }
    template <class T, class... P>
    inline const void read(T &x, P &...ark) noexcept {
        x = 0;
        char ch(getchar());
        bool f(0);
        while (ch < '0' || ch > '9') f ^= ch == '-', ch = getchar();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
        x = f ? -x : x;
        read(ark...);
    }
    template <class T>
    inline const void readu(T &x) noexcept {
        x = 0;
        char ch(getchar());
        bool f(0);
        while (ch < '0' || ch > '9') ch = getchar();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    }
    template <class T>
    inline const T readu() noexcept {
        T x(0);
        char ch(getchar());
        bool f(0);
        while (ch < '0' || ch > '9') ch = getchar();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
        return x;
    }
    template <class T, class... P>
    inline const void readu(T &x, P &...ark) noexcept {
        x = 0;
        char ch(getchar());
        bool f(0);
        while (ch < '0' || ch > '9') ch = getchar();
        while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
        readu(ark...);
    }
    template <class T>
    inline const void readArr(T *begin, T *end) noexcept {
        while (begin < end) {
            *begin = 0;
            char ch(getchar());
            bool f(0);
            while (ch < '0' || ch > '9') f ^= ch == '-', ch = getchar();
            while (ch >= '0' && ch <= '9') *begin = (*begin << 1) + (*begin << 3) + (ch ^ 48), ch = getchar();
            *begin = f ? -*begin : *begin;
        }
    }
    template <class T>
    inline const void readArr(T *begin, int cnt) noexcept {
        while (cnt--) {
            read(*begin);
            ++begin;
        }
    }
    template <class T>
    inline const void write(T x) noexcept {
        if (x < 0) putchar('-'), x = -x;
        int cnt = 0;
        while (x) write_cache[cnt++] = x % 10 ^ 48, x /= 10;
        if (!cnt) putchar('0');
        else while (cnt--) putchar(write_cache[cnt]);
    }
    template <char end = ' ', class T, class... ARK>
    inline const void write(T &x, ARK &...ark) noexcept {
        write(x);
        putchar(end);
        write(ark...);
    }
    template <char end = '\n', class T>
    inline const void println(T x) noexcept {
        if (x < 0) putchar('-'), x = -x;
        int cnt = 0;
        while (x) write_cache[cnt++] = x % 10 ^ 48, x /= 10;
        if (!cnt) putchar('0');
        else while (cnt--) putchar(write_cache[cnt]);
        putchar(end);
    }
    template <char sep = ' ', char endl = '\n', class T>
    inline const void writeArr(T *begin, T *end) noexcept {
        while (begin < end) write(*begin), putchar(sep), ++begin;
        putchar(endl);
    }
    template <char sep = ' ', char end = '\n', class T>
    inline const void writeArr(T *arr, int cnt) {
        while (cnt--) {
            write(*arr);
            putchar(sep);
            ++arr;
        }
        putchar(end);
    }
}
using namespace FastIO;
inline int _min(int x, int y) {
	return (x < y) ? x : y;
}
const int N = 5005, INF = 1e9 + 7;
int n, m, k, a[N][N], q[N], h = 1, t, ans = INF, r[N][N], l[N][N], maxn[N], minn[N], maxx[N], minx[N];
inline void work(int a[], int ans[], int len, bool flag) {
	int x, y;
    h = 1;
	t = 0;
    for (int i = 1; i <= len; ++ i ) {
        if (h <= t && q[h] <= i - k) h ++ ;
        if (flag) while (h <= t && a[i] <= a[q[t]]) t -- ;
        else while (h <= t && a[i] >= a[q[t]]) t -- ;
        q[ ++ t] = i;
        if (i >= k) ans[i - k + 1] = a[q[h]];
    }
}
signed main() {
    read(n, m, k);
    for (int i = 1; i <= n; ++ i ) for (int j = 1; j <= m; ++ j ) read(a[i][j]);
    for (int i = 1; i <= n; ++ i ) work(a[i], r[i], m, 0), work(a[i], l[i], m, 1);
    for (int j = 1; j <= m - k + 1; ++ j ) {
        for (int i = 1; i <= n; ++ i ) maxn[i] = r[i][j], minn[i] = l[i][j];
        work(maxn, maxx, n, 0), work(minn, minx, n, 1);
        for (int i = 1; i <= n - k + 1; ++ i ) ans = _min(ans, maxx[i] - minx[i]);
    }
    println(ans);
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 75ms
memory: 34888kb

input:

1000 1000 100
804544523 340648618 718292412 235345736 704741136 942776831 741228920 463473302 677289...

output:

998893495

result:

ok single line: '998893495'

Test #2:

score: 10
Accepted
time: 74ms
memory: 34884kb

input:

1000 1000 100
629053044 957239666 456746076 940194386 201529807 592062148 244394389 825620014 223976...

output:

998862873

result:

ok single line: '998862873'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1052kb

input:

5 4 2
1 2 5 6
0 17 16 0
16 17 0 1
2 10 2 1
1 2 3 2

output:

2

result:

ok single line: '2'

Test #4:

score: 10
Accepted
time: 0ms
memory: 2416kb

input:

100 100 10
2 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 1200001 ...

output:

908999

result:

ok single line: '908999'

Test #5:

score: 10
Accepted
time: 45ms
memory: 36136kb

input:

1000 1000 20
1 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 120000...

output:

1901899

result:

ok single line: '1901899'

Test #6:

score: 10
Accepted
time: 21ms
memory: 12464kb

input:

500 500 50
79289095 232165705 955620938 481434262 465576217 112035388 50089892 459799006 181906335 3...

output:

995944328

result:

ok single line: '995944328'

Test #7:

score: 10
Accepted
time: 22ms
memory: 18092kb

input:

500 1000 80
499163842 331022295 940054497 684192083 248823911 842132608 629298697 398526298 98438040...

output:

998299092

result:

ok single line: '998299092'

Test #8:

score: 10
Accepted
time: 60ms
memory: 35200kb

input:

1000 1000 80
102 134 429 251 299 109 264 669 727 296 112 550 270 270 544 660 131 546 968 219 113 678...

output:

998

result:

ok single line: '998'

Test #9:

score: 10
Accepted
time: 22ms
memory: 12076kb

input:

500 500 100
65532583 920409544 753795976 989349545 818384831 778207112 425141881 853270293 542112588...

output:

999172505

result:

ok single line: '999172505'

Test #10:

score: 10
Accepted
time: 38ms
memory: 17940kb

input:

500 1000 100
456896744 779471086 578349238 52507342 192194992 156396237 475084995 775765435 96617474...

output:

998801667

result:

ok single line: '998801667'