Showing posts with label Analysis. Show all posts
Showing posts with label Analysis. Show all posts

Thursday, June 28, 2012

[UVa] 11456 - Trainsorting


Method:
The problem at first glance seems too straightforward but it's not that much. Think a bit about the lines "Erin can add it to the beginning or end of her train, or refuse to add it at all. The resulting train should be as long as possible, but the cars within it must be ordered by weight.". What I understand from these lines is.

1. Erin can choose to start making the train from any point of the arrivals of the cars. So she may have started to make the train from either 1st or n-1st or nth or n/2nd, any point.
2. And from the point in time where she starts this, she checks if the new car is smaller in weight than the last car of her train or higher in weight than the first car or vice versa.
3. So the sorting can start from any point and the optimal choice is at any point.
Ofcourse you have to use LIS, but you also need LDS. Why??

Because suppose she starts from position i. Since she wants to make the train as long as possible so she must make sure the train is optimal from the last till i. Then after i she wants to join the longest possible train using the cars i+1 to n. So, she wants to join the LIS(1 -> i) with LDS(i+1 -> n).

In my solution, I subtracted 1 from LIS(i)+LDS(i) because in both the i car is present. So erased it from 1 of them.
// BISMILLAHIRRAHMANIRRAHIM

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <vector>
#include <list>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <stack>

#define swp(type, a, b) {type x=a; a=b; b=x;}

typedef long long lint;
typedef unsigned long long ulint;

using namespace std;

int seq[10000];
int lis[10000], lds[10000];

int LIS(int n) {
 
 int i, j, mx=0;
 
 
 for (i=n-1 ; i>=0 ; i--) {
  lis[i] = 1;
  for (j=i+1 ; j<n ; j++) {
   if (seq[i]<seq[j]) {
    lis[i] = max(lis[i] , 1+lis[j]);
   }
  }
  mx = max(mx, lis[i]);
 }
 return mx;
}

int LDS(int n) {
 
 int i, j, mx=0;
 
 
 for (i=n-1 ; i>=0 ; i--) {
  lds[i] = 1;
  for (j=i+1 ; j<n ; j++) {
   if (seq[i]>seq[j]) {
    lds[i] = max(lds[i] , 1+lds[j]);
   }
  }
  mx = max(mx, lds[i]);
 }
 return mx;
}



int main ( void ) {
 
 int i, kount, kase, n, x, mx, mnn, mxx, len, res;
 
 
 
 cin >> kase;
 
 while (kase--) {
  cin >> n;
  mx = n;
  
  len = 0;
  kount = 0;
  while (mx--) {
   cin >> x;
   seq[kount++] = x;
  }
  
  LDS(n); LIS(n);
  
  res = 0;
  
  for (i=0 ; i<n ; i++) {
   res = max(res, lis[i]+lds[i]-1);
  }
  
  
  /*cout << "{ debug-start" << endl;
  for (i=0 ; i<n ; i++) {
   cout << seq[i] << " ";
  } cout << endl;
  for (i=0 ; i<n ; i++) {
   cout << lis[i] << " ";
  } cout << endl;
  for (i=0 ; i<n ; i++) {
   cout << lds[i] << " ";
  } cout << endl;
  cout << "end-debug }" << endl;*/
  
  cout << res << endl;
 }
 
 return 0;
}

Thursday, April 05, 2012

[UVa] 10903 - Rock-Paper-Scissors Tournament

The common mistake is there are not K games. There are K*N*(N-1)/2 Games. And Win Average is undefined when W+L = 0.

Show/Hide Code

Wednesday, October 26, 2011

[UVa] 11000 - Bee

This is all about careful reading and a very bit of analysis.
First analyze what happens with a Male bee. You'll see every year the number of bees in that tree is a Fibonacci number, since all the past bees are dead by then.
Then for a Female be, it's actually the same, because it gives birth to a Male bee the next year. And since the special bee produces one Male bee each year, so the result is actually the sum of Fibonacci numbers till n.

#include <iostream>
#include <cmath>
using namespace std;
long long sum[100], fibs[100];
int fib( ) {
    int i;
    long long lim = (long long)pow(2.00,32);

    fibs[0]=0LL; sum[0]=0LL;
    fibs[1]=1LL; sum[1]=1LL;
    fibs[2]=1LL; sum[2]=2LL;
    for (i=3 ; sum[i-1]<lim ; i++) {
        fibs[i]=fibs[i-1]+fibs[i-2];
        sum[i]=sum[i-1]+fibs[i];
    }

    return i;
}

int main( ) {
    fib();
    int n;
    while (cin >> n && n>=0)
    {
        cout << sum[n] << " " << sum[n+1] << endl;
    }
    return 0;
}

Monday, October 17, 2011

[UVa] 278 - Chess

Analyze all 4 cases and soon you'll equations for all of them. The given advantage is the lower limit of 4. Below that the derived theorems fail.

#include <stdio.h>
#include <string.h>
#define min(a,b) (a<b?a:b)
char inp[100];
int main()
{
    int test, x, y;
    char a;
    scanf("%d",&test);
    getchar();
    while (test--)
    {
        gets(inp);


        sscanf(inp,"%c %d %d",&a,&x,&y);
        if (a=='k')
        {
            printf("%d\n",(x*y+1)/2);
        } else if (a=='K')
        {
            printf("%d\n",((x+1)/2) * ((y+1)/2));
        } else if (a=='Q'||a=='r')
        {
            printf("%d\n",min(x,y));
        }
    }
    return 0;
}

Friday, September 09, 2011

[UVa] 10642 - Can you solve it?

Analysis:
Draw the coordinate system but start from 1 not 0, just to escape negative value problems. Draw it like a triangle where the top has 1. Now see that on the right side, every i-th number is the sum of all numbers till i.
1 = 1x(1+1)/2
3 = 2x(2+1)/2
6 = 3x(3+1)/2
10 = 4x(4+1)/2
15 = 5x(5+1)/2
So, given the x coordinate you can find the first number in that row using X*(X+1)/2, BUT make X=X+1 first.
To find the Y-th element (in my case (Y+1)-th, you can use the formula
S = a + (a+d) + (a+2d) + ... + [a + (n-1)d] = n[2a + (n-1)d]/2
But you have to modify it a bit first. This is derived from the analysis that every number in a row has some initial numbers lost except the first row. In X=2, 2 is not in the series, in X=3, 3 and 4 are not in the series. This keeps increasing by 1 at every i. So the series is
Y[0], Y[0]+1x1, Y[0]+1x2, Y[0]+1x3 ......
So you can simply use the formula with a=0 and n=Y+(X-1). Just add X to that.
Find both the numbers this way and print their difference. Done!!!.


A useful link for Summation formulas

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <vector>
#include <climits>
#include <set>
#include <algorithm>
#define MI INT_MAX
#define ULONG unsigned long long
#define LLONG long long
#define swap(a,b) {int t=a ; a=b ; b=t; }
#define sz(a) sizeof(a)
#define FOR(i, a, b) for (i=a ; i<b ; i++)
#define QSORT(a,n,s,f) qsort(a,n,sizeof(s),f)
using namespace std;
int sum(int a, int n)
{
    return n*(2*a+(n-1)*1)/2;
}
int main()
{
    int test, kase=1, x1, x2, y1, y2, num1, num2, temp, n, a;
    scanf("%d",&test);
    while (test--)
    {
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        x1++; x2++; y1++; y2++;


        num1 = x1+(sum(0,y1+(x1-1)));
        num2 = x2+(sum(0,y2+(x2-1)));


        printf("Case %d: %d\n",kase++,num2-num1);
    }
    return 0;
}

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...