Sunday, 24 May 2015

Devu and decorating birthday cake ARRANGE BALOONS SO THAT NO TWO BALOON WILL BE ADJECENT

Devu and decorating birthday cake 
Solved

Problem code: REARRSTR

All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian as well.

Today is Devu's birthday. He has obtained few colored balloons from his friends. You are given this information by a string s consisting of lower case English Latin letters. Each letter (from 'a' to 'z') denotes a color. e.g. if s = "aab", then it means that he has received two balloons of color 'a' whereas one balloon of color 'b'.
Now, Devu wants to decorate the cake by arranging all the balloons linearly from left to right on the cake such that no same colored balloons are nearby/ adjacent to each other.
Now Devu wonders whether it is possible to do so or not? Please help him in this. If it is not possible to do so, print -1. Otherwise, print any one of arrangements of the balloons on the cake. If there are more than one possible ways of doing so, you can print any one of them.

Input

  • The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
  • First line of each test case will contain string s

Output

Print a single line corresponding to the answer of the problem.

Constraints

  • 1 ≤ T ≤ 105
  • 1 ≤ size of string s ≤ 105
  • Sum of size of string s over all test cases will be less than or equal to ≤ 106

Example

Input:
3
aab
ab
aa
Output:
aba
ab
-1

Explanation

Example case 1. He can arrange the balloons in the order given by the following string "aba".
Example case 2. He can arrange the balloons in the order given by the following string "ab"
Example case 3. There is no valid way of decorating cakes with balloon in the desired way. So we print -1.



/**********************************************************editorial*******************************************************
  count hash of all ballons and check for the possiblity of answer ..
   if  ballon with colour having maximum count is greater than half of rest all ballons (in case of  even no of ballons )   OR  if no total ballon is odd than maximu>rest/2+1 than answer not exit ..
else
recunstruct an array which consist of  ballons according to frequency -- ex aabbbbbccc  will be recunstructed as bbbbbcccaa .. 
 now contruct ans array whic contain 1 element from start ie index 0  and next from middle , ie.
0th index, mid index ,1st index ,mid+1 index 2 nd index ,mid+2 index and so on ..... 

*******************************************code**************************************************************************
#include<iostream>
using namespace std;
char arr[1000000];
char ans[1000000];

#include<bits/stdc++.h>
 int  main()
 {
  long long int  t; 
  cin>>t;
   while(t--)
    {
    long long int  n;
        cin>>arr;
        long long int  len=strlen(arr);
           //sort(arr,arr+len);
         
           long long int sum=0;
            long long int has[28];
            vector<pair<int,int> >v;
             for(int i=0;i<=27;i++) has[i]=0;
             for(int i=0;i<len;i++)
              {
              has[arr[i]-'a']++;
             
              }
               for(int i=0;i<26;i++)
                {
                v.push_back(make_pair(has[i],i));
                }
              sort(has,has+26);
              
              sort(v.begin(),v.end());
              int top=0;
              vector<pair<int,int> > ::iterator it;
              for( it=v.end();it>=v.begin();it--)
               {
                int c=it->first;
                // cout<<"c is "<<c<<endl;
                 for(int j=0;j<c;j++)
                  {
                  arr[top]=it->second+'a';
                  top++;
                  }
               }
               
               
               
               
              for(int i=0;i<25;i++) sum+=has[i];
           
               if(len%2==1 && has[25]-sum>1)
                cout<<"-1"<<endl;
                else if(len%2==0 && has[25]-sum>0) 
                 {
                  cout<<"-1"<<endl;
                 }
            else
             {
              long long int  mid=0;
              if(len%2==1)
              {
              mid=(len/2)+1;
              }
              else mid=len/2;
              long long int  j=mid;
              long long int  tv=-1;
                for(long long int  i=0;i<mid;i++,j++)
{
 
ans[++tv]=arr[i];
ans[++tv]=arr[j];
}
 for(long long int  i=0;i<len;i++)
   cout<<ans[i];
    cout<<endl;
             }
            
   
    }
    return 0;
 }

Sunday, 10 May 2015

MINIMUM NO NEED TO INSERT TO FORM 1 TO N NUMBERS

                                                          GOOGLE CODEJAM --
 
FIND MINIMUM NO OF NUMBERS NEEDED TO INSERT IN A SET OF GIVEN NUMBERS SUCH THAT ALL NO TILL 'D' CAN BE  FORMED BY SUMMING UP THE NOS OF GIVEN SET AND ADDED NEW ELEMENTS ..
NO NUMBER CAN BE USED TWICE IN FORMING ANY NUMBER ..


EX-
LET GIVEN SET = 1,2 AND WE NEED TO FORM SUMM TILL 5
THAN 1 CAN BE FORMED ,
2 CAN BE FORMRD
3 CAN BE FORMED (1+2)
4 CAT BE FORMED SOO ADD 4 IN SET
5 NOW 5 CAN BE FORMED BY(4+1) SINCE 4 IS ADDED IN THE SET

HENCE NO OF NUMBER INSERTED =1 ( NO IS 4);

-----------------------------------------------------------EDITORIAL-------------------------------------------
  FIRST CALCULATE ALL NOS WHICH CAN BE FORMED FROM GIVEN SET OF NUMBERS ..
 NOW ITERATE FROM 1 TO D AND IN ANY NO IS NOTE FORMED THAN INSERT THAT NO IN THE SET AND ALSO CALCULATE ALL NEW NOS FORMED DUE TO INSERTION OF THIS NEW NO ...

------------------------------------------CODE--------------------------------------------------------------

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <numeric>
#include <utility>
#include <bitset>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
 #define tr(container, it) \
    for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define maX(a,b) (a) > (b) ? (a) : (b)
#define pii pair< int, int >
#define pip pair< int, pii >
#define FOR(i,n) for(int i=0; i<(int)n ;i++)
#define REP(i,a,n) for(int i=a;i<(int)n;i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
//int dx[]= {-1,0,1,0};
//int dy[]= {0,1,0,-1};
int A[105];
int main()
{
// freopen("C-small-attempt0.in","r",stdin);
  //  freopen("C-small-attempt0.out","w",stdout);
    int t;
    scanf("%d",&t);
    int kas = 0;
    while(t--)
    {
    kas++;
    int c,d,v;
        cin >>  d >> v;
        FOR(i,d)
        scanf("%d",&A[i]);// GIVEN SET
        set<int> S;
        S.insert(0);
        for(int i=0;i<d;i++)// CALCULATING ALL NOS THAT CANBE FORMED FROM                                                            //GIVENSET
        {
            std::vector<int> G;
            for(set<int>::iterator it = S.begin(); it != S.end();it++)
            {
                G.pb(*it + A[i]);
            }
            FOR(k,G.size())
            {
                S.insert(G[k]);
            }
        }
     

        int ans = 0;
        for(int i=1;i<=v;i++)
        {
            if(S.find(i) == S.end())// IF NOT FOUND I IN THE SET THAN INSERT IT AND ALL                                                         //NEW NOSTHAT CAN BE FORMED DUE TOTHIS INSERTION
            {
                ans++;
                std::vector<int> G;
                for(set<int>::iterator it = S.begin(); it != S.end();it++)
                {
                    G.pb(*it + i);
                }
                FOR(k,G.size())
                {
                    S.insert(G[k]);
                }
            }
        }
        printf("Case #%d: %d\n",kas,ans);
    }
return 0;
}

Saturday, 9 May 2015

 Dress'em in Vests!

The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y ≥ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x ≤ bj ≤ ai + y.
Input
The first input line contains four integers nmx and y (1 ≤ n, m ≤ 1050 ≤ x, y ≤ 109) — the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) in non-decreasing order, separated by single spaces — the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 ≤ bj ≤ 109) in non-decreasing order, separated by single spaces — the sizes of the available vests.
Output
In the first line print a single integer k — the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (uivi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Sample test(s)
input
5 3 0 0
1 2 3 3 4
1 3 5
output
2
1 1
3 2
input
3 3 2 2
1 5 9
3 5 7
output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped.
---------------   ---------------------------------------------------------EDITORIAL----------------------------------
consider a singel vest can be  provide to any 1 solder only  and vest size and solder requirement is already sorted so .. so start iterating from 1st vest check whether 1 sth vest can fit 1st solder or not if it is valid for 1st solder than increase pointer for both solder and vest ans also increase count of  covered .
else -- if size of vest is small than arr[i]-x than this vest canot be fit to any solder since  i+i th solder demand more bigger vest than ith solder ,, so increase pointer of vest ..
else if size of vest is more than arr[i]+y than ith person cant gat any vest since no vest is available with size less than arr[i]+y so increase pointer of solder ......
------------------------------------------------------------CODE-----------------------------------------------------
#include<iostream>
using namespace std;
typedef long long int lli;
#include<bits/stdc++.h>
int main()
 {
  lli n;
  cin>>n;
  lli m;
   cin>>m;
   lli x,y;
    cin>>x>>y;
   lli arr[n+10];
   lli brr[m+10];
   vector<pair<lli,lli> >v;
    for(int i=0;i<n;i++) cin>>arr[i];
     for(int j=0;j<m;j++) cin>>brr[j];
     lli s=0;
     lli c=0;
     lli cov=0;
      while(s<n && c<m)
       {
        if(arr[s]-x>brr[c]) c++;
        else if(arr[s]+y<brr[c]) s++;
        else
        {
        v.push_back(make_pair(s+1,c+1));
        s++;
        c++;
        cov++;
        }
       }
        cout<<cov<<endl;
        for(int i=0;i<cov;i++)
         {
          cout<<v[i].first<<" "<<v[i].second<<endl;
         }
      return 0;
  }
  

B. Vanya and Lanterns
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns.
Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?
Input
The first line contains two integers nl (1 ≤ n ≤ 10001 ≤ l ≤ 109) — the number of lanterns and the length of the street respectively.
The next line contains n integers ai (0 ≤ ai ≤ l). Multiple lanterns can be located at the same point. The lanterns may be located at the ends of the street.
Output
Print the minimum light radius d, needed to light the whole street. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.
Sample test(s)
input
7 15
15 5 3 7 9 14 0
output
2.5000000000
input
2 5
2 5
output
2.0000000000
Note
Consider the second sample. At d = 2 the first lantern will light the segment [0, 4] of the street, and the second lantern will light segment[3, 5]. Thus, the whole street will be lit.


---------------------------------------DISCREAT BINARY SEARCH SOLUTION--------------------------
  JUST FIX DIST AND CHECK WHETHER THIS DIST CAN COVER ALL POINT IN THE STREAT OR NOT .. INITIALLY LEFT= 0 , RIGHT = MAXIMUM DISTANCE ..
--------------------------------------------------CODE--------------------------------------------------------------
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
typedef long long int lli;
int main()
 {
  lli n,dis;
   cin>>n>>dis;
  lli arr[n+10];
  for(int i=0;i<n;i++)
    cin>>arr[i];
  double  left=0;
  double right =dis;
  sort(arr,arr+n);
  double ans=dis;
   while(right-left>0.0000000001)
    {
     double mid=((double)left+(double)right)/2.0;
       //printf("%.9lf\n",mid);
     int f=0;
     if(double(arr[0]-mid>0.0)) f=1;
     
      for(int i=0;i<n-1;i++)
       {
        if(f==1) break;
        if(((double)arr[i]+mid-(double(arr[i+1])-mid)>=0.0) )
        {
        // cout<<"cover range forard"<<(double)arr[i]+mid<<" backward "<<(double(arr[i+1])-mid);
        continue;
        }
       
        else
        {
        f=1;
        break;
        }
       }
       if((double)arr[n-1]+mid<dis) f=1;
       if(f==1)
        {
        left=mid+(0.00000000001);
        }
        else
        {
        if(ans-mid>0.0) ans=mid;
        right=mid;
        }
    }
     //cout<<ans<<endl;
     printf("%.9lf\n",ans);
 }


----------------------------------------------------NORMAL IMPLEMITATION ---------------------------------
Sort lanterns in non-decreasing order. Then we need to find maximal distance between two neighbour lanterns, let it be maxdist. Also we need to consider street bounds and count distances from outside lanterns to street bounds, it will be (a[0] - 0) and (l - a[n - 1]). The answer will be max(maxdist / 2, max(a[0] - 0, l - a[n - 1]))
Time complexity O(nlogn).
-----------------------------------------------------CODE--------------------------------------------------------
#include <stdio.h>
#include <algorithm>
using namespace std;
int n,i,a[100500],rez,l;
int main()
{
    scanf("%d%d",&n,&l);
    for (i = 0; i < n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    rez = 2*max(a[0],l-a[n-1]);
    for (i = 0; i < n-1; i++)
        rez = max(rez, a[i+1]-a[i]);
    printf("%.10f\n",rez/2.);
    return 0;
}