Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
数据太水了,其实这个解法是错的In Reply To:最简单的解法:sort + 贪心 时间:1500ms Posted by:Ioencgc at 2023-07-23 22:53:30 数据太水了,其实这个解法是错的 因为题目中要求sorted顺次连接而成,所以不能直接sort+贪心 正解: #include<iostream> #include<stack> #include<string.h> #include<cmath> #include<iomanip> #include<algorithm> #include<climits> #include<cstdio> #include<vector> #include<sstream> #include<ctype.h> #include<set> #include<map> #include<ctime> #include<stdlib.h> #include<queue> #include<bitset> #define ll long long #define inf INT_MAX using namespace std; template<typename TTT>inline void mr(TTT& theNumberToRead) { theNumberToRead = 0; bool prn = false; char c = getchar(); while (!isdigit(c)) { if (c == '-')prn = true; c = getchar(); } while (isdigit(c)) theNumberToRead = 10 * theNumberToRead + (c ^ 48), c = getchar(); if (prn) theNumberToRead = -theNumberToRead; } template<typename TTT>inline TTT mrr() { TTT theNumberToRead = 0; bool prn = false; char c = getchar(); while (!isdigit(c)) { if (c == '-')prn = true; c = getchar(); } while (isdigit(c)) theNumberToRead = 10 * theNumberToRead + (c ^ 48), c = getchar(); return prn ? -theNumberToRead : theNumberToRead; } template<typename T>void my_write(T x) { if (x) my_write(x / 10), putchar(x % 10 ^ 48); } template<typename T>void mw(T x, char mid) { if (x) { if (x < 0) putchar('-'), x = -x; my_write(x); } else putchar(48); if (mid) putchar(mid); } // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** const int maxm = 500000, maxn = 50000; int s[maxm], t[maxm], dp[maxn + 1], n2, st[1 << 17]; inline int ls(int x) { return x << 1; } inline int rs(int x) { return x << 1 | 1; } inline int fa(int x) { return x >> 1; } inline void up(int x) { st[x] = min(st[ls(x)], st[rs(x)]); } void st_init(int n) { for (n2 = 1; n2 <= n; n2 <<= 1); fill(st, st + (1 << 17), 500000); for (int i = n2 + 1; i; i >>= 1) st[i] = 0; } void update(int i, int x) { i += n2, st[i] = x; for (i >>= 1; i; i >>= 1) up(i); } int query(int l, int r) { int ans = INT_MAX; for (l += n2 - 1, r += n2 + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1 && ans > st[l ^ 1]) ans = st[l ^ 1]; if (r & 1 && ans > st[r ^ 1]) ans = st[r ^ 1]; } return ans; } int main() { int n, m; mr(n), mr(m); for (int i = 0; i < m; ++i) mr(s[i]), mr(t[i]); fill(dp, dp + n + 1, 500000); dp[1] = 0; st_init(n); for (int i = 0, v; i < m; ++i) { v = min(dp[t[i]], query(s[i], t[i]) + 1); dp[t[i]] = v; update(t[i], v); } mw(dp[n], '\n'); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator