Advertisement
dmkozyrev

220.cpp

Jul 27th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <vector>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5.  
  6. int solve1(int screenHeight, int mouseHeight, int scrollStep, int tableHeight, int linesCount, const std::vector<int> & linePos) {
  7.     std::vector<int> count(screenHeight), countMod(scrollStep);
  8.    
  9.     // First screen
  10.     int left = 0, right = linesCount-1;
  11.    
  12.     while (left < linesCount && linePos[left] < screenHeight) {
  13.         for (int i = linePos[left]; i >= 0; i -= scrollStep) {
  14.             ++count[i];
  15.         }
  16.         ++left;
  17.     }
  18.    
  19.     // Last Screen
  20.     // U-1 + k*T == L-1 --> k = [(L-U) / T]+1;
  21.     int k = (int)ceil((tableHeight-screenHeight) * 1.0 / scrollStep);
  22.     while (right > left && linePos[right] >= k*scrollStep) {
  23.         for (int i = linePos[right]-k*scrollStep; i < screenHeight; i += scrollStep) {
  24.             ++count[i];
  25.         }
  26.         --right;
  27.     }
  28.    
  29.     // Mid screen
  30.     for (int line_id = left; line_id <= right; ++line_id) {
  31.         ++countMod[linePos[line_id] % scrollStep];
  32.     }  
  33.    
  34.     // Find best pos
  35.     int min_sum = (int) 1e9;
  36.     for (int startPos = 0; startPos+mouseHeight-1 < screenHeight; ++startPos) {
  37.         int sum = 0;
  38.         for (int i = startPos; i <= startPos+mouseHeight-1; ++i) {
  39.             sum += count[i]+countMod[i % scrollStep];
  40.         }              
  41.         min_sum = std::min(sum, min_sum);
  42.     }
  43.    
  44.     return min_sum;
  45. }
  46.  
  47. int main() {
  48.     freopen("input.txt", "rt", stdin);
  49.      
  50.     int screenHeight, mouseHeight, scrollStep, tableHeight;
  51.     scanf("%d %d %d %d", &screenHeight, &mouseHeight, &scrollStep, &tableHeight);
  52.      
  53.     int countLines;
  54.     scanf("%d", &countLines);
  55.      
  56.     std::vector<int> linePos(countLines);
  57.      
  58.     for (auto & x : linePos)
  59.         scanf("%d", &x);
  60.        
  61.     printf("%d", solve1(screenHeight, mouseHeight, scrollStep, tableHeight,countLines,linePos));
  62.    
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement