ACM 第十六天

计算几何

练习题:

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.

Input
* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<string.h>
  5 
  6 using namespace std;
  7 
  8 struct Point
  9 {
 10     int x,y;
 11     Point(int _x=0,int _y=0)
 12     {
 13         x=_x;
 14         y=_y;
 15     }
 16     Point operator -(const Point &b) const
 17     {
 18          return Point(x-b.x,y-b.y);
 19     }
 20     int operator ^(const Point &b) const
 21     {
 22         return x*b.y-y*b.x;
 23     }
 24     int operator *(const Point &b) const
 25     {
 26         return x*b.x+y*b.y;
 27     }
 28     void input()
 29     {
 30         scanf("%d%d",&x,&y);
 31     }
 32 };
 33 
 34 int dist2(Point a,Point b)
 35 {
 36     return (a-b)*(a-b);
 37 }
 38 const int maxn=50010;
 39 Point list[maxn];
 40 int Stack[maxn],top;
 41 bool _cmp(Point p1,Point p2)
 42 {
 43     int tmp=(p1-list[0])^(p2-list[0]);
 44     if(tmp>0) return true;
 45     else if(tmp==0 && dist2(p1,list[0])<= dist2(p2,list[0]))
 46         return true;
 47     else return false;
 48 }
 49 void Graham(int n)
 50 {
 51     Point p0;
 52     int k=0;
 53     p0=list[0];
 54     for(int i=1;i<n;i++)
 55     if(p0.y>list[i].y || (p0.y==list[i].y && p0.x>list[i].x))
 56     {
 57         p0=list[i];
 58         k=i;
 59     }
 60     swap(list[0],list[k]);
 61     sort(list+1,list+n,_cmp);
 62     if(n==1)
 63     {
 64         top=1;
 65         Stack[0]=0;
 66         return ;
 67     }
 68     if(n==2)
 69     {
 70         top=2;
 71         Stack[0]=0;
 72         Stack[1]=1;
 73         return ;
 74     }
 75     Stack[0]=0;
 76     Stack[1]=1;
 77     top=2;
 78     for(int i=2;i<n;i++)
 79     {
 80         while(top>1 && ((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]]))<=0)
 81             top--;
 82         Stack[top++]=i;
 83     }
 84 }
 85 //旋转卡壳,求两点间距离平方的最大值
 86 int rotating_calipers(Point p[],int n)
 87 {
 88     int ans=0;
 89     Point v;
 90     int cur=1;
 91     for(int i=0;i<n;i++)
 92     {
 93         v=p[i]-p[(i+1)%n];
 94         while((v^(p[(cur+1)%n]-p[cur]))<0)
 95             cur=(cur+1)%n;
 96         ans=max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+1)%n],p[(cur+1)%n])));
 97     }
 98     return ans;
 99 }
100 Point p[maxn];
101 int main()
102 {
103     int n;
104     while(scanf("%d",&n)==1)
105     {
106         for(int i=0;i<n;i++)
107         list[i].input();
108         Graham(n);
109         for(int i=0;i<top;i++)
110          p[i]=list[Stack[i]];
111          printf("%d\n",rotating_calipers(p,top));
112     }
113     return 0;
114 }

 

Arcade mall is a new modern mall. It has a new hammer game called "Arcade Game". In this game you're presented with a number n which is hanged on a wall on top of a long vertical tube, at the bottom of the tube there is a button that you should hit with your hammer.

When you hit the button with all your force (as you always do), a ball is pushed all over the tube and hit the number n. The number n flies in the air and it's digits fall back in any random permutation with uniform probability.

If the new number formed is less than or equal to the previous number, the game ends and you lose what ever the new number is. Otherwise (if the number is greater than the previous number), you are still in the game and you should hit the button again.

You win if the new number formed is greater than the previous number and it is equal to the greatest possible permutation number.

Can you compute the probability of winning?


Input

The first line of the input contains the number of test cases T. Following that there are T lines represents T test cases. In each line, there is a single integer (1 ≤ n ≤ 109) the target number. The digits of n are all unique, which means that any 2 digits of n are different.

Output

For each test case, print one line containing the answer. Print the answer rounded to exactly 9 decimal digits.

Examples
Input
3
952
925
592
Output
0.000000000
0.166666667
0.194444444
Note

In the first test case, the answer is 0 because 952 is greater than all 2,5 and 9 permutations so you can't win, whatever you do.

In the second test case, the answer is 0.166666667 because you may win by getting number 952 with probability 1/6.

In the third test case the answer is 0.194444444 because you may win by getting number 952 in round1 with probability 1/6 or you can win by getting number 925 in round 1 and then 952 in round 2 with probability 1/6 * 1/6.

原博客:https://blog.csdn.net/why850901938/article/details/51204620
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n[15]={1,1}; int a[15]; double p,q; int main() { for(int i=2;i<10;i++) { n[i]=n[i-1]*i; } int T; cin>>T; while(T--) { char s[11]; int cnt=0; scanf("%s",s); int l=strlen(s); for(int i=0;i<l;i++) { a[i]=s[i]-'0'; } while(next_permutation(a,a+l)) { cnt++; } if(cnt==0) { printf("0.000000000\n"); } else { double temp=1.0/n[l]; q=p=temp; for(int i=1;i<cnt;i++) { q=p+p*temp; p=q; } printf("%.9lf\n",p); } } return 0; }

 

Geometry is a very important field in mathematics, Squares and rectangles are essential shapes in geometry, both of them have 4 right angles, but a square is a special case of a rectangle where width and height are the same.

The figure below shows a square on the left and a rectangle on the right:

If you have the width and the height of a 4 right angled shape, can you figure out if it is a square or a rectangle?


Input

The first line contains T, the number of test cases, for each test case there is a line with two integers (1 ≤ w, h ≤ 1, 000, 000) representing width and height, respectively.

Output

For each test case, print one line consists of 'Square' if the shape is a square, otherwise print 'Rectangle' if it is a rectangle.

Examples
Input
3
10 10
13 200
300 300
Output
Square
Rectangle
Square
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,a,b;
 5     scanf("%d",&n);
 6     while(n--)
 7     {
 8         scanf("%d %d",&a,&b);
 9         if(a==b)
10             printf("Square\n");
11         else
12             printf("Rectangle\n");
13     }
14     return 0;
15 }

 

Salem is known to be one of the best competitive programmers in the region. However, he always finds a hard time understanding the concept of the hamming distance. The hamming distance of two numbers is defined as the number of different digits in their binary representations (leading zeros are used if necessary to make the binary representations have the same length). For example the hamming distance between 12 and 5 is 2 since their binary representations are 1100 and 0101 respectively and they differ in the first and fourth positions.

Recently, Salem got a problem that he needs your help to solve. He is given N integers and asked to get the maximum among the hamming distances of all possible pairs of the given integers.


Input

The first line of the input will be a single integer T representing the number of test cases. Followed by T test cases. Each test case will start with a line with single integer (2 ≤ N ≤ 100) representing the number of the integers. Each of the following N lines contains one of the integers (1 ≤ Ai ≤ 10, 000) from the list of the integers.

Output

For each test case print one line consists of one integer representing the maximum hamming distance between all possible pairs from the given integers.

Examples
Input
2
2
12
5
3
1
2
3
Output
2
2
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int check(int n)
 5 {
 6     int c=0;
 7     while(n)
 8     {
 9         n=n&(n-1);
10         c++;
11     }
12     return c;
13 }
14 int main()
15 {
16     int n;
17     int a[105];
18     cin>>n;
19     while(n--)
20     {
21         int n1;
22         cin>>n1;
23         int maxx=0;
24         for(int i=1;i<=n1;i++)
25         {
26             cin>>a[i];
27         }
28         for(int i=1;i<=n1;i++)
29         {
30             for(int  j=i+1;j<=n1;j++)
31             {
32                 int m=check(a[i]^a[j]);
33 //                cout<<"m: "<<m<<endl;
34                 if(m>maxx)
35                     maxx=m;
36             }
37         }
38         cout<<maxx<<endl;
39     }
40 }

 

 

 
posted @ 2018-08-19 17:46  白雪儿  Views(223)  Comments(0Edit  收藏  举报