cfdiv2_140

来源:互联网 发布:柴犬为什么会笑 知乎 编辑:程序博客网 时间:2024/06/07 07:36

http://codeforces.com/contest/227/problem/A

A. Where do I Turn?
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Trouble came from the overseas lands: a three-headed dragonGorynych arrived. The dragon settled at point C and began to terrorize the residents ofthe surrounding villages.

A brave hero decided to put an end to the dragon. He moved frompoint A to fight withGorynych. The hero rode from point A along a straight road and met pointB on his way. The hero knowsthat in this land for every pair of roads it is true that they areeither parallel to each other, or lie on a straight line, or areperpendicular to each other. He also knows well that pointsB and C are connected by a road. So the heromust either turn 90 degrees to the left or continue riding straightahead or turn 90 degrees to the right. But he forgot where thepoint C is located.

Fortunately, a Brave Falcon flew right by. It can see all threepoints from the sky. The hero asked him what way to go to get tothe dragon's lair.

If you have not got it, you are the falcon. Help the hero andtell him how to get him to point C: turn left, go straight or turnright.

At this moment the hero is believed to stand at pointB, turning his back to pointA.

Input

The first input line contains two space-separated integersxa, ya (|xa|, |ya| ≤ 109) — the coordinates of pointA. The second line containsthe coordinates of point B inthe same form, the third line contains the coordinates of pointC.

It is guaranteed that all points are pairwise different. It isalso guaranteed that either point B lies on segment AC, or angle ABC is right.

Output

Print a single line. If a hero must turn left, print "LEFT"(without the quotes); If he must go straight ahead, print "TOWARDS"(without the quotes); if he should turn right, print "RIGHT"(without the quotes).

Sample test(s)
Input
0 0
0 1
1 1
Output
RIGHT
Input
-1 -1
-3 -3
-4 -4
Output
TOWARDS
Input
-4 -6
-3 -7
-2 -6
Output
LEFT
Note

The picture to the first sample:

cfdiv2_140

The red color shows points A, B and C. The blue arrow shows thehero's direction. The green color shows the hero's trajectory.

The picture to the second sample:

cfdiv2_140

比赛的时候就是想不出来,用向量做了测试数据都过不了,表示题意没理解错,方法错了。。

题意:给3个点,A B C。。AB表示一条有向射线,BC表示另一条有向射线,求ABC是否在同一条直线上||AB-BC向左成90度||AB——BC向右成90度。。。利用白书第五章最后一节说到的面积计算公式就行了。。

#include
#include
using namespace std;

int main()
{
    double  x1,y1,x2,y2,x0,y0;
    while(scanf("%lf%lf%lf%lf%lf%lf",&x0,&y0,&x1,&y1,&x2,&y2)!=EOF)
    {
       double area=x0*y1+x2*y0+x1*y2-x2*y1-x0*y2-x1*y0;
       if(area==0)
       {
           printf("TOWARDS\n");
       }
       else if(area>0)
       {
           printf("LEFT\n");
       }
       else
       {
           printf("RIGHT\n");
       }
    }
    return 0;
}

 

http://codeforces.com/contest/227/problem/B

B. Effective Approach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Once at a team training Vasya, Petya and Sasha got a problem onimplementing linear search in an array.

According to the boys, linear search works as follows. The arrayelements in a pre-selected order are in turn compared with thenumber that you need to find. Once you find the array element thatis equal to the required one, the search ends. The efficiency ofthe algorithm is the number of performed comparisons. The fewercomparisons the linear search has made, the more effective itis.

Vasya believes that a linear search would work better if itsequentially iterates through the elements, starting with the1-st one (in this problem we considerthe elements of the array indexed from 1 to n) andending with the n-th one. AndPetya says that Vasya is wrong: the search will need lesscomparisons if it sequentially iterates the elements starting fromthe n-th and ending with the1-st one. Sasha argues that the twoapproaches are equivalent.

To finally begin the task, the teammates decided to settle thedebate and compare the two approaches on an example. For this, theytook an array that is a permutation of integers from 1 to n, andgenerated m queries of theform: find element with value biin the array. They want to calculate for both approaches how manycomparisons in total the linear search will need to respond to allqueries. If the first search needs fewer comparisons, then thewinner of the dispute is Vasya. If the second one does, then thewinner is Petya. If both approaches make the same number ofcomparisons, then Sasha's got the upper hand.

But the problem is, linear search is too slow. That's why theboys aren't going to find out who is right before the end of thetraining, unless you come in here. Help them to determine who willwin the dispute.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in thearray. The second line contains n distinct space-separated integersa1, a2, ..., an (1 ≤ ai ≤ n) — the elements ofarray.

The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The lastline contains mspace-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the searchqueries. Note that the queries can repeat.

Output

Print two integers, showing how many comparisons Vasya'sapproach needs and how many comparisons Petya's approach needs.Separate the numbers by spaces.

Please, do not use the %lld specifier to read or write 64-bitintegers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Sample test(s)
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note

In the first sample Vasya's approach will make one comparison(it starts with the 1-st element andimmediately finds the required number), and Petya's approach makestwo comparisons (first he compares with the 2-nd array element, doesn't find the search itemand compares with the 1-stelement).

In the second sample, on the contrary, Vasya's approach willneed two comparisons (first with 1-stelement, and then with the 2-nd), andPetya's approach will find the required value in one comparison(the first comparison with the 2-ndelement).

 

 

B题:题意:有个数列,然后在输入若干个数,求这些数要通过几次比较才能找到(正着比较&&逆着比较)。。

#include
#include
using namespace std;
#define maxn 100100
long long int a[maxn];
long long int pos[maxn];
int main()
{
    intn,m;
   while(scanf("%d",&n)!=EOF)
    {
       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
           //scanf("%d",&a[i]);
           pos[a[i]]=i;
       }
       scanf("%d",&m);
       int b;
       long long int sum1=0,sum2=0;
       for(int i=1;i<=m;i++)
       {
           scanf("%d",&b);
           sum1+=pos[b];
           sum2+=n-pos[b]+1;
       }
       cout<<sum1<<" "<<sum2<<endl;
       //printf("%d %d\n",sum1,sum2);
    }
    return0;
}

原创粉丝点击