Question: 1372-Log Jumping the start position and length of n boards. Overlapping boards can jump to each other to obtain the maximum number of rings. Train of Thought: first sort by the starting position, and then judge whether he and the previous one can jump to each other with one more board each time. If yes, they can add more boards. Code:
# Include
# Include
# Include using namespace std; # define max (a, B) (a)> (B )? (A) :( B) # define INF 0x3f3f3fconst int N = 5005; int t, n, k, I, start [N]; bool check (int s, int e) {return e-s <= k;} int main () {scanf ("% d", & t); while (t --) {int ans = 0; scanf ("% d", & n, & k); for (I = 0; I <n; I ++) scanf ("% d ", & start [I]); sort (start, start + n); I = 0; while (I <n) {int sum = 1; while (I <n-1 & (sum = 1 & check (start [I], start [I + 1]) | (check (start [I], start [I + 1]) & chec K (start [I-1], start [I + 1]) sum ++, I ++; if (! Check (start [I], start [I + 1]) | I = n-1) I ++; ans = max (ans, sum );} printf ("% d \ n", ans);} return 0 ;}