Advertisement
Guest User

xx

a guest
Jan 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. #include<vector>
  4. #include<algorithm>
  5. #include<cstdio>
  6. using namespace std;
  7.  
  8. int X[1000002];
  9. long long T[1000000002];
  10. long long Q[1000000002];
  11. long long A[1000002];
  12. vector<int> indexesof1;
  13. int N;
  14. int Z;
  15. long long possible1; //how many teams are mekable
  16. int maxi=1;
  17. unsigned long long a = 1;
  18.  
  19. long long teamcheck(int previousindexof1, int currentindexof1, int nextindexof1);
  20.  
  21. int main() {
  22.    
  23.     ios_base::sync_with_stdio(0);
  24.     srand(time(NULL));
  25.     scanf("%d", &Z);
  26.    
  27.     for(int z=0; z<Z; z++){
  28.        
  29.        
  30.             scanf("%d",&N);
  31.             a <<= 60; //huge prime num
  32.        
  33.  
  34.        
  35.             for(int n = 0; n < N; n++){
  36.                
  37.                 scanf("%d",&X[n]);  //input
  38.                
  39.                 if(X[n]==1){
  40.                     indexesof1.push_back(n); //adding indexes on which I can find 1's
  41.                     possible1++;
  42.                 }
  43.                
  44.                
  45.                 maxi=max(maxi, X[n]); //checking for the maximum num in this array
  46.             }
  47.        
  48.             for(int i=1; i<=maxi; i++){
  49.                
  50.                 T[i]=rand()%a; //weird random nums assigned to numbers in this array
  51.             }
  52.        
  53.        
  54.             Q[1]=T[1];
  55.        
  56.             for(int k = 2; k <= maxi; k++)
  57.                 Q[k]=Q[k-1]+T[k]; //prefix sums of weird random nums
  58.        
  59.        
  60.             A[0]=T[X[0]];
  61.        
  62.             for(int i=1; i<N; i++){
  63.                
  64.                 A[i]=A[i-1]+T[X[i]];  //prefix sums of numbers in this array
  65.                
  66.             }
  67.        
  68.             for(int i=0; i<indexesof1.size(); i++){
  69.                
  70.                 int previousindexof1;
  71.                 int currentindexof1;
  72.                 int nextindexof1;
  73.                
  74.                 if(i==0){
  75.                    
  76.                     previousindexof1=0;
  77.                     currentindexof1=indexesof1[i];
  78.                     nextindexof1=indexesof1[i+1];
  79.                 }
  80.                 else if(i==indexesof1.size()-1){
  81.                    
  82.                     previousindexof1=indexesof1[i-1];
  83.                     currentindexof1=indexesof1[i];
  84.                     nextindexof1=N-1;
  85.                 }
  86.                 else{
  87.                    
  88.                 previousindexof1=indexesof1[i-1];
  89.                 currentindexof1=indexesof1[i];
  90.                 nextindexof1=indexesof1[i+1];
  91.                    
  92.                 }
  93.                
  94.                 possible1+=teamcheck(previousindexof1, currentindexof1, nextindexof1);
  95.                
  96.             }
  97.        
  98.         printf("%lld \n", possible1);
  99.     }
  100.    
  101.    
  102.    
  103.    
  104.    
  105.    
  106.    
  107. }
  108.  
  109. long long teamcheck(int previousindexof1, int currentindexof1, int nextindexof1){
  110.    
  111.     long long possible=0;
  112.    
  113.     int j=1;
  114.     while(previousindexof1+j<currentindexof1){
  115.        
  116.         //from left side for given indexesof1[i]
  117.         if(X[previousindexof1+j]<X[previousindexof1+j+1]){
  118.            
  119.             if(previousindexof1+j+X[previousindexof1+j+1]-1<N){
  120.                
  121.                 if(A[previousindexof1+j+X[previousindexof1+j+1]-1]-A[previousindexof1+j-1]==Q[X[previousindexof1+j+1]])
  122.                     possible++;
  123.                
  124.             }
  125.         }
  126.         else if(X[previousindexof1+j]>X[previousindexof1+j+1]){
  127.            
  128.             if(previousindexof1+j+X[previousindexof1+j]-1<N){
  129.                
  130.                 if(A[previousindexof1+j+X[previousindexof1+j]-1]-A[previousindexof1+j-1]==Q[X[previousindexof1+j]])
  131.                     possible++;
  132.                
  133.         }
  134.            
  135.     }
  136.         j++;
  137.  }
  138.     int k=1;
  139.     while(nextindexof1-k>currentindexof1){
  140.        
  141.         //from right side for given indexesof1[i]
  142.         if(X[nextindexof1-k]<X[nextindexof1-k-1]){
  143.            
  144.             if(nextindexof1-k-X[nextindexof1-k-1]>=0){
  145.                
  146.                 if(A[nextindexof1-k]-A[nextindexof1-k-X[nextindexof1-k-1]]==Q[X[nextindexof1-k-1]])
  147.                     possible++;
  148.                
  149.             }
  150.         }
  151.         else if(X[nextindexof1-k]>X[nextindexof1-k-1]){
  152.            
  153.             if(nextindexof1-k-X[nextindexof1-k]>=0){
  154.                
  155.                 if(A[nextindexof1-k]-A[nextindexof1-k-X[nextindexof1-k]]==Q[X[nextindexof1-k]])
  156.                     possible++;
  157.                
  158.             }
  159.            
  160.         }
  161.         k++;
  162.     }
  163.     return possible;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement