652-C Problem. Please Help me find my mistake.

Правка en1, от minimum_guy, 2020-06-24 16:48:24

/*I don't know why I am getting segmentation Fault. Please suggest me the changes to run my code.*/ //Enjoy the process of transformation. Don't worry about results......

include<bits/stdc++.h>

using namespace std;

define ll unsigned long long int

define ultraboost ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

define rep(i,a,b) for(ll i=a;i<b;i++)

define repn(i,a,b) for(ll i=a;i>=b;i--)

define pb push_back

bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true;

// This is checked so that we can skip 
// middle five numbers in below loop 
if (n % 2 == 0 || n % 3 == 0) 
    return false; 

for (int i = 5; i * i <= n; i = i + 6) 
    if (n % i == 0 || n % (i + 2) == 0) 
        return false; 

return true;

}

ll com(ll n, ll a) { ll result=1; if(n-a<a) a= n-a; ll cnt=1; rep(i,n-a+1,n+1) { result*= i; result/= cnt; cnt++; } return result; } void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place  
for (j = 0; j < n-i-1; j++)  
    if (arr[j] > arr[j+1])  
        swap(&arr[j], &arr[j+1]);

}

int main() { ultraboost; ll test; cin>>test; while(test>0) { ll n, k; cin>>n>>k; ll a[n], w[k]; ll neg=0, pos=0; rep(i,0,n) { cin>>a[i]; } sort(a, a+n); rep(i,0,k) { cin>>w[i]; } sort(w,w+k); ll p=0, j=n-1, z=0; ll maxa=0, mini=0, sum=0, h=n-1, count=0;

//rep(i,0,k)
    while(w[p]==1 && p<=n-1 && j>=0)
    {
        //cout<<p<<"\n";
        maxa= a[j];
        mini= a[j];
        j--;
        p++;
        //count++;
        sum+= maxa + mini;
        //cout<<sum<<" ";
    }
    //cout<<p<<"\n";
    for(int i=k-1;i>=p;i--)
    {   //sort(a,a+n-1-j);
            maxa= a[j];
            mini= a[z];
            j--;
            z+=w[i]- 1;
            sum+= maxa + mini;
            //cout<<sum<<" ";
    }
    cout<<sum;
    cout<<"\n";
test--;       
}

}

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский minimum_guy 2020-06-24 17:09:36 2451
en1 Английский minimum_guy 2020-06-24 16:48:24 2603 Initial revision (published)