首页 > 代码库 > [51NOD1272]最大距离(贪心)

[51NOD1272]最大距离(贪心)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272

思路:排序后由于序列都是顺序的,则只需要考虑序号了,加入当前维护的序号比后面的小,则更新ret。否则更新当前序号

 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio>10 #include <bitset>11 #include <vector>12 #include <deque>13 #include <queue>14 #include <stack>15 #include <ctime>16 #include <set>17 #include <map>18 #include <cmath>19 20 using namespace std;21 #define fr first22 #define sc second23 #define cl clear24 #define BUG puts("here!!!")25 #define W(a) while(a--)26 #define pb(a) push_back(a)27 #define Rint(a) scanf("%d", &a)28 #define Rll(a) scanf("%lld", &a)29 #define Rs(a) scanf("%s", a)30 #define Cin(a) cin >> a31 #define FRead() freopen("in", "r", stdin)32 #define FWrite() freopen("out", "w", stdout)33 #define Rep(i, len) for(int i = 0; i < (len); i++)34 #define For(i, a, len) for(int i = (a); i < (len); i++)35 #define Cls(a) memset((a), 0, sizeof(a))36 #define Clr(a, p) memset((a), (p), sizeof(a))37 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))38 #define lrt rt << 139 #define rrt rt << 1 | 140 #define pi 3.1415926535941 #define RT return42 #define lowbit(p) p & (-p)43 #define onenum(p) __builtin_popcount(p)44 typedef long long LL;45 typedef long double LD;46 typedef unsigned long long ULL;47 typedef pair<int, int> pii;48 typedef pair<string, int> psi;49 typedef pair<LL, LL> pll;50 typedef map<string, int> msi;51 typedef vector<int> vi;52 typedef vector<LL> vl;53 typedef vector<vl> vvl;54 typedef vector<bool> vb;55 56 const int maxn = 50500;57 typedef struct Node {58     int i, n;59 }Node;60 61 int n;62 int a[maxn];63 Node b[maxn];64 65 bool cmp(Node x, Node y) {66     if(x.n == y.n) return x.i < y.i;67     return x.n < y.n;68 }69 70 int main() {71     // FRead();72     while(~Rint(n)) {73         Rep(i, n) {74             Rint(a[i]);75             b[i].n = a[i]; b[i].i = i;76         }77         sort(b, b+n, cmp);78         int ret = 0, cur = b[0].i;79         For(i, 1, n) {80             if(b[i].i > cur) ret = max(ret, b[i].i-cur);81             else cur = b[i].i;82         }83         printf("%d\n", ret);84     }85     RT 0;86 }

 

[51NOD1272]最大距离(贪心)