码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 567A 567B 567C 567D

时间:2015-08-07 20:11:33      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:acm算法

A. Lineland Mail
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single point.

Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).

Strange but true, the cost of sending the letter is exactly equal to the distance between the sender‘s city and the recipient‘s city.

For each city calculate two values ??mini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city

Input

The first line of the input contains integer n (2?≤?n?≤?105) — the number of cities in Lineland. The second line contains the sequence of n distinct integers x1,?x2,?...,?xn (?-?109?≤?xi?≤?109), where xi is the x-coordinate of the i-th city. All the xi‘s are distinct and follow in ascending order.

Output

Print n lines, the i-th line must contain two integers mini,?maxi, separated by a space, where mini is the minimum cost of sending a letter from the i-th city, and maxi is the maximum cost of sending a letter from the i-th city.

Sample test(s)

Input

4-5 -2 2 7

Output

3 123 94 75 12

Input

2-1 1

Output

2 22 2


  题意就是找每个点距离最小和最大的,排序后。对于最小的,显然只有找它旁边的两个,对于最大的,找第一个和最后一个,注意,第一个和最后一个特判一下就好
#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
#define N 500005
typedef long long ll;
#include <queue>
#include <set>
#include <map>
#include <stack>
int main()
{
    int n,i;
    while(scanf("%d",&n)!=EOF)
    {
        int a[200005];
        memset(a,0,sizeof(a));
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        ll min,max;
        if(n==2)
        {
            printf("%d %d\n",abs(a[0]-a[1]),abs(a[0]-a[1]));
            printf("%d %d\n",abs(a[0]-a[1]),abs(a[0]-a[1]));
            continue;
        }
        for(i=0; i<n; i++)
        {

            if(i==0)
            {
                if(abs(a[n-1]-a[0])>=abs(a[0]-a[1]))
                {
                    max=abs(a[n-1]-a[0]);
                    min=abs(a[0]-a[1]);
                }
                else
                {

                    min=abs(a[0]-a[1]);
                    max=abs(a[0]-a[1]);
                }
            }
           else if(i==n-1)
            {
                if(abs(a[i]-a[n-2])>=abs(a[i]-a[0]))
                {
                    max=abs(a[i]-a[n-2]);
                    min=abs(a[i]-a[n-2]);
                }
                else
                {
                    min=abs(a[i]-a[n-2]);
                    max=abs(a[i]-a[0]);
                }
            }
            else
            {
                if(abs(a[i]-a[0])>=abs(a[i]-a[n-1]))
                {
                    max=abs(a[i]-a[0]);
                }
                else
                {

                    max=abs(a[i]-a[n-1]);
                }
                if(abs(a[i]-a[i+1])>=abs(a[i]-a[i-1]))
                {
                    min=abs(a[i]-a[i-1]);
                }
                else
                    min=abs(a[i]-a[i+1]);
            }
            printf("%lld %lld\n",min,max);

        }
    }
    return 0;
}







B. Berland National Library
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.

Today was the pilot launch of an automated reading room visitors‘ accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library — it‘s a unique integer from 1 to 106. Thus, the system logs events of two forms:

  • "+ ri" — the reader with registration number ri entered the room;
  • "- ri" — the reader with registration number ri left the room.

The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.

Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.

Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.

Input

The first line contains a positive integer n (1?≤?n?≤?100) — the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ ri" or "- ri", where ri is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).

It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.

Output

Print a single integer — the minimum possible capacity of the reading room.

Sample test(s)
Input
6
+ 12001
- 12001
- 1
- 1200
+ 1
+ 7
Output
3
Input
2
- 1
- 2
Output
2
Input
2
+ 1
- 1
Output
1
Note

In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.




题意:+ 代表进去一个编号为 x的人,- 代表出来一个编号为x的人。问这量公交车至少能容纳几个人。
举一个例子;
- 1
- 2
+3
+4
答案是2.
首先出去两个人,说明里面有两个位子空了 ,又进去两个,所以位子个数不变。
做法就是用set模拟。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
#define N 500005
typedef long long ll;
#include <queue>
#include <set>
#include <map>
#include <stack>
set<int>q;
set<int>::iterator it,itt;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        char s[101][2];
        int x;
        int ans=0;
        for(int i=0; i<n; i++)
        {
            cin>>s[i]>>x;
            if(s[i][0]=='-')
            {
                if(q.count(x))
                    q.erase(x);
                else
                    ans++;
            }
            else
            {
                q.insert(x);
                int ans1=q.size();
                ans=max(ans,ans1);
            }
        }
         cout<<ans<<endl;
    }
    return 0;
}




C. Geometric Progression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.

He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.

A subsequence of length three is a combination of three such indexes i1,?i2,?i3, that 1?≤?i1?<?i2?<?i3?≤?n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.

A geometric progression with common ratio k is a sequence of numbers of the form b·k0,?b·k1,?...,?b·kr?-?1.

Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.

Input

The first line of the input contains two integers, n and k (1?≤?n,?k?≤?2·105), showing how many numbers Polycarp‘s sequence has and his favorite number.

The second line contains n integers a1,?a2,?...,?an (?-?109?≤?ai?≤?109) — elements of the sequence.

Output

Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.

Sample test(s)
Input
5 2
1 1 2 2 4
Output
4
Input
3 1
1 1 1
Output
1
Input
10 3
1 2 6 2 3 6 9 18 3 9
Output
6
Note

In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.

            


给定比值k,问有多少个比值为k,项数为3的等比数列。
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
__int64 n,k;
__int64 a[200005];
__int64 b[200005]; //记录i位置的数对应 有多少个 a[i]/k 存在
map <int ,__int64 > c;
int main()
{
    __int64 i;
    scanf("%I64d%I64d",&n,&k);
    for (i=1; i<=n; i++)
    {
        scanf("%I64d",&a[i]);
        if (a[i]%k==0)
            b[i]=c[a[i]/k]; 		//记录当前位置 拥有多少个 a[i]/k (等比的前一项)
        c[a[i]]++;
        //每次把a[i]个数加一
    }
    // for(i=1; i<=n; i++)
    // cout<<b[i]<<endl;
    c.clear();
    __int64 ans=0;
    for (i=1; i<=n; i++)
    {
        if (a[i]%k==0)
            ans+=c[a[i]/k];   //a[i]为第三项,a[i]/k为第二项,c[a[i]/k]存的是(每一个(第二项对应的第一项)的个数)之和
        c[a[i]]+=b[i]; 	//c[a[i]] 存的是 数a[i] 对应的 a[i]/k 的个数
    }
    printf("%I64d\n",ans);
    return 0;
}




D. One-Dimensional Battle Ships
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1?×?n table).

At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1?×?a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.

After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit").

But here‘s the problem! Alice like to cheat. May be that is why she responds to each Bob‘s move with a "miss".

Help Bob catch Alice cheating — find Bob‘s first move, such that after it you can be sure that Alice cheated.

Input

The first line of the input contains three integers: n, k and a (1?≤?n,?k,?a?≤?2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.

The second line contains integer m (1?≤?m?≤?n) — the number of Bob‘s moves.

The third line contains m distinct integers x1,?x2,?...,?xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.

Output

Print a single integer — the number of such Bob‘s first move, after which you can be sure that Alice lied. Bob‘s moves are numbered from 1 to m in the order the were made. If the sought move doesn‘t exist, then print "-1".

Sample test(s)
Input
11 3 3
5
4 8 6 1 11
Output
3
Input
5 1 3
2
1 5
Output
-1
Input
5 1 3
1
3
Output
1




题意:大意是给你k个长度为a的船,以及一个长度为n的地方,船不能相邻或相交,再给你m个箭,,这个箭射的位置不能有船,输出到哪个位置一定会有船,否则输出-1.

x_x_x_x_x_   _为箭射的地方。
二分答案,每次2分箭射的地方,找到上界和下界

#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define maxn 1100000
using namespace std;
int bb[maxn],a[maxn],n,m,k,aa;
bool check(int x)
{
    int size=0,ans=0;
    for(int i=1; i<=n; i++)
    {
        size++;
        if(bb[i]<=x&&bb[i])size=0;
        if(size>=aa)
        {
            size=-1;
            ans++;
        }
    }
    if(ans>=k)
    return 1;
    else
     return 0;
}
int main()
{
    scanf("%d%d%d%d",&n,&k,&aa,&m);
    for(int i=1; i<=m; i++)
    {
        scanf("%d",&a[i]);
        bb[a[i]]=i;
    }
    int l=1,r=m;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(check(mid))l=mid+1;
        else r=mid-1;
    }
    if(l<=m)printf("%d\n",l);
    else puts("-1");
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 567A 567B 567C 567D

标签:acm算法

原文地址:http://blog.csdn.net/sky_miange/article/details/47340569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!