Poj Solution 2019

http://poj.org/problem?id=2019

//* @author: 82638882@163.com
import java.io.*;
import java.util.Arrays;
public class Main
{
    
 public static void main(String[] args) throws IOException
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  String[] ss=in.readLine().split(" ");
  int a=Integer.parseInt(ss[0]);
  int b=Integer.parseInt(ss[1]);
  int c=Integer.parseInt(ss[2]);
  int[][] p=new int[a][a];
  for(int i=0;i< a;i++)
  {
   ss=in.readLine().split(" ");
   for(int j=0;j< a;j++)
     p[i][j]=Integer.parseInt(ss[j]);
  }
  while((c--)!=0)
  {
   ss=in.readLine().split(" ");
   int x=Integer.parseInt(ss[0]);
   int y=Integer.parseInt(ss[1]);
   int max=-99999,min=99999;
   for(int i=x-1;i< x+b-1;i++)
   {
     for(int j=y-1;j< y+b-1;j++)
     {
    if(p[i][j]>max)max=p[i][j];
      if(p[i][j]< min) min=p[i][j];
    }
      }
      System.out.println(max-min);
    }
  }
}
							
Posted in poj | Leave a comment

Poj Solution 2018

http://poj.org/problem?id=2018

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
    
  static int n,k;
  static final int N = 100000+100,que[] = new int[N];
  static double DP[] = new double[N] , sum[] = new double[N];
    
  public static int Get_Num(StreamTokenizer cin) throws Exception{
    cin.nextToken();
    return (int)cin.nval;
  }
  
  public static void main(String []args) throws Exception{
        
   int i,j;
   double temp;

   StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        
    //n = cin.nextInt();
    //k = cin.nextInt();
    n = Get_Num(in);
    k = Get_Num(in);
    sum[0] = 0.0;
    for(i=1;i<=n;++i){
        temp = (double) Get_Num(in);
        temp*=1000.0;
        sum[i] = sum[i-1]+temp;
    }
    i = (int) solve();
    System.out.println(i);
    }
    
   public static boolean G(int a,int b,int c){
    if((sum[c]-sum[a])*(b-a)<=(sum[b]-sum[a])*(c-a))
        return true;
    return false;
   }

  public static int solve(){
        
   double Max;
   int left=0,right=0,i,j;
   for(i=k;i<=n;++i){
    while(right-left>=2 && G(que[right-2],que[right-1],i-k))
        --right;
    que[right++] = i-k;
        
    DP[i] = (sum[i] - sum[que[left]])/(i-que[left]);
    while(left< right-1 && (DP[i]<=((sum[i]-sum[que[left+1]])/(i-que[left+1])))){
        left++;
        DP[i] = (sum[i] - sum[que[left]])/(i-que[left]);
    }
    }
    for(Max=0.0,i=1;i<=n;++i)
        Max = Max>DP[i]?Max:DP[i];
    return (int)Max;
  }
}

							
Posted in poj | Leave a comment

Poj Solution 2017

http://poj.org/problem?id=2017

import java.io.BufferedInputStream;   
import java.util.Scanner;   
  
/**  
 *poj2017  
 * @author NC  
 */  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in)); 

  
        while (scan.hasNext()) {   
            int n = scan.nextInt();   
            if (n == -1) {   
                break;   
            }   
            int lastTime = 0;   
            int sum = 0;   
            for (int i = 0; i < n; i++) {   
                int speed = scan.nextInt();   
                int time = scan.nextInt();   
                sum = sum + speed * (time - lastTime);   
                lastTime = time;   
            }   
            System.out.println(sum + " miles");   
        }   
    }   
}  
							
Posted in poj | Leave a comment

Poj Solution 2014

http://poj.org/problem?id=2014

//* @author: 
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int max_width;
while((max_width=cin.nextInt())!=0){
   int final_width=0;
   int final_height=0;
   int remain=max_width;
   int cur_height=0;
   int r_width=cin.nextInt();
   int r_height=cin.nextInt();
   while(r_width!=-1){
      if(r_width<=remain){
      remain-=r_width;
      if(final_width< max_width-remain)
         final_width=max_width-remain;
      if(cur_height< r_height)
         cur_height=r_height;
      }
      else{
      final_height+=cur_height;
      cur_height=r_height;
      remain=max_width-r_width;
      }
    r_width=cin.nextInt();
    r_height=cin.nextInt();
   }
   if(final_width< max_width-remain)
       final_width=max_width-remain;
   final_height+=cur_height;
   System.out.println(final_width+" x "+final_height);
}
}
}
							
Posted in poj | Leave a comment

Poj Solution 2013

http://poj.org/problem?id=2013

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
 public static void main(String[] args){
  Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n;
        String[] s;
        int index=1;
        while (true){
            n=scanner.nextInt();
            if (n==0){
                break;
            }
            System.out.println("SET "+index);
            index++;
            s=new String[n];
            for (int i=0;i< n/2 ;i++ ){
                s[i]=scanner.next();
                s[n-i-1]=scanner.next();
            }
            if (n%2==1){
                s[(n-1)/2]=scanner.next();
            }
            for (int i=0;i< n ;i++ ){
                System.out.println(s[i]);
            }
        }
    }
}


							
Posted in poj | Leave a comment

Poj Solution 2011

http://poj.org/problem?id=2011

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

int ans[20], an;
int temp[20];

int get( int num, int s, int &r ) {
    int g = 0, tg=1, tr=1;
    bool key = true;
    r = 0;
    while( num ) {
        if( s & 1 ) {
            key = num%10;
            g += num%10*tg;
            tg *= 10;
        }
        else {
            r += num%10*tr;
            tr *= 10;
        }
        num /= 10;
        s >>= 1;
    }
    if( !key )
        return -1;
    else
        return g;
}

void search( int s, int k ) {
    temp[k++] = s;

    int m, t=s, i, r;
    for( m=0; t; m++, t/=10 )
        ;
    for( i=1; i<(1<<m)-1; i++ ) {
        t = get( s, i, r );
        if( t > 1 && s%t == 0 )
            search( r, k );
    }

    if( k > an || k == an && !std::lexicographical_compare( ans, ans+an, temp, temp+an ) ) {
        std::copy( temp, temp+k, ans );
        an = k;
    }
    return;
}

int main( ) {
    int n, i;
    while( scanf( "%d", &n ) == 1 && n ) {
        an = 0;
        search( n, 0 );
        for( i=0; i<an; i++ ) {
            if( i ) printf( " " );
            printf( "%d", ans[i] );
        }
        printf( "n" );
    }
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2010

http://poj.org/problem?id=2010

#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

priority_queue< int ,vector<int> > q;

typedef pair< int, int > cow;

cow c[1000000];
int money[100000];
int n, m, f;

int main()
{
    int i, v;

    scanf( "%d %d %d", &n, &m, &f );
    
    for( i=0; i<m; i++ )
        scanf( "%d %d", &c[i].first, &c[i].second );

    sort( c, c+m );

    for( v=0, i=0; i<n/2; i++ )
    {
        v += c[i].second;
        q.push( c[i].second );
    }
    money[i] += v;

    for( ; i<m-n/2-1 ; i++ )
    {
        if( q.top() > c[i].second )
        {
            v -= q.top();
            q.pop();
            v += c[i].second;
            q.push( c[i].second );
        }
        money[i+1] += v;
    }

    while(!q.empty())q.pop();
    
    for( v=0,i=m-1; i>=m-n/2; i-- )
    {
        v += c[i].second;
        q.push( c[i].second );
    }
    

    for( ; i>=n/2 ;i-- )
    {
        money[i] += v;
        if( money[i] + c[i].second <= f )break;

        if( q.top() > c[i].second )
        {
            v -= q.top();
            q.pop();
            v += c[i].second;
            q.push( c[i].second );
        }
    }

    if( i>=n/2 ) printf( "%dn", c[i].first );
    else printf( "%dn", -1 );

    return 0;
}

        


							
Posted in poj | Leave a comment

Poj Solution 2008

http://poj.org/problem?id=2008

#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;

pair<int,int> p[1000];
pair<int,int> q[1000];

bool cmp( const pair<int,int> &a, const pair<int,int> &b ) {
    return a.second < b.second;
}

int main( ) {
    int a, b, c, n, i, j, m, limit, ans = 0;
    scanf( "%d", &n );
    scanf( "%d%d%d", &a, &b, &c );

    for( i=0; i<n; i++ )
        scanf( "%d%d", &p[i].first, &p[i].second );

    sort( p, p+n );
    priority_queue<int, vector<int> > pq;

    for( i=0; i<n; i++ ) {
        m = 0;
        for( j=i; j<n; j++ )
            q[m++] = p[j];
        
        sort( q, q+m, cmp );

        for( j=m-1; j>=0; j-- ) {

            pq.push( a*q[j].first+b*q[j].second );
            limit = a*p[i].first+b*q[j].second+c;

            while( !pq.empty() && pq.top() > limit )
                pq.pop();
            if( pq.size() > ans )
                ans = pq.size();

        }
        while( !pq.empty() )
            pq.pop();
    }

    printf( "%dn", ans );

    return 0;
}



							
Posted in poj | Leave a comment

Poj Solution 2007

http://poj.org/problem?id=2007

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define debug 0
#define INF 1000
#if debug
    #define NMAX 10
#else
    #define NMAX 52
#endif
int point[NMAX][2];
int base[2];
int m[5];
int qu(int p[])
{
    if(p[0]>0&&p[1]>0)
        return 1;
    if(p[0]<0&&p[1]>0)
        return 2;
    if(p[0]<0&&p[1]<0)
        return 3;
    if(p[0]>0&&p[1]<0)
        return 4;
    return 0;
}
int product(int a[],int b[])
{
    return a[0]*b[1]-a[1]*b[0];
}

int cmp(const void *a,const void *b)
{

    
    int p1[2],p2[2];
    if(m[qu((int*)a)]==m[qu((int*)b)])
    {
        p1[0]=((int*)a)[0];
        p2[0]=((int*)b)[0];
        p1[1]=((int*)a)[1];
        p2[1]=((int*)b)[1];
        return -product(p1,p2);
    }
    else
    return m[qu((int*)a)]-m[qu((int*)b)];
            
    
}
int findbase(int flag[])
{
    int i,j;
    for(i=1;i<=4;i++)
    {
        if(!flag[i])
        {
            i++;
            while(!flag[i])
                i++;
            break;
        }
    }
    int k=1;
    for(j=i;j<=i+3;j++)
    {
        m[(j-1)%4+1]=k++;
    }
    return 0;
}


int main()
{
#if debug     
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
       int i;
    int flag[5]={0};
    
       int n;
       i=0;
       n=0;
       while(scanf("%d%d",&point[i][0],&point[i][1])!=EOF)
       {
           flag[qu(point[i])]=1;
           i++;
       }
       findbase(flag);
       n=i;
       qsort((void*)(point+1),n-1,2*sizeof(int),cmp);
       for(i=0;i<n;i++)
       {
           printf("(%d,%d)n",point[i][0],point[i][1]);
       }
       

#if debug
    fclose(stdin);
    fclose(stdout);
#endif
    return 1;
}
							
Posted in poj | Leave a comment

Poj Solution 2005

http://poj.org/problem?id=2005

//* @author: <strong>
import java.util.*;
public class Main
{
 
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   int tot[]=new int[12],t[]=new int[4];
   char ts[]=new char[5];
    int sum,s1,s2,win,tem;
    while (sc.hasNext()) {
       int n=sc.nextInt();
       if(n==0) break;
        for (int i=2;i< 10;i++) tot[i]=4*n;
        tot[10]=16*n;tot[11]=4*n;
        sum=52*n-3;win=0;
        for (int i=1;i<=3;i++) {
            ts=sc.next().toCharArray();
            if (ts[0]=='T'||ts[0]=='J'||ts[0]=='Q'||ts[0]=='K') t[i]=10;
            else if (ts[0]=='A') t[i]=11;
            else t[i]=ts[0]-48;
            tot[t[i]]--;
        }
        s1=t[1];s2=t[2]+t[3];
        if (s2>21) s2-=10;
        for (int i=2;i<=11;i++) {
            tem=s1+i;if (tem>21) tem-=10;
            if (tem< s2) win+=tot[i];
        }
        System.out.printf("%.3f%%nn",100.0*win/sum);
    }
  }
}

							
Posted in poj | Leave a comment

Poj Solution 2004

http://poj.org/problem?id=2004

//* @author: 82638882@163.com
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
public class Main
{
 static my[] s=new my[10001];
 public static void main(String[] args) throws IOException
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  int l=0,max=-1,rr=-1;
  while(in.ready())
  {
    String t=in.readLine();
    s[l++]=new my(t);
  }
  Comparator< my> cp=new Comparator< my>()
  {
    public int compare(my o1, my o2) {
        return o1.l-o2.l;
    }
   };
   Arrays.sort(s,0,l,cp);
   for(int i=0;i< l;i++)
   {
    for(int j=0;j< i;j++)
    {
     if(s[j].l==s[i].l) break;
     if(s[j].l!=s[i].l-1) continue;
        int cnt=0,k=0;
        while(k< s[j].l&&cnt< 2)
        {
              if(s[j].c[k]!=s[i].c[k+cnt]) cnt++;
          else k++;
        }
        if(cnt< 2)
        {
          if(s[i].n< s[j].n+1){
            s[i].n=s[j].n+1;
            s[i].r=j;
          }        
        }    
    }
    }
    
   for(int i=0;i< l;i++)
   {
    if(s[i].n>max)
    {
        max=s[i].n;
        rr=i;
    }
   }
   p(rr);
 }

  static void p(int a)
  {
    if(s[a].r!=-1)
    p(s[a].r);
    System.out.println(s[a].s);
  }
}
class my
{
    String s;
    int n=0;
    int l;
    int r=-1;
    char[] c;
    public my(String t)
    {
        s=t;
        l=s.length();
        c=new char[l];
        for(int i=0;i< l;i++)
            c[i]=s.charAt(i);
        Arrays.sort(c);
    }
}
							
Posted in poj | Leave a comment

Poj Solution 2002

http://poj.org/problem?id=2002

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define DIFF 20000
#define MAX 1005
#define NUM 40005
#define EP 1e-8
#define LIM 1e-6
#define PI acos(-1.0)
struct pt{
    int x,y;
};
pt p[MAX];
int s[NUM],e[NUM];
int n;
int Max,Min;
bool cmp(pt a,pt b)
{
    return a.x<b.x || a.x==b.x && a.y<b.y;
}

void input()
{
    int i;
    Min=NUM,Max=-1;
    for (i=0;i<n;i++) {
        scanf("%d %d",&p[i].x,&p[i].y);
        p[i].x+=DIFF;
        if (p[i].x<Min) Min=p[i].x;
        if (p[i].x>Max) Max=p[i].x;
    }
    std::sort(p,p+n,cmp);
    for (i=1;i<n;i++)
    memset(s+Min,-1,(Max-Min+2)*sizeof(int));
    s[p[0].x]=0;
    for (i=1;i<n;i++) {
        if (p[i].x!=p[i-1].x) {
            e[p[i-1].x]=i-1;
            s[p[i].x]=i;
        }
    }
    e[p[n-1].x]=n-1;
}

int search(pt z)
{
    if (z.x<Min || z.x>Max || s[z.x]<0) return 0;    
    int a=s[z.x], b=e[z.x], mid,x=z.x,y=z.y;
    while (a<b) {
        mid=(a+b)>>1;
        if (p[mid].y==y) {a=mid; break;}
        else if (p[mid].y<y) a=mid+1;
        else b=mid;
    }
    return (p[a].x==z.x && p[a].y==z.y);
}

void solve()
{
    int i,j;
    double alpha,d;
    double ax,ay,bx,by;
    pt a,b;
    int ans=0;
    for (i=0;i<n;i++) {
        for (j=i+1;j<n;j++) {
            if (p[j].x==p[i].x)    {
                a.x=p[i].x+p[j].y-p[i].y;
                a.y=p[j].y;
                b.x=p[i].x+p[j].y-p[i].y;
                b.y=p[i].y;
                if (search(a) && search(b)) ++ans;
            }
            else if (p[j].y>p[i].y) {

                a.x=p[j].y-p[i].y+p[j].x; a.y=p[i].x-p[j].x+p[j].y;
                if (!search(a)) continue;

                b.x=(p[i].x+a.x)-p[j].x; b.y=(p[i].y+a.y)-p[j].y;
                if (search(b)) ++ans;
            }
        }
    }
    printf("%dn",ans);
}

int main()
{
    while (scanf("%d",&n)==1) {
        if (!n) break;
        input();
        solve();
    }    
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 2001

http://poj.org/problem?id=2001

import java.util.*;

public class Main {
 static int MAX = 1010;
 static TrieNode root = new TrieNode();
 static char[][] result = new char[MAX][22];
 static char[][] s = new char[MAX][22];

 public static void main(String[] args) {
  Scanner cin = new Scanner(System.in);
  TrieNode my = new TrieNode();
  int n=0;
  char[] ss = new char[21];
  while(cin.hasNext()) {
    ss = cin.nextLine().toCharArray();
    s[n++] = ss;
    insert(ss);
  }

  int i;
  for(i=0;i< n;i++) {
   check(i);
   System.out.println(new String(s[i])+" "+ new String(result[i]).trim());
  }
 }

private static void insert(char[] str) {
 TrieNode p = root;
 int i,len = str.length;
 for(i=0;i< len;i++){
   if(p.num[str[i]-'a'] == null) {
   p.num[str[i]-'a'] = new TrieNode();
   p = p.num[str[i]-'a'];
  }else{
   p = p.num[str[i]-'a'];
  }
  p.value++;
 }
}

private static void check(int ind) {
 TrieNode p = root;
 int i;
 for(i=0;i< s[ind].length;i++) {
  if(p.value == 0) {
   return;
  }
  p = p.num[s[ind][i]-'a'];
  result[ind][i] = s[ind][i];
 }
}

}
class TrieNode {
 int value;
 TrieNode[] num = new TrieNode[26];
 public TrieNode() {
  value=-1;
  int i;
  for(i=0;i< 26;i++)
    num[i] = null;
  }
} 


							
Posted in poj | Leave a comment

Poj Solution 2000

http://poj.org/problem?id=2000

import java.util.*;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner cin = new Scanner(System.in);   
           
        while(true)   
        {   
            int day = Integer.valueOf(cin.nextLine()).intValue();   
            if(day == 0)   
                break;   
               
            System.out.println(day + " " + getTotalCoin(day));   
        }   
    }   
       
    private static int getTotalCoin(int day)   
    {   
        int total = 1;   
        int factor = 2;   
        int index = 0;   
           
        if(day == 1)   
            return 1;   
           
        for(int i = 2; i <= day; i++)   
        {   
            if(index == factor)   
            {   
                index = 0;   
                factor ++;   
            }   
               
            total += factor;   
            index ++;   
        }      
        return total;   
    }   
}  

							
Posted in poj | Leave a comment

Poj Solution 1995

http://poj.org/problem?id=1995

//* @author: 
import java.util.*;
public class Main{

//��a^b%n
  private static int modexp(int a,int b,int n)   
{   
    int ret=1;   
    int tmp=a;   
    while(b!=0)   
    {   
       if((b&0x1)==1) ret=ret*tmp%n;   
       tmp=tmp*tmp%n;   
       b>>=1;   
    }   
    return ret;   
}   

 public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
   int t=sc.nextInt();
   while(t-->0){
     int m=0;
    int h=0;
    int a=0;
    int b=0;
    int sum=0;
    m=sc.nextInt();
    h=sc.nextInt();
    for(int i=0;i< h;i++){
       a=sc.nextInt();
       b=sc.nextInt();
       sum=(sum+modexp(a%m,b,m))%m;
    }
     System.out.println(sum);
   }
      }
}
							
Posted in poj | Leave a comment

Poj Solution 1990

http://poj.org/problem?id=1990

//* @author: 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
/*
 *�ο���ţ�ij���,��һ��д��״����...
 *�Ƚ�cows��volumeΪ�ؼ�������..
 *}����״����,һ��ά�������,һ��ά��ǰ����С�ڵ�ǰ������֮��
 */
class cin
{
 static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
 static StringTokenizer st;
 static int leave=0;
 static int nextInt() throws IOException
 {
  while(leave==0)
  {
   st=new StringTokenizer(in.readLine());
   leave=st.countTokens();
  }
  leave--;
  return Integer.parseInt(st.nextToken());
 }
 static boolean hasNext() throws IOException
 {
  while(leave==0)
  {
   String temp=in.readLine();
   if(temp==null)return false;
   st=new StringTokenizer(temp);
   leave=st.countTokens();
  }
  return true;
 }
}

class TreeArray 
{
    int value[],n;
    TreeArray(int num)
    {
     n=num;
     value=new int[n+1];
     Arrays.fill(value,0);
    }
    
    int lowBit(int t)
    {
     return t&(t^(t-1));
    }
    
    void plus(int a,int i)
    {
     while(i<=n)
     {
      value[i]+=a;
         i+=lowBit(i);
     }
    }
    
    int getSum(int i)
    {
     int sum=0;
     while(i>0)
     {
      sum+=value[i];
      i=i-lowBit(i);
     }
     return sum;
    }   
}

class Moo
{
 int x,v;
 Moo(int a,int b)
 {
  v=a;x=b;
 }
}

class Cmp implements Comparator< Object>
{
 public int compare(Object a,Object b)
 {
  if(((Moo)a).v>((Moo)b).v)return 1;
  return -1;
 }
}

class MooFest
{
 TreeArray count,total;
 long sum=0,now=0;
 int n,i;
 Moo cow[];
 MooFest() throws IOException
 {
  n=cin.nextInt();
  cow=new Moo[n+1];
  count=new TreeArray(20000);
  total=new TreeArray(20000);
  for(i=1;i<=n;i++)
  {
   cow[i]=new Moo(cin.nextInt(),cin.nextInt());
  }
  Arrays.sort(cow,1,n+1,new Cmp());
 }
 
 long totalV()
 {
  int c,t;
  for(i=1;i<=n;i++)
  {
   now+=cow[i].x;
   count.plus(1,cow[i].x);
   total.plus(cow[i].x,cow[i].x);
   c=count.getSum(cow[i].x);
   t=total.getSum(cow[i].x);
   sum+=(long)(2*c*cow[i].x-2*t+now-i*cow[i].x)*cow[i].v;
  }
  return sum;
 }
}
public class Main {
     public static void main(String args[]) throws IOException
     {
      MooFest data=new MooFest();
      System.out.println(data.totalV());
     }
}
							
Posted in poj | Leave a comment

Poj Solution 1989

http://poj.org/problem?id=1989

//* @author: ccQ.SuperSupper
import java.util.*;
import java.math.*;
public class Main {
    public static void main(String []args) throws Exception{
        
        int i,j,k,n,ans;
        int way[] = new int[100100];
        int flag[] = new int[10100];
        
        Scanner cin = new Scanner(System.in);
        
        n = cin.nextInt();
        k = cin.nextInt();
        
        for(i=0;i<=k;++i) flag[i] = -1;
        
        for(i=0;i< n;++i) way[i] = cin.nextInt();
        
        for(i=ans=j=0;i< n;++i){
            
            if(flag[way[i]]!=ans){
                ++j;
                flag[way[i]] = ans;
            }
            
            if(j>=k){
                ++ans;
                j = 0;
            }
        }
        
        System.out.println(ans+1);
    }
}

							
Posted in poj | Leave a comment

Poj Solution 1988

http://poj.org/problem?id=1988

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
 static int p[]=new int[30010];
 static int d[]=new int[30010];
 static int num[]=new int[30010];
 static int i=0;
 static char c[]=new char[5];

 static int getup(int a)
 {
  int b=a;
   while(p[b]!=b)
    b=p[b];
  return p[a]=b;
 }

 static int getd(int a)
 {
   int b=a;
   int v=0;
   while(d[b]!=b)
   {
    v+=num[b];
    b=d[b];
   }
   num[a]=v;
   return d[a]=b;
 }

 public static void main(String[] args){   
  Scanner sc=new Scanner(System.in);
  for(i=1;i< 30005;i++)
   p[i]=d[i]=i;
  Arrays.fill(num,0);
  int n,m,u,v;
  n=sc.nextInt();
  while((n--)!=0)
  {
    c=sc.next().toCharArray();
    if(c[0]=='C')
    {
         m=sc.nextInt();
      getd(m);
      System.out.printf("%dn",num[m]);
    }
    else if(c[0]=='M')
    {
      u=sc.nextInt();
         v=sc.nextInt();
      int g1=getd(u);
      int g2=getup(v);
      d[g1]=g2;
      p[g2]=g1;
      num[g1]=1;
    }
   }
  }
}  
							
Posted in poj | Leave a comment

Poj Solution 1987

http://poj.org/problem?id=1987

#include"stdio.h"
#include"algorithm"
using namespace std;

long  use[16];
long  father[40010];
long  queue[40010];
long  lchild[40010][16];
long  rchild[40010][16];
long  sum[40010][16];
long  depth[40010][16];

long  value[40010],dist[40010];
long  m,n,k,kind,answer;

long findit(long key,long s1,long deep)
{ 
    long &lc=lchild[s1][deep],&rc=rchild[s1][deep],sumnow=sum[s1][deep];
    long now=depth[s1][deep];

    if(now==key)
    {
        if(rc>=0)return sumnow-sum[rc][deep+1];
        else return sumnow;
    }

    else if(key<now)
    {
        if(lc>=0)return findit(key,lc,deep+1);
        return 0;
    }
    else 
    {
        if(rc>=0)return sumnow-sum[rc][deep+1]+findit(key,rc,deep+1);
        return sumnow;
    }
}

void merge(long s1,long s2,long deep)
{ 
    long &lc=lchild[s1][deep],&rc=rchild[s1][deep];
    long &lc2=lchild[s2][deep],&rc2=rchild[s2][deep];

    sum[s1][deep]+=sum[s2][deep];
            
    if(lc<0)lc=lc2;
    else 
    {
        if(lc2>=0)merge(lc,lc2,deep+1);
    }
    
    if(rc<0)rc=rc2;
    else 
    {
        if(rc2>=0)merge(rc,rc2,deep+1);
    }
}

void great(long key)
{
    long down=0,up=kind-1,deep=0,c,*p=0;
    while(1)
    {
        c=(down+up)/2;
        depth[use[deep]][deep]=value[c];
        sum[use[deep]][deep]=1;
        if(p)*p=use[deep];
        use[deep]++;
                
        if(value[c]==key)break;
        else if(key<value[c]){up=c-1;p=&lchild[use[deep]-1][deep];}
        else {down=c+1;p=&rchild[use[deep]-1][deep];}
        deep++;
    }
}

////////////////////////////////////////////////////////
struct edge
{
    long from,to;
    long distance;
}e[40020*2];

long begin[40020],end[40020];


int cmp(edge a,edge b)
{
    return a.from<b.from||(a.from==b.from&&a.to<b.to);
}

void init()
{
    scanf("%ld %ld",&n,&m);

    long i,j;char c;

    for(i=0;i<2*m;i+=2)
    {
        scanf("%ld %ld %ld %c",&e[i].from,&e[i].to,&e[i].distance,&c);

        e[i].from--;
        e[i].to--;

        e[i+1].from=e[i].to;
        e[i+1].to=e[i].from;
        e[i+1].distance=e[i].distance;
    }

    sort(&e[0],&e[m*2],cmp);
    
    begin[0]=0;
    for(i=1;i<m*2;i++)
    {
        if(e[i].from!=e[i-1].from)
        {
            end[e[i-1].from]=i;
            begin[e[i].from]=i;
        }
    }
    end[n-1]=2*m;
    scanf("%ld",&k);
    
    for(i=0;i<16;i++)
    {
        use[i]=0;
        for(j=0;j<n;j++)
        {
            rchild[j][i]=lchild[j][i]=-1;
            sum[j][i]=0;
        }
    }
    long now,qh=0,qt=1;
    dist[0]=0;queue[0]=0;father[0]=-1;

    while(qh<qt)
    {
        now=queue[qh++];
        for(i=begin[now];i<end[now];i++)
        if((j=e[i].to)!=father[now])
        {
            queue[qt++]=j;
            father[j]=now;
            dist[j]=dist[now]+e[i].distance;
        }
    }
    
    copy(dist,dist+n,value);
    sort(value,value+n);
    
    for(i=1,kind=1;i<n;i++)
    if(value[i]!=value[i-1])value[kind++]=value[i];    

    answer=0;
    return;
}

void countit(long t,long s,int deep,long l)
{
    long a=sum[s][deep],lc=lchild[s][deep],rc=rchild[s][deep];
        
    if(lc>=0)
    {
        a-=sum[lc][deep+1];
        countit(t,lc,deep+1,l);
    }
    
    if(rc>=0)
    {
        a-=sum[rc][deep+1];
        countit(t,rc,deep+1,l);
    }
    
    if(a>0)
    {
        answer+=a*findit(k+l-depth[s][deep],t,0);
    }
    
    return;

}

void doit()
{
    long s,f,i,l,tree[40010];
    
    for(i=0;i<n;i++)
    {
        tree[i]=i;
        great(dist[i]);
    }
    for(i=n-1;i>0;i--)
    {   
        s=queue[i];
        f=father[s];
        l=dist[f]*2;
        if(sum[tree[s]][0]<sum[tree[f]][0])
        {
            countit(tree[f],tree[s],0,l);
            merge(tree[f],tree[s],0);
        }
        else
        {    
            countit(tree[s],tree[f],0,l);
            merge(tree[s],tree[f],0);
            tree[f]=tree[s];
        }
    }
    
    printf("%ldn",answer);
    return ;
}

int main()
{
    init();
    doit();
    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 1981

http://poj.org/problem?id=1981

#include<iostream>
#include"math.h"
#include"algorithm"
using namespace std;

struct point
{
    double x,y;
}p[310];

double jl(point i,point j)
{
    return sqrt((i.x-j.x)*(i.x-j.x)+(i.y-j.y)*(i.y-j.y));
}



bool fzk(point a,point b,double &in,double &out)
{
    double jiao,l,x;
    if((l=jl(a,b))>2)return 0;

    jiao=atan2(b.y-a.y,b.x-a.x);

    x=acos(l/2);

    in=jiao-x;
    out=jiao+x;
    
    return 1;
}

int n,m;
struct node
{
    char sign;
    double jiao;
}t[310];

int cmp(node a,node b)
{
    return a.jiao<b.jiao;
}


void doit()
{
    int i,j,answer,now,kh;
    double a,b;
        
    answer=1;

    for(i=0;i<n;i++)
    {
        m=0;
        for(j=0;j<n;j++)
        {
            if(i!=j&&fzk(p[i],p[j],a,b))
            {
                t[m].jiao=a;t[m].sign=1;m++;
                t[m].jiao=b;t[m].sign=2;m++;
            }
        }

        std::sort(&t[0],&t[m],cmp);

        now=1;kh=9999;
    //    for(j=0;j<m&&t[j].sign==2;j++)
    //        ;
        if(m)
        for(j=0;j<m+kh;j++)
        {
            if(kh==9999&&t[j%m].sign==2)kh=j+1;
            if(t[j%m].sign==1)now++;
            else now--;
            if(now==0)while(1)
                cout<<"error";

            if(now>answer)answer=now;
        }

    }

    cout<<answer<<endl;
}

int main()
{
    int i;
    while(1)
    {
        cin>>n;
        if(n==0)break;
        for(i=0;i<n;i++)
            cin>>p[i].x>>p[i].y;

        doit();
    }

    return 0;
}


    
							
Posted in poj | Leave a comment

Poj Solution 1979

http://poj.org/problem?id=1979

 
							
Posted in poj | Leave a comment

Poj Solution 1978

http://poj.org/problem?id=1978

import java.io.BufferedInputStream;   
import java.util.LinkedList;   
import java.util.Scanner;   
public class Main {   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in)); 

  
        while (scan.hasNext()) {   
            int m = scan.nextInt();   
            int n = scan.nextInt();   
            if (m == 0 && n == 0) {   
                break;   
            }   
            LinkedList<Integer> cards = new LinkedList<Integer>();   
            for (int i = 1; i <= m; i++) {   
                cards.addFirst(i);   
            }   
            for (int i = 0; i < n; i++) {   
                int p = scan.nextInt();   
                int c = scan.nextInt();   
                LinkedList<Integer> lk = new LinkedList<Integer>();   
                for (int j = 0; j < c; j++) {   
                    int a = cards.remove(p - 1);   
                    lk.addLast(a);   
                }   
                for (int j = 0; j < c; j++) {   
                    int a = lk.removeLast();   
                    cards.addFirst(a);   
                }   
            }   
            System.out.println(cards.getFirst());   
        }   
    }   
}  


							
Posted in poj | Leave a comment

Poj Solution 1977

http://poj.org/problem?id=1977

#include<iostream>
#include"string.h"
using namespace std;

char m[30][100][100];
int n,t,v[100];
char name[100][100][100];
char nm[100][100];
int s[100];
char ans[100][100];
char temp[100][100];

void multiply(char a[100][100],char b[100][100],char c[100][100])
{
    int i,j,k;
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    {
        c[i][j]=0;
        for(k=0;k<n;k++)
            c[i][j]+=a[i][k]*b[k][j];
        c[i][j]%=2;
    }
    return;
}
void init()
{
    int i,j,k;
    cin>>n>>t;

    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
        m[0][i][j]=0;
    for(i=0;i<n;i++)
        m[0][i][i]++;
    
    for(i=0;i<n;i++)
    {
        cin>>nm[i];
        cin>>v[i];
        v[i]%=2;
        cin>>s[i];
        for(j=0;j<s[i];j++)
            cin>>name[i][j];
    }

    for(i=0;i<n;i++)
    for(j=0;j<s[i];j++)
    {
        for(k=0;k<n;k++)
            if(strcmp(nm[k],name[i][j])==0)break;
        m[0][i][k]++;
        m[0][i][k]%=2;
    }

    t--;
    for(i=1;(1<<i)<=t;i++)
    {
        multiply(m[i-1],m[i-1],m[i]);
    }

    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
        ans[i][j]=0;
    
    for(i=0;i<n;i++)
        ans[i][i]=1;
    
    for(i=0;t;i++)
    {
        if(t%2)
        {
            multiply(ans,m[i],temp);
            for(j=0;j<n;j++)
            for(k=0;k<n;k++)
                ans[j][k]=temp[j][k];
        }
        t/=2;
    }
}

        

int main()
{
    int cas,i,j,an[100],answer;
    cin>>cas;
    while(cas--)
    {
        init();
        for(i=0;i<n;i++)
        {
            an[i]=0;
            for(j=0;j<n;j++)
                an[i]+=v[j]*ans[j][i];
            an[i]%=2;
        }
        answer=0;
        for(i=0;i<n;i++)
            answer+=an[i];
        cout<<answer<<endl;
    }
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1976

http://poj.org/problem?id=1976

#include<stdio.h>
#include<string.h>

#define debug 0
#define NMAX 10002
int a[NMAX];
int s[4][NMAX];

int main()
{
#if debug     
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int i,j,k,t,d,N;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&N);
        memset(s,0,sizeof(int));
        memset(a,0,sizeof(int));
        for(i=1;i<=N;i++)
        {
            scanf("%d",&a[i]);
            a[i]+=a[i-1];
        }
        scanf("%d",&d);
        for(k=1;k<=3;k++)
        {
            for(i=1;i<=N;i++)
            {
                j=0>i-d?0:i-d;
                s[k][i]=s[k][i-1]>s[k-1][j]+a[i]-a[j]?s[k][i-1]:s[k-1][j]+a[i]-a[j];
            }
        }
        
        printf("%dn",s[3][N]);
        
    }
#if debug
    fclose(stdin);
    fclose(stdout);
#endif
    return 1;
}
							
Posted in poj | Leave a comment

Poj Solution 1974

http://poj.org/problem?id=1974

#include<stdio.h>
#include<stdlib.h>


#define debug 0


#define NMAX 1000003

    
struct data
{
   long x,y;
}v[NMAX];

long t,m,n,k;
long count;

int cmp1(void const *a,void const *b)
{
    if((*(struct data*)a).y==(*(struct data*)b).y)
        return (*(struct data*)a).x-(*(struct data*)b).x;
    else
        return (*(struct data*)a).y-(*(struct data*)b).y;
}

int cmp2(void const *a,void const *b)
{
    if((*(struct data*)a).x==(*(struct data*)b).x)
        return (*(struct data*)a).y-(*(struct data*)b).y;
    else
        return (*(struct data*)a).x-(*(struct data*)b).x;
}
void solve()
{

    int i;
    qsort(v,k,sizeof(struct data),cmp1);

    int sum=0;
//    cy=v[0].y;
    if(v[0].x>2)
        sum++;
    sum+=v[0].y-1;
    for(i=1;i<k;i++)
    {
        if(v[i].y==v[i-1].y)
        {
            if(v[i].x-v[i-1].x>2)
                sum++;
        }
        else
        {
            sum+=v[i].y-v[i-1].y-1;
            if(n+1-v[i-1].x>2)
                sum++;
            if(v[i].x>2)
                sum++;
        }
    }
    if(n+1-v[i-1].x>2)
        sum++;
    sum+=m-v[i-1].y;
    qsort(v,k,sizeof(struct data),cmp2);

    if(v[0].y>2)
        sum++;
    sum+=v[0].x-1;
    for(i=1;i<k;i++)
    {
        if(v[i].x==v[i-1].x)
        {
            if(v[i].y-v[i-1].y>2)
                sum++;
        }
        else
        {
            sum+=v[i].x-v[i-1].x-1;
            if(m+1-v[i-1].y>2)
                sum++;
            if(v[i].y>2)
                sum++;
        }
    }
    if(m+1-v[i-1].y>2)
        sum++;
    sum+=n-v[i-1].x;
    printf("%dn",sum);
}
        
int main()
{
#if debug     
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&m,&n,&k);
        int p=0;
        for(i=0;i<k;i++)
        {
            int t1,t2;
            scanf("%d%d",&t1,&t2);
//            if(t1<=m&&t2<=n&&t1>=1&&t2>=1)
//            {
                v[p].x=t2;
                v[p].y=t1;
                p++;
//            }
        }
        k=p;
        solve();
    }

#if debug
    fclose(stdin);
    fclose(stdout);
#endif
    return 1;
}
							
Posted in poj | Leave a comment

Poj Solution 1973

http://poj.org/problem?id=1973

#include<iostream>
using namespace std;
int f[110],ft[110];
int n,m;
int t1[110],t2[110];

bool checkout(int t)
{
    int i,j,a,b;
    for(i=1;i<=m;i++)
        f[i]=-1;
    f[0]=0;

    for(j=0;j<=m;j++)
        ft[j]=f[j];

    for(i=0;i<n;i++)
    {
        for(a=0;a<=t/t1[i]&&a<=m;a++)
        {
            b=(t-t1[i]*a)/t2[i];
            for(j=0;j<=m-a;j++)
                if(f[j]>=0)
                {
                    if(ft[j+a]<f[j]+b)
                        ft[j+a]=f[j]+b;
                }
        }
        for(j=0;j<=m;j++)
          f[j]=ft[j];
        if(f[m]>=m)return 1;
    }
    return f[m]>=m;
}

int main()
{
    int t,a,b,i,c;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(i=0;i<n;i++)
            cin>>t1[i]>>t2[i];
        a=0;
        b=9999;
        while(a<b)
        {
            c=(a+b)/2;
            if(checkout(c))b=c;
            else a=c+1;
        }
        cout<<b<<endl;
    }
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1972

http://poj.org/problem?id=1972

//* @author: <strong>
import java.util.*;
public class Main
{
 static int max(int u,int v,int w,int x)
{
    if (u< v)u=v;
    if (u< w)u=w;
    if (u< x)u=x;
    return u;
}

 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   
   int a[][]=new int[2][7],s[]=new int[6];
       int nn=sc.nextInt();
    while ((nn--)!=0)
    {
     int n=sc.nextInt();
        for(int i=0;i< s.length;i++)
          s[i]=sc.nextInt();
     a[0][s[0]]=a[0][s[5]]=max(s[1],s[2],s[3],s[4]);
     a[0][s[1]]=a[0][s[3]]=max(s[0],s[2],s[4],s[5]);
     a[0][s[2]]=a[0][s[4]]=max(s[0],s[1],s[3],s[5]);
     for(int i=1;i< n;i++)
     {
      for(int j=0;j< s.length;j++)
            s[j]=sc.nextInt();

      a[i&1][s[0]]=a[(i+1)&1][s[5]]+max(s[1],s[2],s[3],s[4]);
      a[i&1][s[5]]=a[(i+1)&1][s[0]]+max(s[1],s[2],s[3],s[4]);
      a[i&1][s[1]]=a[(i+1)&1][s[3]]+max(s[0],s[2],s[4],s[5]);
      a[i&1][s[3]]=a[(i+1)&1][s[1]]+max(s[0],s[2],s[4],s[5]);
      a[i&1][s[2]]=a[(i+1)&1][s[4]]+max(s[0],s[1],s[3],s[5]);
      a[i&1][s[4]]=a[(i+1)&1][s[2]]+max(s[0],s[1],s[3],s[5]);
    }
    int ans=0;
    if ((n%2)==0) for (int i=1;i<=6;i++) if (ans< a[1][i]) ans=a[1][i];
    if (n%2!=0) for (int i=1;i<=6;i++) if (ans< a[0][i]) ans=a[0][i];
    System.out.printf("%dn",ans);
     }
  }
}
							
Posted in poj | Leave a comment

Poj Solution 1971

http://poj.org/problem?id=1971

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define debug 0

#define INF 30000

#if debug
    #define NMAX 10    
#else
    #define NMAX 1003
#endif
typedef struct
{
    int x,y;
}point;
point a[NMAX],m[NMAX*NMAX];
int cmp(const void*a,const void *b)
{
    if((*(point*)a).x==(*(point*)b).x)
        return (*(point*)a).y-(*(point*)b).y;
    else 
        return (*(point*)a).x-(*(point*)b).x;
}

int main()
{

#if debug     
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int t,n,i,j,M,ans,c,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        k=0;
        for(i=0;i<n;i++) 
        {
            for(j=i+1;j<n;j++)
            {
                
                
                    m[k].x=(a[i].x+a[j].x);
                    m[k].y=(a[i].y+a[j].y);
                    k++;
                 
            }
        }
        qsort(m,k,sizeof(point),cmp);
        ans=0;
        c=1;
        for(i=1;i<k;i++)
        {
            if(m[i].x==m[i-1].x&&m[i].y==m[i-1].y)
                c++;
            else
            { 
                ans+=c*(c-1)/2;
                c=1;
            }
        }
//        ans+=c*(c-1)/2;
        printf("%dn",ans);
    }
#if debug
    fclose(stdin);
    fclose(stdout);
#endif
    return 1;
}
							
Posted in poj | Leave a comment

Poj Solution 1970

http://poj.org/problem?id=1970

/* @author: */
import java.util.Scanner;
public class Main{
 static int p[][]=new int[21][21];
 public static void  main(String args[])
{
  Scanner sc=new Scanner(System.in);
  int tt,i,j,k,x=0,y=0;
  tt=sc.nextInt();
  while((tt--)!=0)
  {
    
   for(i=1;i< 20;i++)
    for(j=1;j< 20;j++)
    p[i][j]=sc.nextInt();
   int tag=0;
   for(i=1;i< 20;i++)
   {
    for(j=1;j< 16;j++)
    {
    if(p[i][j]==p[i][j-1]) continue;
    if(p[i][j]!=p[i][j+1]) continue;
    if(p[i][j]!=p[i][j+2]) continue;
    if(p[i][j]!=p[i][j+3]) continue;
    if(p[i][j]!=p[i][j+4]) continue;
    if(p[i][j]==p[i][j+5]) continue;
    tag=p[i][j];
    x=i;
    y=j;
    break;
    }
    if(tag!=0) break;
   }

   for(j=1;j< 20;j++)
   {
    if(tag!=0) break;
    for(i=1;i< 16;i++)
    {
      if(p[i][j]==p[i-1][j]) continue;
      if(p[i][j]!=p[i+1][j]) continue;
      if(p[i][j]!=p[i+2][j]) continue;
      if(p[i][j]!=p[i+3][j]) continue;
      if(p[i][j]!=p[i+4][j]) continue;
      if(p[i][j]==p[i+5][j]) continue;
      tag=p[i][j];
      x=i;
      y=j;
      break;
    }
    }
   for(i=1;i< 16;i++)
   {
    if(tag!=0) break;
    for(j=1;j< 16;j++)
    {
    if(p[i][j]==p[i-1][j-1]) continue;
    if(p[i][j]!=p[i+1][j+1]) continue;
    if(p[i][j]!=p[i+2][j+2]) continue;
    if(p[i][j]!=p[i+3][j+3]) continue;
    if(p[i][j]!=p[i+4][j+4]) continue;
    if(p[i][j]==p[i+5][j+5]) continue;
    tag=p[i][j];
    x=i;
    y=j;
    break;
    }
   }
  for(i=19;i>4;i--)
  {
   if(tag!=0) break;
   for(j=1;j< 16;j++)
   {
    if(p[i][j]==p[i+1][j-1]) continue;
    if(p[i][j]!=p[i-1][j+1]) continue;
    if(p[i][j]!=p[i-2][j+2]) continue;
    if(p[i][j]!=p[i-3][j+3]) continue;
    if(p[i][j]!=p[i-4][j+4]) continue;
    if(p[i][j]==p[i-5][j+5]) continue;
    tag=p[i][j];
    x=i;
    y=j;
    break;
   }
 }
 if(tag==0) System.out.println("0");
 else System.out.printf("%dn%d %dn",tag,x,y);
 }
 }
}
							
Posted in poj | Leave a comment

Poj Solution 1969

http://poj.org/problem?id=1969

import java.util.*;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner cin = new Scanner(System.in);   
           
        while(cin.hasNext())   
        {   
            int num = cin.nextInt();   
            int index = 1;   
            int result = 0;   
            while(result <= num)   
            {   
                result += index;   
                index ++;   
            }   
            result -= (index-1);   
               
            int layer = index - 1;   
            int offset = num - result;   
            int up, down = 0;   
               
            if(layer % 2 == 0)   
            {   
                if(offset == 0)   
                {   
                    up = 1;   
                    down = layer - 1;   
                }   
                else  
                {   
                    up = offset;   
                    down = layer + 1 - offset;    
                }   
            }   
            else  
            {   
                if(offset == 0)   
                {   
                    up = layer - 1;   
                    down = 1;   
                }   
                else  
                {   
                    up = layer + 1 - offset;   
                    down = offset;     
                }   
            }   
            System.out.println("TERM " + num + " IS " + up + "/" + down);   
        }   
    }   
}  


							
Posted in poj | Leave a comment

Poj Solution 1965

http://poj.org/problem?id=1965

//* @author  mekarlos@gmail.com
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {        
 Scanner scan=new Scanner(System.in);
  BigDecimal k;
  BigDecimal root=new BigDecimal("1");        
  BigDecimal aux=new BigDecimal("0.0000000001");
  BigDecimal tr=new BigDecimal("3");
   String a;
   int ind=0,sum;
   while(scan.hasNext()){
    k=scan.nextBigDecimal();
    root=k.divide(new BigDecimal("2"));
    while(k.subtract(root.pow(3)).abs().compareTo(aux)>0)
     root=root.subtract(root.pow(3).subtract(k).divide(root.pow(2).multiply(tr),120,BigDecimal.ROUND_DOWN));
    a=root.toPlainString();
    ind=a.indexOf('.');
    a=a.substring(0,ind+11);
    sum=0;
    for(int j=0;j< a.length();j++) if(j!=ind)sum+=a.charAt(j)-48;
    System.out.println(sum%10+" "+a);
     }
  }
}

							
Posted in poj | Leave a comment

Poj Solution 1964

http://poj.org/problem?id=1964

//* @author  mekarlos@gmail.com
import java.util.Scanner;
public class Main {
 public static void main(String[] args){
  Scanner scan=new Scanner(System.in);
   int[][] N=new int[1001][1001];
   int[][] M=new int[1001][1001];
   int k=scan.nextInt();
   int n=0,m=0,d,maxA;
   for(int z=0;z< k;z++){
    maxA=0;
    m=scan.nextInt();
    n=scan.nextInt();
    for(int i=0;i< m;i++) 
      for(int j=0;j< n;j++)
       if(scan.next().equals("F")) 
        M[i][j]=N[i][j]=1; else M[i][j]=N[i][j]=0;
    for(int i=0;i< m;i++) for(int j=n-2;j>=0;j--) if(N[i][j]!=0)N[i][j]+=N[i][j+1];
    for(int i=0;i< n;i++) for(int j=m-2;j>=0;j--) if(M[j][i]!=0)M[j][i]+=M[j+1][i];
    for(int i=0;i< m;i++)
        for(int j=0;j< n;j++){
            d=N[i][j];
            for(int l=0;l< M[i][j];l++){
                if(N[i+l][j]< d)d=N[i+l][j];
                if(d*(l+1)>maxA)maxA=d*(l+1);
            }
        }
    System.out.println(maxA*3);            
  }        
 }
}
							
Posted in poj | Leave a comment

Poj Solution 1961

http://poj.org/problem?id=1961

//* @author:
import java.util.Scanner;   
  
public class Main {   
    static int next[]=new int[1000010];

    public static void main(String[] args) {   
        Scanner scn = new Scanner(System.in); 
        int n;        
        String input = "";   
        int j=0;
        int k=0;
        while(scn.hasNext()){  
            n=scn.nextInt(); 
            input = scn.next(); 
             if(n==0){  
                break;
             } 
           getNext(input,n);
           System.out.printf("Test case #%dn",++k);
           

           /* �������� */ 
          for (int i = 2; i <= n; i++){ 
            /* ������β�ظ��Ӵ��ij��� */
            j = i - next[i];
            /* �������ظ��������ظ��Ӵ���Ϊ���� */
             if (i % j == 0 && i / j > 1){
                System.out.printf("%d %dn", i, i / j); 
              }
           }
           System.out.printf("n");
        }   
       
      
    }   

   public static void getNext(String T,int len) {//��bģʽ��T��next[]��
    int i = 0;
    int j = next[0] = -1;
    
    while (i< len)
     if (0 > j || T.charAt(i) == T.charAt(j)) {//ƥ��
        j++; i++;
        next[i] = j;
     }else//ʧ��
       j = next[j];
  }
} 
							
Posted in poj | Leave a comment

Poj Solution 1959

http://poj.org/problem?id=1959

/* @author: */
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a[]=new int[100];
    int n,T;
    long sum;
    a[0]=0;
    for(int i=1;i<=20;i++)
    {
        a[i]=i;
        a[20+i]=2*(i);
        a[40+i]=3*(i);
    }
    a[61]=25;
    a[62]=50;
    
    T=sc.nextInt();
    for(int t=0;t< T;t++)
    {
        sum=0;
        n=sc.nextInt();
        for(int i=0;i<=62;i++)
          for(int j=i;j<=62;j++)
           for(int k=j;k<=62;k++)
           if(a[i]+a[j]+a[k]==n)
            sum++;
        System.out.printf("Scenario #%d:n%dnn",t+1,sum);
            
            
    }
  }                                                  
}


							
Posted in poj | Leave a comment

Poj Solution 1953

http://poj.org/problem?id=1953


import java.io.BufferedInputStream;   
import java.util.Scanner;   
 
public class Main {   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in));   
        if (scan.hasNext()) {   
            long[] f = new long[46];   
            f[0] = 0;   
            f[1] = 2;   
            f[2] = 3;   
            for (int i = 3; i <= 45; i++) {   
                f[i] = f[i - 1] + f[i - 2];   
            }   
            int n = scan.nextInt();   
            for (int i = 1; i <= n; i++) {   
                int a = scan.nextInt();   
                System.out.println("Scenario #"+i+":");   
                System.out.println(f[a]);   
                System.out.println();   
            }   
  
        }   
    }   
}  


							
Posted in poj | Leave a comment

Poj Solution 1952

http://poj.org/problem?id=1952

//* @author: 82638882@163.com
import java.io.*;
public class Main
{
  public static void main(String[] args) throws IOException
  {
   InputStreamReader is=new InputStreamReader(System.in);
   BufferedReader in=new BufferedReader(is);
   int a=Integer.parseInt(in.readLine());
   int n=0;
   my[] p=new my[a];
   String[] ss;
   while(a-n>10)
    {
    ss=in.readLine().split(" ");
    for(int i=0;i< 10;i++)
    {
        p[n+i]=new my();
        p[n+i].p=Integer.parseInt(ss[i]);
    }
    n+=10;
    }
   ss=in.readLine().split(" ");
   for(int i=0;i< ss.length;i++)
   {
    p[n+i]=new my();
    p[n+i].p=Integer.parseInt(ss[i]);
   }
   for(int i=0;i< a;i++)
   {
    p[i].q=1;
    for(int j=i-1;j>=0;j--)
    {
        if(p[j].p>p[i].p&&p[j].q>=p[i].q)
            p[i].q=p[j].q+1;
    }
   }

  int max=p[a-1].q;
  for(int i=0;i< a;i++)
    if(p[i].q>max) max=p[i].q;
  System.out.print(max);
  int cnt=0;
  for(int i=0;i< a;i++)
    if(p[i].q==1)p[i].n=1;
  for(int i=0;i< a;i++)
  {
    for(int j=0;j< i;j++)
    {
        if(p[i].q==p[j].q+1&&p[i].p< p[j].p)
             p[i].n+=p[j].n;
        else if(p[i].p==p[j].p&&p[i].q==p[j].q)
        {
            p[i].n-=p[j].n;
        }
    }
   }
   for(int i=0;i< a;i++)
    if(p[i].q==max) cnt+=p[i].n;
   System.out.println(" "+cnt);
 }
}

  class my implements Comparable< my>
  {
    int p;
    int q;
    int n=0;
    @Override
    public int compareTo(my arg0) {
        if(q==arg0.q)
            return p-arg0.p;
        else return q-arg0.q;
    }
}
							
Posted in poj | Leave a comment

Poj Solution 1950

http://poj.org/problem?id=1950

//* @author:alpc12
import java.io.*;
import java.util.*;
import java.math.*;


public class Main {
    
 char[] opa = { '+', '-', '.' };
 char[] op;
 int cnt;
 int n;
 int rec[] = {88, 162, 197, 437, 1350 };
    
 void print() {
  cnt++;
  if(cnt > 20) return;
  int i;
  for(i = 1; i <= n; ++i) {
    System.out.print(i);
    if(i != n){
    System.out.print(" " + op[i] + " ");
    }
  }
  System.out.println("");
 }
    
  void search(int depth, long lastnum, char lastop, long tot) {
    if(cnt > 20) return;
    if(depth == n) {
    if(lastop == '+')
    tot += lastnum;
    else tot -= lastnum;
            
    if(tot == 0)
      print();
    return;
     }
    for(char c : opa) {
    char curop = lastop;
    long curtot = tot;
    if(c == '+') {
       op[depth] = c;
       if(lastop == '+') tot += lastnum;
       else tot -= lastnum;
    lastop = c;
    search(depth+1, depth+1, lastop, tot);
    } else if(c == '-') {
      op[depth] = c;
      if(lastop == '+') tot += lastnum;
      else tot -= lastnum;
      lastop = c;
      search(depth+1, depth+1, lastop, tot);
    } else if(c == '.') {
      op[depth] = c;
      String s = "" + lastnum;
      s += (depth+1);
      if(s.length() > 9) continue;
      long x = Long.parseLong(s);
      search(depth+1, x, lastop, tot);
    }
    tot = curtot;
    lastop = curop;
   }
 }
    
void run() {
  Scanner cin = new Scanner(System.in);
  n = cin.nextInt();
  op = new char[n];
  cnt = 0;
  search(1, 1, '+', 0);
  if(n < 11)
    System.out.println(cnt);
  else 
    System.out.println(rec[n-10-1]);
 }

 public static void main(String[] args) {
   new Main().run();

 }

}
							
Posted in poj | Leave a comment

Poj Solution 1948

http://poj.org/problem?id=1948

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <list>
using namespace std;
bool sign[800][800];
int l[40], n;
typedef pair<int,int> node;
node q[800*800];
int qn;
int main()
{
    int i, j, x, y, s, m;
    list< node >::iterator iter;

    scanf( "%d", &n );
    
    for( s=0,i=0; i<n; i++ )
    {
        scanf( "%d", l+i );
        s += l[i];
    }
    memset( sign, 0, sizeof( sign ) );
    sign[0][0] = 1;    
    qn = 1;
    q[0].first = 0;
    q[0].second = 0;
    for( i=0; i<n; i++ )
    {
        for( j = 0,m = qn; j < m; j++ )
        {
            x = q[j].first;
            y = q[j].second;
            if( x + l[i] <= s/2  && !sign[ x+l[i] ][y] )
            {
                sign[x+l[i]][y] = sign[y][x+l[i]] = true;
                q[qn].first = x+l[i];
                q[qn].second = y;
                qn++;
            }
            if( y + l[i] <= s/2 && !sign[ y+l[i] ][x] )
            {
                sign[y+l[i]][x] = sign[x][y+l[i]] = true;
                q[qn].first = y+l[i];
                q[qn].second = x;
                qn++;
            }
        }
    }
    double ans = -1, p, temp;
    for( i=0; i <= s/2; i++ )
    for( j=i; j <= s/2; j++ )
    if( sign[i][j] && j+i > s-(j+i) && j-i < s-(j+i) && s-j > j )
    {

        p = (double)s/2;
        if( ( temp=sqrt( p*(p-j)*(p-i)*(p-(s-i-j)) ) ) > ans )
            ans = temp;
    }
    if( ans == -1 )printf( "-1n" );
    else printf( "%dn", (int)(ans*100) );    
    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 1947

http://poj.org/problem?id=1947

#include <list>
#include <iostream>
using namespace std;
list< int > e[150];
int ans[150][151];
int n, p, answer;
int root;
int creat( int r )
{
    int i, j, n, m, c, temp[151];
    n = 1;
    ans[r][0] = 1;
    ans[r][1] = 0;
    list< int >::iterator iter;    
    for( iter = e[r].begin(); iter != e[r].end(); iter++ )
    {
        c = *iter;
        m = creat( c );
        for( i = 1; i<=n+m; i++ )
        {
            temp[i] = 99999;
            for( j = (i-n<0)?0:(i-n) ; j <= ( (i-1<m)?(i-1):m ) ; j++ )
            if( ans[c][j] + ans[r][i-j] < temp[i] )
            {
                temp[i] = ans[c][j] + ans[r][i-j];
            }
        }        
        for( i = 1; i<=n+m; i++ )
            ans[r][i] = temp[i];
        n += m;
    }
    if( ans[r][p] + (int) (r!=root) < answer ) answer = ans[r][p] + (int) (r!=root);
    return n ;
}
void init()
{
    int i, a, b, j;
    bool sign[150] = {0};
    scanf( "%d %d", &n, &p );
    for( i=0; i<n-1; i++ )
    {
        scanf( "%d %d", &a, &b );
        e[a-1].push_front( b-1 );
        sign[b-1] = true;
    }
    for( i=0; i<n; i++ )
    for( j=0; j<=n; j++ )
        ans[i][j] = 99999;
    answer = 99999;
    for( i=0; i<n; i++ )
    if( !sign[i] )
    {
        root = i;
        break;
    }
}
int main()
{
    init();
    creat(root);
    printf( "%dn", answer );
    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 1946

http://poj.org/problem?id=1946

#include <iostream>
using namespace std;
int maxs[101][101], n, e, d;
//       ʱ�� ��
int need[101][101];
//         ·�� ��
void init()
{
    int t, s, v, h;
    cin>>n>>e>>d;

    for( t=0; t<=d; t++ )
        maxs[t][0] = 0;
    for( h=0; h<=e; h++ )
        maxs[0][h] = 0;
    for( t=1; t<=d; t++ )
    for( h=1,v=0,s=0; h<=e; h++ )
    {
        while( (v+1)*(v+1)*t <= h )
            v++;
        maxs[t][h] = v*t + (h-v*v*t)/(2*v+1);
    }
    for( h=0; h<=e; h++ )
    {
        t = 0;
        for( s=0; s<=d; s++ )
        {
            while( t<=d && maxs[t][h] < s )
                t++;
            if( t > d ) need[s][h] = -1;
            else need[s][h] = t;
        }
    }

}
int tm[21][101];
//     ��  ·��
int main()
{
    int i, j, k;
    init();
    for( i=0; i<=n; i++ )
    for( j=0; j<=d; j++ )
        tm[i][j] = -1;    
    tm[0][0] = 0;
    for( i=1; i<=n; i++ )
    for( j=0; j<=d; j++ )
    {
        tm[i][j] = tm[i-1][j];
        for( k=0; k<j; k++ )
            if( tm[i-1][k] >=0 && need[j-k][e-k] >=0 && ( tm[i][j]<0 || tm[i-1][k] + need[j-k][e-k] < tm[i][j] ) )
                tm[i][j] = tm[i-1][k] + need[j-k][e-k];
    }

    if( tm[n][d] < 0 ) tm[n][d] = 0;    
    cout<<tm[n][d]<<endl;
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1944

http://poj.org/problem?id=1944

#include <stdio.h>
#include <algorithm>
#include <memory.h>
using namespace std;
int t[4100];
int sum[4100];
void insert( int l, int r, int a, int b, int s, bool del )
{
    int c = (l+r)/2;
    if( l>=r || l>=b || r<=a ) return;
    if( a<=l && r<=b )
    {
        if(del)
        {
            t[s]--;
            if( !t[s] ) sum[s] = (r-l==1) ? 0 : ( sum[s*2+1] + sum[s*2+2] );
        }
        else 
        {
            t[s]++;
            sum[s] = r - l;
        }
        return;
    }
    insert( l, c, a, b, s*2+1, del );
    insert( c, r, a, b, s*2+2, del );
    if( !t[s] )    sum[s] = sum[s*2+1] + sum[s*2+2];
}
typedef pair<int,int> seg;
seg h[20010];
int n, p;
void init()
{
    int i;
    scanf( "%d %d", &n, &p );    
    memset( t, 0, 4100*sizeof(int) );
    memset( sum, 0, 4100*sizeof(int) );
    for( i=0; i<p; i++ )
    {
        scanf( "%d %d", &h[i].first, &h[i].second );
        
        h[i].first--, h[i].second--;

        if( h[i].first > h[i].second ) swap( h[i].first, h[i].second );

        insert( 0, 2048, h[i].first, h[i].second, 0, false );
        
        h[i+p].first = h[i].second;
        h[i+p].second = h[i].first+n;
    }

    p*=2;
    
    sort( h, h+p );


}
int main()
{
    int i, ans, j;
    init();    
    ans = 10000;
    for( i=0, j=0 ; i<n; i++ )
    {
        if( sum[0] < ans ) ans = sum[0];
        while( j < p && h[j].first <= i )
        {
            insert( 0, 2048, h[j].first, h[j].second, 0, true );
            insert( 0, 2048, h[j].second, h[j].first+n, 0, false );
            j++;
        }
    }
    printf( "%dn", ans );
    return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 1942

http://poj.org/problem?id=1942

//* @author: 82638882@163.com
import java.io.*;
public class Main
{
 public static void main(String[] args) throws IOException
  {
    InputStreamReader is=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(is);
    while(true)
    {
    String[] ss=in.readLine().split(" ");
    Long n=Long.parseLong(ss[0]);
    Long m=Long.parseLong(ss[1]);
    if(n==0&&m==0) break;
    long ans=1;
    n=m+n;
    if(m>n-m) m=n-m;
    long temp=m;
    for(long i=n-m+1;i<=n;i++)
    {
        ans*=i;
        while(temp!=0&&ans%temp==0)
        {
            ans/=temp;
            temp--;
        }
    }
    System.out.println(ans);
     }
  }
    
}
							
Posted in poj | Leave a comment

Poj Solution 1940

http://poj.org/problem?id=1940

//* @author: 82638882@163.com
import java.util.*;
public class Main
{
 public static void main(String[] args)
  {
   Scanner in=new Scanner(System.in);
   while(in.hasNext())
   {
    int a=in.nextInt();
    double[] x=new double[a];
    double[] y=new double[a];
    for(int i=0;i< a;i++)
    {
        x[i]=in.nextDouble();
        y[i]=in.nextDouble();
    }
    double sx=0,sy=0,uu=-1;
    System.out.print(a);
    for(int i=0;i< a;i++)
    {
        sx=sy=0;
        for(int j=i;j< a+i;j++)
        {
          sx+=x[j%a]*Math.pow(uu, j-i);
          sy+=y[j%a]*Math.pow(uu, j-i);
        }
        System.out.printf(" %.6f %.6f",sx,sy);
    }
    System.out.println();
    }
  }
}
							
Posted in poj | Leave a comment

Poj Solution 1939

http://poj.org/problem?id=1939

//* @author: 82638882@163.com
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
   Scanner in=new Scanner(System.in);
   while(in.hasNext())
   {
    int a=in.nextInt();
    System.out.print(a+" ");
    double x1,y1,x,y,kx,ky;
    x=x1=in.nextDouble();
    y=y1=in.nextDouble();
    for(int i=1;i< a;i++)
    {
      kx=in.nextDouble();
      ky=in.nextDouble();
      System.out.printf("%.6f %.6f ",(x+kx)/2,(y+ky)/2);
      x=kx;
      y=ky;
    }
    System.out.printf("%.6f %.6fn",(x+x1)/2,(y+y1)/2);
      }
  }
}
							
Posted in poj | Leave a comment

Poj Solution 1936

http://poj.org/problem?id=1936

import java.util.*;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner cin = new Scanner(System.in);   
           
        String[] str = new String[2];   
        String a, b;   
        boolean result;   
           
        while(cin.hasNext())   
        {   
            str = cin.nextLine().split(" ");   
            a = str[0];   
            b = str[1];   
               
            result = findit(a, b);   
            if(result == true)   
                System.out.println("Yes");   
            else  
                System.out.println("No");   
        }   
    }   
       
    private static boolean findit(String lstr, String rstr)   
    {   
        char[] stra = lstr.toCharArray();   
        char[] strb = rstr.toCharArray();   
           
        int cura = 0;   
        char curb = 'n';   
           
        for(int j = 0; j < strb.length; j++)   
        {   
            curb = strb[j];   
            if(curb != stra[cura])   
                continue;   
            else  
            {   
                if(cura == stra.length-1)   
                    return true;   
                else  
                    cura ++;   
            }   
        }   
        return false;   
    }   
}  


							
Posted in poj | Leave a comment

Poj Solution 1934

http://poj.org/problem?id=1934

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <set>
using namespace std;

int ans[81][81];
int from[81][81];
char route1[100],route2[100];
int n,m;

void doit()
{
    int i,j;
    for( i=0; i<=n; i++)
        ans[i][0] = 0;
    for( j=0; j<=m; j++)
        ans[0][j] = 0;

    for( i=1; i<=n; i++ )
    for( j=1; j<=m; j++ )
    {
        if( route1[i] == route2[j] )
        {
            ans[i][j] = ans[i-1][j-1] + 1;
            from[i][j] = 1;
        }
        else ans[i][j] = -1;

        if( ans[i-1][j] == ans[i][j] )
            from[i][j] |= 2;
        else if( ans[i-1][j] > ans[i][j] )
            from[i][j] = 2, ans[i][j] = ans[i-1][j];

        if( ans[i][j-1] == ans[i][j] )
            from[i][j] |= 4;
        else if( ans[i][j-1] > ans[i][j] )
            from[i][j] = 4, ans[i][j] = ans[i][j-1];
    }
}



struct cmp
{
    bool operator()(vector<char>* a,vector<char>* b)const
    {
        return std::lexicographical_compare( a->begin(), a->end(), b->begin(), b->end() );
    }
};

set< vector<char>*, cmp > word[81][81],output;

vector<char> w;

void search(int a,int b,int r)
{
    
    if( word[a][b].find( &w ) == word[a][b].end() )
    {
        vector<char>* p = new vector<char>( w );
        word[a][b].insert( p );
    }
    else return;

    if( r < 0 )
    {
        if( output.find( &w ) == output.end() )
        {
            vector<char>* p = new vector<char>( w );
            output.insert( p );
        }
        return;
    }

    if( from[a][b] & 1 )
    {
        w[r] = route1[a];
        search( a-1, b-1, r-1);
        w[r] = '';
    }

    if( from[a][b] & 2 )
        search( a-1, b, r );

    if( from[a][b] & 4 )
        search( a, b-1, r );
}

int main()
{
    int i,j;

    scanf("%s %s",route1+1,route2+1);

    n = strlen( route1+1 );
    m = strlen( route2+1 );

    doit();

    for(i=0;i<n;i++)
    for(j=0;j<m;j++)
        word[i][j].clear();

    w.resize(ans[n][m],'');

    search( n, m, ans[n][m]-1 );

    set< vector<char>*, cmp >::iterator iter, iter2;

    for( iter=output.begin(), i=0; iter != output.end() ; iter++,i++ )
    {
        if( !i || cmp()( *iter2, *iter ) )
        {    
            for(j=0;j<ans[n][m];j++)
                printf("%c",(*(*iter))[j] );
            printf("n");
        }
        iter2 = iter;
    }

    return 0;
    
}



							
Posted in poj | Leave a comment

Poj Solution 1928

http://poj.org/problem?id=1928

//* @author: 82638882@163.com
import java.io.*;
import java.util.Arrays;
public class Main
{
 public static void main(String[] args) throws IOException
 {
    InputStreamReader is=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(is);
    int a=Integer.parseInt(in.readLine());
    while((a--)!=0)
    {
         String[] ss=in.readLine().split(" ");
      int cow=Integer.parseInt(ss[0]);
      int cul=Integer.parseInt(ss[1]);
      int sum=Integer.parseInt(ss[2])-2;
      my[] p=new my[cow*cul+3];
      int l=1;
      for(int i=0;i< cow;i++)
      {
        ss=in.readLine().split(" ");
        for(int j=0;j< cul;j++)
        {
            int y=Integer.parseInt(ss[j]);
            if(y!=0)
            {
              p[l]=new my();
              p[l].x=i;
              p[l].y=j;
              p[l].n=y;
              l++;
            }
        }
        }
       Arrays.sort(p,1,l);
       p[0]=new my();
       p[0].x=0;
       p[0].y=p[1].y;
       p[0].n=0;
       int total=0;
       for(int i=1;i< l;i++)
       {
        int dis=Math.abs(p[i].x-p[i-1].x)+Math.abs(p[i].y-p[i-1].y);
        if(sum>=dis+1+p[i].x)
        {
            sum=sum-dis-1;
            total+=p[i].n;
        }
        else break;
        }
       System.out.println(total);
    }
   }
}

class my implements Comparable< my>
{
    int x;
    int y;
    int n;
    @Override
    public int compareTo(my arg0) {
        return arg0.n-n;
    }
}
							
Posted in poj | Leave a comment

Poj Solution 1927

http://poj.org/problem?id=1927

/* @author: */
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
  
   int k = 1;
   double l1, l2, l3, s, c, p, R, ctg1, ctg2, ctg3, len, r, ls, area;

   while( sc.hasNext() )
   {
      l1=sc.nextDouble();
      l2=sc.nextDouble();
      l3=sc.nextDouble();
      len=sc.nextDouble();
      if( l1 == 0 && l2 == 0 && l3 == 0 && len == 0 )
      break;

      c = l1 + l2 + l3;
      p = ( l1 + l2 + l3 ) / 2;
      s = Math.sqrt( p * (p-l1) * (p-l2) * (p-l3) );

      if( len >= c )
      {
       System.out.printf( "Case %d: %.2fn", k++, s );
       continue;
    }
    R = s * 2 / c ;
    if( len <= R * 2 * Math.PI )
    {
           System.out.printf( "Case %d: %.2fn", k++, len*len / (4*Math.PI) );
        continue;
    }
    ctg1 = ( p - l1 ) / R;
    ctg2 = ( p - l2 ) / R;
    ctg3 = ( p - l3 ) / R;
    r = ( c - len ) / ( 2 * ( ctg1 + ctg2 + ctg3 ) - 2 * Math.PI );
    ls = c - 2 * ( ctg1 + ctg2 + ctg3 ) * r;
    l1 -= ( ctg2 + ctg3 ) * r;
    l2 -= ( ctg1 + ctg3 ) * r;
    l3 -= ( ctg1 + ctg2 ) * r;
    p = ( l1 + l2 + l3 ) / 2;
    area = ls * r + Math.sqrt( p * (p-l1) * (p-l2) * (p-l3) ) + r * r * Math.PI;
    System.out.printf( "Case %d: %.2fn", k++, area ); 
   }
  }
}

							
Posted in poj | Leave a comment

Poj Solution 1925

http://poj.org/problem?id=1925

#include"stdio.h"
#include"math.h"
#include"memory.h"


int x[5010],h[5010];
int pos[1000001];

int n;

void init()
{
    int i;
    scanf( "%d", &n );

    for( i=0; i<n; i++ )
        scanf( "%d %d", x+i, h+i );

    memset( pos, -1, sizeof(int)*(x[n-1]+1) );
}

void doit()
{
    int i,j,l,y,k;
    int ans;

    ans = 100000;
    pos[0] = 0;

    for( i=1; i<n; i++ )
    {
        
        l = x[i] - (int)sqrt( (double)h[i]*h[i] - (double)( h[i] - h[0] )*( h[i] - h[0] ) );

        for(j = l>0?l:0 ; j < x[i] ; j ++ )
        if(pos[j] >=0 )
        {
            y = (x[i]<<1) - j;
            k = pos[j] + 1;

            if( y >= x[n-1] )
            {
                if( k < ans ) ans = k;
            }
            else if( pos[y] < 0 || k < pos[y] ) pos[y] = k;
        }

//        if(i%100==0)
//            printf("::%dn", i);
    }

    if( ans == 100000 ) ans = -1;

    printf( "%dn", ans );
}

int main()
{
    int i,cas;

    scanf( "%d", &cas );
    for( i=1; i<=cas; i++ )
    {
        init();
        doit();
    }

    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1923

http://poj.org/problem?id=1923

import java.util.Scanner;
public class Main {
  
static int com(int a)
{
    return a*(a-1)/2;
}

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
   
    long line[][]=new long[101][10001];
    int i,j,k,p,q,cs=0;
    long  max,tmp;
    for(i=1;i<=100;i++)
     line[i][0]=i+1;
    for(i=2;i<=100;i++)
    for(j=1;j<=com(i);j++)
    {
       max=0;
       for(k=1;k<=i-1;k++)
       {
         p=i-k;q=j-k*p;
         if(q>=0)
         {
           if(line[p][q]!=0)
        {
           tmp=line[p][q]+k*(p+1);
           if(tmp>max)
             max=tmp;
        }
        }
     }
     line[i][j]=max;
       }
    while (sc.hasNext())
    {
        i=sc.nextInt();
        j=sc.nextInt();
     if(i==0&&j==0)
        break;
     cs++;
    if(line[i][j]>0)
      System.out.printf("Case %d: %d lines with exactly %d crossings can cut the plane into %d pieces at most.n",cs,i,j,line[i][j]);
    else System.out.printf("Case %d: %d lines cannot make exactly %d crossings.n",cs,i,j);
    }
   }
}

							
Posted in poj | Leave a comment