Question: Click to open the link
Not written for more than a monthCodeNow, for Shousheng's 1B and KMP, we can't understand how to avoid being crushed in the tat multi-School Exercise competition .......
Do some greedy questions to find the feeling. This is a typical problem, with a range coverage.
The number of covered line segments should be intercepted as much as possible, and the difference value must be large first, so as to ensure the shortest length of Line Segment coverage.
Do not forget the line segment deviation-1 ..
# Include <iostream> # include <cstring> # include <algorithm> using namespace STD; int qujian [400]; int Cha [400]; bool CMP1 (int A, int B) {return a <B;} bool cmp2 (int A, int B) {return A> B;} int main () {int n, m; while (CIN> N> m) {memset (qujian, 0, sizeof (qujian); memset (CHA, 0, sizeof (CHA )); for (INT I = 0; I <n; I ++) CIN> qujian [I]; sort (qujian, qujian + N, CMP1 ); for (Int J = 0; j <n-1; j ++) {Cha [J] = qujian [J + 1]-qujian [J]-1;} Sort (CHA, CHA + n-1, cmp2); int sum = qujian [n-1]-qujian [0] + 1; for (int K = 0; k <m-1; k ++) {sum-= CHA [k] ;}cout <sum <Endl ;}return 0 ;}