Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <sstream>
  7. #include <cstdio>
  8. #include <algorithm>
  9. #include <stack>
  10. #include <queue>
  11. #include <cmath>
  12. #include <iomanip>
  13. #include <fstream>
  14. //#include <bits/stdc++.h>
  15. using namespace std;
  16. typedef long long ll;
  17. const int INF = (1 << 30);
  18. const ll inf = (1LL << 60LL);
  19. const int maxn = 1e5 + 5;
  20. int n, k;
  21. int arr[maxn], sortedArr[maxn];;
  22. int p[maxn];
  23. int cnt[maxn];
  24. int tmpArr[maxn];
  25. bool visited[maxn];
  26. int main(int argc, const char * argv[]) {
  27.     ios_base::sync_with_stdio(false);
  28.     cin >> n >> k;
  29.     //    ifstream cin("in.txt");
  30.     for(int i =0 ; i < n; i ++){
  31.         cin >> arr[i];
  32.     }
  33.     for(int i = 0; i < k; i ++){
  34.         cin >> p[i];
  35.         p[i] --;
  36.     }
  37.     ll A, B;
  38.     int mxx1, mxx2, turn;
  39.     for(int crt= 0; crt < k; crt ++){
  40.         for(int j = 0; j <= n; j ++){
  41.             visited[j] = false;
  42.             cnt[j] = 0;
  43.         }
  44.         mxx1 = 0;
  45.         for(int j = 0; j <= p[crt]; j ++){
  46.             cnt[arr[j]] ++;
  47.             mxx1 = max(mxx1, arr[j]);
  48.         }
  49.         int cc = 0;
  50.         A = B = turn = 0;
  51.         int tmp = 0;
  52.         for(int j = 0; j < n; j ++){
  53.             while(mxx1 > 0 && cnt[mxx1] == 0){
  54.                 mxx1 --;
  55.             }
  56.             if(mxx1 > tmp){
  57.                 cnt[mxx1] --;
  58.                 if(turn == 0){
  59.                     A += mxx1;
  60.                 }
  61.                 else{
  62.                     B += mxx1;
  63.                 }
  64.             }
  65.             else{
  66.                 if(turn == 0){
  67.                     A += tmp;
  68.                 }
  69.                 else{
  70.                     B += tmp;
  71.                 }
  72.                 tmp = 0;
  73.             }
  74.             cc = p[crt] + j + 1;
  75.             if(cc < n){
  76.                 if(arr[cc] > mxx1){
  77.                     tmp = arr[cc];
  78.                 }
  79.                 else{
  80.                     cnt[arr[cc]] ++;
  81.                 }
  82.             }
  83.             turn = 1 - turn;
  84.         }
  85.         cout << (A - B) << "\n";
  86.     }
  87.     return 0;
  88. }
  89. /*
  90.  10 1
  91.  10 1 4 2 8 7 3 3 10 2
  92.  10
  93.  
  94.  5 1
  95.  2 4 2 3 5
  96.  3
  97.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement