Tuesday, 19 April 2016

*Railway Tickets


 Railway Tickets


The railway line “Yekaterinburg-Sverdlovsk” with several stations has been built. This railway line can be represented as a line segment, railway stations being points on it. The railway line starts at the station “Yekaterinburg” and finishes at the station “Sverdlovsk”, so stations are numbered starting from “Yekaterinburg” (it has number 1) and “Sverdlovsk” is the last station.
Problem illustration
Cost of the ticket between any two stations depends only on a distance between them. The prices for the tickets are specified in the following table.
distance X between stationsprice for the ticket
0 < X ≤ L1C1
L1 < X ≤ L2C2
L2 < X ≤ L3C3
Direct tickets from one station to another can be booked if and only if the distance between these station does not exceed L3. So sometimes it is necessary to book several tickets to pay for the parts of the whole way between stations.
For example, on the railway line shown at the figure above there are seven stations. The direct ticket from the second station to the sixth one can not be booked. There are several ways to pay for the travel between these stations. One of them is to book two tickets: one ticket at price C2 to travel between the second and the third stations, and other at price C3 to travel between the third and the sixth stations. Note, that though the distance between the second and the sixth stations is equal to 2L2, the whole travel can not be paid by booking two tickets at price C2, because each ticket is valid for only one travel and each travel should start and end only at stations.
Your task is to write a program, that will find the minimal cost of the travel between two given stations.

Input

The first line of the input contains 6 integers L1L2L3C1C2C3 (1 ≤ L1 < L2 < L3 ≤ 109, 1 ≤ C1 < C2 < C3 ≤ 109) in the specified order with one space between. The second line contains the amount of stations N (2 ≤ N ≤ 10000). The third line contains two different integers separated by space. They represent serial numbers of stations, the travel between which must be paid. Next N−1 lines contain distances from the first station (“Yekaterinburg”) on the railway line to others. These distances are given as different positive integers and are arranged in the ascending order. The distance from “Yekaterinburg” to “Sverdlovsk” does not exceed 109. The distance between any neighboring stations does not exceed L3. The minimal travel cost between two given stations will not exceed 109.

Output

Program should print to the output the only number, which is the minimal travel cost between two given stations.

Sample

inputoutput
3 6 8 20 30 40
7
2 6
3
7
8
13
15
23
70
Problem Author: Pavel Zaletsky 
Problem Source: Ural Collegiate Programming Contest '99

-----------------------------------------editorial-------------------------------------------------------------------
  this is a easy dp from each position we call all 3 possible tickets
main tricky  thing is that  in case of a>b swap a and b 

state dp[pos] -  it contain minimum cost needed from pos to the  last station , recursively solve all 3 ways from pos to reach last station and store minimim cose among all options 
-----------------------------------code-------------------------------------------------------------------
#include<bits/stdc++.h>
typedef  long long int lli;
using namespace std;
vector<lli> v;
int len;
lli dp[100000];
lli l1,l2,l3,c1,c2,c3;
int ct=0;
lli solve(lli index)
 {
 
  if(index>=len) return 0;
  
   if(dp[index]!=-1)
   {
   
    return dp[index];
   
 
   else
    {
       lli pos=v[index];
      
       lli ret=1000000000000000000;
       
       vector<lli> :: iterator it;
           // using L1
       it=lower_bound(v.begin(),v.end(),pos+l1);
       
       if(it==v.end())
       {
         ret=min(ret,c1);
 }
 else 
  {
     if(*it>pos+l1)
     
     it--;
     int p=it-v.begin();
    
     if(p!=index)
    ret=min(ret,solve(p)+c1);
  }
  
  
  
  
  // using L2
  
  it=lower_bound(v.begin(),v.end(),pos+l2);
       if(it==v.end())
       {
         ret=min(ret,c2);
 }
 else 
  {
   if(*it>pos+l2)
     it--;
    
     int p=it-v.begin();
  
        if(p!=index)
    ret=min(ret,solve(p)+c2);
  }
  
  using L3
  it=lower_bound(v.begin(),v.end(),pos+l3);
       if(it==v.end())
       {
         ret=min(ret,c3);
 }
 else 
  {
   if(*it>pos+l3)
     it--;
     int p=it-v.begin();
  
        if(p!=index)
    ret=min(ret,solve(p)+c3);
  }
      
       dp[index]=ret;
       return ret;
   }
 }
 
 
int main()
{

 cin>>l1>>l2>>l3>>c1>>c2>>c3;
 lli a,b;
   lli n;
 cin>>n;
  cin>>a>>b;
  if(a>b)
   {
     lli c=a;
     a=b;
     b=c;
   }
   if(a==1) v.push_back(0);
   for(int i=0;i<=n;i++) dp[i]=-1;
  for(int i=1;i<=n-1;i++)
   {
     lli  val=0;
     cin>>val;
   
     if(i>=a-1 && i<=b-1)
     v.push_back(val);
   }
  len =v.size();

  
    dp[len-1]=0;
    lli ans=solve(0);
    cout<<ans<<endl;
     
  return 0;
}


The colorful street


There is a street by the name of colorful street in the Pretty Town. The residents of the house have decided that they will paint their houses in either Pink, Orange or Yellow color and not other. They have also decided that no two adjacent houses will have the same color. For house i, i-1 and i+1 are the neighbors and note that the first and the last house are not neighbors.
The cost of painting a house in a particular color is different. The cost of painting the first house in color yellow maybe different than what its for painting the second house in the same color.
You have to find the minimum of cost of painting the houses which satisfy the given condition.
Input Constraints - Number of houses will contain between 1 and 20 elements, inclusive. - Each line of input for houses will have three values for cost of coloring in each color. Each value will be between 1 and 1000.
Input Format - The first line will contain the number of testcases - T. - The second line will contain an integer N that will specify how many houses are there. - Each of the following N lines will contain 3 numbers separated by space that represents the cost of painting that house in either pink, orange or yellow color.
Output Format Print T lines showing the cost of painting the houses for each testcase.

Sample Input
(Plaintext Link)
1
2
11 12 13
14 15 16
Sample Output
(Plaintext Link)
26
Explanation
The first house should be painted in pink (11), the second should be painted in orange (15). So the total cost becomes 11+15 = 26

Time Limit: 2 sec(s) for all input files combined.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed languages: C, CPP, CLOJURE, CSHARP, GO, HASKELL, JAVA, JAVASCRIPT, JAVASCRIPT_NODE, LISP, OBJECTIVEC, PASCAL, PERL, PHP, PYTHON, RUBY, R, RUST, SCALA5 =
--------------------------------------------------CODE----------------------------------------------------------------------------------
#INCLUDE<BITS/STDC++.H>
USING NAMESPACE STD;

INT MAIN()
 {
  INT T;
   CIN>>T;
   WHILE(T--)
    {
      INT N;
      CIN>>N;
      INT DP[N+1][3];
      VECTOR<PAIR<PAIR<INT,INT>,INT> >V;
      FOR(INT I=0;I<N;I++)
      {
        INT A,B,C;
         CIN>>A>>B>>C;
         V.PUSH_BACK(MAKE_PAIR(MAKE_PAIR(A,B),C));
      }
       DP[0][0]=V[0].FIRST.FIRST;
       DP[0][1]=V[0].FIRST.SECOND;
       DP[0][2]=V[0].SECOND;
       FOR(INT I=1;I<N;I++)
        {
          DP[I][0]=MIN(DP[I-1][1],DP[I-1][2])+V[I].FIRST.FIRST;
          DP[I][1]=MIN(DP[I-1][0],DP[I-1][2])+V[0].FIRST.SECOND;
          DP[I][2]=MIN(DP[I-1][0],DP[I-1][1])+V[0].SECOND;
        }
        COUT<<MIN(MIN(DP[N-1][0],DP[N-1][1]),DP[N-1][2])<<ENDL;
    }
    
 
 }

CXZBCXZ

Once upon a time in Time-Land




Once upon a time in Time-Land

In a mystical TimeLand, a person's health and wealth is measured in terms of time(seconds) left. Suppose a person there has 24x60x60 = 86400 seconds left, then he would live for another 1 day. A person dies when his time left becomes 0. Some time-amount can be borrowed from other person, or time-banks. Some time-amount can also be lend to another person, or can be used to buy stuffs.
Our hero Mr X, is in critical condition, has very less time left.
Today's the inaugural day of a new time-bank. So they are giving away free time-amount worth 1000 years.
Bank released N slips, A[1], A[2], .... A[N]. Each slip has a time-amount(can be +ve as well as -ve).
A person can pick any number of slips(even none, or all of them, or some of them) out of the N slips. But bank introduced a restriction, they announced one more number K. Restriction is that, if a person picks a slip A[i], then the next slip that he can choose to pick will be A[i+K+1]. It means there should be a difference of atleast K between the indices of slips picked.
Now slip(s) should be picked in such a way that their sum results in maximum positive time-amount sum possible with the given restriction.
If you predict the maximum positive sum possible, then you win.
Mr X has asked for your help. Help him win the lottery, and make it quick!
Input Format:
First line of the test file contains single number T, the number of test cases to follow.
Each test case consists of two lines.
First line contains two numbers N and K , separated by a space. Second line contains the N numbers A[1], A[2] ..... A[N]separated by space.
Output Format:
For every test case, output in a single line the maximum positive sum possible, that is output for the case.
Constraints:
T ≤ 250
N ≤ 10000
-109 ≤ A[i] ≤ 109
0 ≤ K ≤ N-1

Sample Input
(Plaintext Link)
2
10 1
1 2 -3 -5 4 6 -3 2 -1 2
10 2
1 2 -3 -5 4 6 -3 2 -1 2
Sample Output
(Plaintext Link)
12
10
Explanation
1st Case: We can take slips { A[2]=2, A[6]=6, A[8]=2, A[10]=2 }, slips are atleast 1 indices apart this makes maximum sum, A[2]+A[6]+A[8]+A[10]=12
2nd Case: We can take slips { A[2]=2, A[6]=6, A[10]=2 }, slips are atleast 2 indices apart this makes maximum sum, A[2]+A[6]+A[10]=10
-----------------------------------------editorial------------------------------------------------------------------------------------------------------
Just like all dp problems we first need to define state here.
We take the state as f (idx) where f (idx) will tell us the current maximum until index idx.
Base case will be when f (idx >= N) which is 0 where N is length of the given cost array.
It is a knapsack style dp, where for every index we make a choice whether to take it or leave it.
If we leave it, next state will be f (idx + 1). If we take it, next state will be f (idx + K + 1) and we add the cost of ith item to the overall cost.
--------------------------------------------------------code-------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli  dp[400000];
int mark[100000];
 int n,k;
 lli arr[100000];
 lli  solve(int index)
 {
  if(index>=n) return 0;
   if(mark[index]!=0)
    {
     return dp[index];
   }
   else
   {
    mark[index]=1;
    lli ret=0;
      dp[index]=max(solve(index+1),solve(index+k+1)+arr[index]);
      return dp[index];
   }
   
 }
int main()
 {
  int t;
   cin>>t;
   while(t--)
   {
    cin>>n>>k;
    memset(dp,0,sizeof(dp));
    memset(mark,0,sizeof(mark));
    for(int i=0;i<n;i++)
     {
       cin>>arr[i];
}
 
 lli  ans=solve(0);
 
 cout<<ans<<endl;
  }
    
 }