Tuesday, 15 March 2016

C. Hamburgers

C. Hamburgers
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.
Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.
Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.
Input
The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).
The second line contains three integers nbnsnc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pbpspc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the%I64d specifier.
Output
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.
Examples
input
BBBSSC
6 4 1
1 2 3
4
output
2
input
BBC
1 10 1
1 10 1
21
output
7
input
BSC
1 1 1
1 1 3
1000000000000
output
200000000001

----------------------------------------------------------editorial---------------------------------------------
Let's use binary search approach. For given number of hamburgers (say, x) let's find the minimal number of money needed to cook them. Say, for one hamburger Polycarpus needs cb bread pieces, cs sausages pieces, cc cheese pieces. So for x hamburgers he needs: cb·xcs·x and cc·x pieces (by types). Since he already has nbns and nc pieces, so he needs to buy:
  • bread: max(0, cb·x - nb),
  • sausages: max(0, cs·x - ns),
  • cheese: max(0, cc·x - nc).
So the formula to calculate money to cook x hamburgers is:
f(x) = max(0, cb·x - nbpb + max(0, cs·x - nsps + max(0, cc·x - ncpc
Obviously, the function f(x) is monotonic (increasing). So it is possible to use binary search approach to find largest x such that f(xler
-------------------------------------------code--------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int arr[20][20];
typedef long long int lli;
lli dp[1<<15];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(dp,-1,sizeof dp);
int n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
  {
 
 cin>>arr[i][j];
  }
 }
 lli ret=1000000000000;
 for(int i=0;i<n;i++)
 {
  ret=min(ret,solve(mask |(1<<i) )+arr[i][i]);
 }
 cout<<ret<<endl;
}
}

Tuesday, 8 March 2016

*Phoebe's Melody

Phoebe's Melody
Phoebe enjoys playing music. She especially enjoys playing it for her friends.
Phoebe has made a new musical instrument. The instrument is very much like a piano. It has N keys arranged in a straight line, numbered from 1 to N. The ith key has volume Vi. No two keys have the same volume and 1 ≤ Vi ≤N. It takes |i-j| time to move from the ith key to the jth key on the instrument. Phoebe has a unique way of playing music. Immediately after playing key i, she can play only a key j such that:
  • j is not closer than K positions from key i (i.e. j should not be in the range [ i-K+1, i+K-1 ]).
  • Vj < Vi.
Each key may have 0 or more keys that can be played immediately after it.
Phoebe wants to find the summation of time required to go from each key i to the closest key that can be played after it. If there is no next playable key for a key i, then consider its time taken as 0.
Input:
The first line of the input contains T, the number of test cases.
The first line of each test case contains N and K. The second line of each test case contains N integers of the array V.
Output:
For each test case, output a single number denoting the summation of time taken to move from each key i to the closest next playable key after i.
Constraints:
  • 1 <= T <= 10
  • 1 <= N <= 2 * 105
  • 1 <= K,V[i] <= N
SAMPLE INPUT
 
3
2 1
1 2 
5 1
3 5 4 2 1 
5 4
1 2 3 4 5
SAMPLE OUTPUT
 
1
6
4
Explanation
Second test case: The next playable keys for: 1 is { }. Closest=none, so time taken = 0 2 is { 1 }. Closest=1, so time taken = 1 3 is { 1 , 2 }. Closest=2, so time taken = 3 4 is { 1 , 2 , 3 }. Closest=2, so time taken = 1 5 is { 1 , 2 , 3 , 4 }. Closest=3 or 4, so time taken = 1 Total time taken is 6
Third test case: There is no key in range for keys 1-4. The next playable keys for: 1 is { } 2 is { } 3 is { } 4 is { } 5 is { 1 }. Closest = 1, So time taken = 4 Total time taken is 0+0+0+0+4=4

----------------------------------------------------------------------------EDITORIAL--------------------------------------------------
THIS IS A simple lower bound concept , but we need to do it ins a good way, a number can only pair with a number which is at a  distance k from the numbers and must be less than the number soo lets process number in the sorted order , and after processing ans  of a number , insert index of this number , so that when a new number comes that number  must be > than this number , and it can use this number,s index to pair with this number , we insert it in a set so that it  indexes will be in a sorted order so that we can easily find  first index near to index i ie(<=i-k  and >i+k)

------------------------------------------------------------------code----------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;

int main()
 {
  int t;
   cin>>t;
   while(t--)
   {
    vector<pair<int,int> > v;
        set<int> vs;
    int n,k;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++)
     {
      int a;
      scanf("%d",&a);
      v.push_back(make_pair(a,i));
     
}
sort(v.begin(),v.end());
long long int ans=0;
vs.insert(v[0].second);
for(int i=1;i<n;i++)
 {
  int temp=100000000;
  int num=v[i].first;
  int pos=v[i].second;
  int bb=pos-k;
  int ff=pos+k;
  set<int>:: iterator back,front;
   
  back=vs.lower_bound(bb);
  int f=0;
  if((back==vs.end() || *back>bb))
  {
  if(back==vs.begin()) f=1;
  back--;
  }
 
  if(f==0 && bb>=1)
    {
    temp=abs(pos-*back);
}
  front=vs.lower_bound(ff);
  if(front!=vs.end()  && ff<=n)
   {
    temp=min(temp,abs(pos-*front));
}
if(temp!=100000000)
{
ans+=temp;
}
vs.insert(pos);
 }
  printf("%lld\n",ans);
 
  }
 }

Monday, 7 March 2016

*C. To Add or Not to Add


C. To Add or Not to Add
A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.
However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).
Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 1050 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.
The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.
Output
In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.
Examples
input
5 3
6 3 4 0 2
output
3 4
input
3 4
5 5 5
output
3 5
input
5 3
3 1 2 2 1
output
4 2
Note
In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.
In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.
In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.

-------------------------------------------editorial---------------------------
One of the main observations, needed to solve this problem, is that the second number in answer always coincides with someone aj. Let's see why it is true. Suppose, the second number of the answer is aj + d for someone j and aj + d ≠ ai for all i. This means, we increased some numbers, which is less than aj, so that they became equal to aj, and then all this numbers and some numbers, which is equal to aj, we increased to aj + d. But if we didn't increase all this numbers to aj + d and remain they equal to aj, we'd perform less operations and the answer would be better.
Due to this fact we can solve problem in a such manner. Sort array in non-decreasing order. Iterate over ai and calculate, what is the maximal number of ai we can obtain. For maximizing first number of answer, we must increase some lesser numbers to ai and perform not greater than k operations. It is obvious that firstly we should increase such aj that aiaj is minimal. So, if we can solve problem inO(n2), we would iterate j from i to 0 and increase aj to ai, while we could. But the solution must be faster, and we will use binary search. We will brute the number of numbers, which we must do equal to ai. Suppose we fix cnt this value. Now we have to check if we can do cnt numbers equal to ai by not greater than k operations. For doing this, let’s calculate . If this value not greater than k, we can do it. For calculating sum quickly, we can save prefix sums and than si - cnt + 1, i = sisicnt. Finally we solved this problem in O(n·logn).
--------------------------------------------------code------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli arr[1000000];
vector<lli>v;
lli fs[1000000];
int main()
 {
  int n;
  lli k ;
  cin>>n>>k;
  for(int i=0;i<n;i++) 
 {
  lli a;
  cin>>a;
  v.push_back(a);
 }
  sort(v.begin(),v.end());
   
  for(int i=1;i<=n;i++)
   {
   
    fs[i]=fs[i-1]+v[i-1];
   // cout<<fs[i]<<" ";
  }
  
  int maxi=0,len=0;
  lli val=0;
  
  for(int i=1;i<=n;i++)
   {
   
    int l=1,r=i;
    len=0;
    int mx=32;
    while(mx--)
     {
      if(l>r) break;
      int mid=(l+r)/2;
      if((fs[i]-fs[mid-1]+k)>=(v[i-1]*(i-mid+1)))
      {
      r=mid;
      len=i-mid+1;
  }
  else
  {
  l=mid+1;
  }
  
 }
 if(len>maxi)
  {
  maxi=len;
  val=v[i-1];
  }
}
cout<<maxi<<" "<<val<<endl;
 }