Showing posts with label adhoc. Show all posts
Showing posts with label adhoc. Show all posts

Tuesday, 2 January 2018

SPOJ Problem ABSP1 Solution in (C++)

Welcome:

The question to the problem can be found here:


Explanation:
1.Notice the array is sorted already.
2. It can be solved with simple Math.
3. There are two logic only.
4. Multiply each element  i -th times(i-th times: position of element in the array)
5. Multiply each element n-1-i -th times.
6. Subtract 4 and 5.
7.Happy Coding !

Code:

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

long long ABS(long long a[],long long n){
    long long sum=0;
rep(i,n){
sum += (a[i]*i) - (a[i]*(n-1-i));
}
return sum;
}
int main(){
 //   freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
int t;
long long arr[10000];
cin>>t;
while(t-->0){
long long n;
cin>>n;
rep(i,n){
    cin>>arr[i];
}
cout<<ABS(arr,n)<<endl;

}

return 0;}

Sunday, 3 September 2017

UVa Problem 272 - TEX Quotes Solution (C++)

Problem:

Please find the problem here.

Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Code:

#include <bits/stdc++.h>
using namespace std;
main(){
    
    int c =0;
string s;
while(getline(cin,s)  ){
    while(s.find('"')+1){
    if(c==0  ){
        s.replace(s.find('"'),1,"``");
        c=1;
    }
    else{
             s.replace(s.find('"'),1,"''");
        c=0;
    }
    }
    cout<<s<<endl;
      
}




}

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...