Aizu The Maximum Number of Customers
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_A The Maximum Number of Customers
Idea: find the most overlapped segment. Similiar to the line painting
Mark the left and right element, x: L, O: R
#include<cstdio> #include<cstdlib> #include<cstring> using namespace std; //https://www.hustyx.com/cpp/5/ -- reference #define N 100005 int a[N]; int main(){ //input freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); memset(a,0,N); int n,t,l,r; scanf("%d %d",&n,&t); for(int i = 0; i<n; i++){ scanf("%d %d",&l,&r); a[l]++; a[r]--; } //preprocess in the array int max = 0; int count = 0; for(int i = 0; i<N; i++){ count += a[i]; if(max<count) max = count; } printf("%d\n",max); //printf("wei"); return 0; }
C++ learning from this code
* redefine the stream from input file
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
* memset in cstring