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

Codeforces Round #Pi (Div. 2) (ABCD题解)

时间:2015-08-12 01:29:06      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:codeforces

比赛链接:http://codeforces.com/contest/567


听说Round #Pi的意思是Round #314。。。


A. Lineland Mail
time limit per test:3 seconds
memory limit per test:256 megabytes

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 12
3 9
4 7
5 12
Input
2
-1 1
Output
2 2
2 2


题目大意:一条线上n个点,求每个点到其他点的最近和最远距离


题目分析:往左往右取最大最小


#include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = 1e5 + 5;
int a[MAX];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    printf("%d %d\n", a[1] - a[0], a[n - 1] - a[0]);
    for(int i = 1; i < n - 1; i++)
        printf("%d %d\n", min(a[i + 1] - a[i], a[i] - a[i - 1]), max(a[i] - a[0], a[n - 1] - a[i]));
    printf("%d %d\n", a[n - 1] - a[n - 2], a[n - 1] - a[0]);
}


B. Berland National Library
time limit per test:1 second
memory limit per test:256 megabytes

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.

题目大意:一共n个操作,+ i表示标号为i的人进会场,-i表示标号为i的人出会场,问会场的最小容量


题目分析:用个set模拟一下


#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
set <int> st;

int main()
{
    int n, ans = 0;;
    scanf("%d", &n);
    while(n --)
    {
        char s[2];
        int id;
        scanf("%s %d", s, &id);
        if(s[0] == '+') 
            st.insert(id);  //进去
        else
        {
            if(!st.count(id)) //本来就在里面的出来
                ans ++;
            else
                st.erase(id);  //之前进去的出来
        }
        ans = max(ans, (int) st.size());
    }
    printf("%d\n", ans);
}



C. Geometric Progression
time limit per test:1 second
memory limit per test:256 megabytes

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.


题目大意:n个数,问能找到多少组三个数,它们的下标严格递增且公比为k


题目分析:ai那么大,数组不够,直接上map,fir表示中间那个数出现的次数,sec表示第一个数出现的次数,每次按公比k找满足条件的数


#include <cstdio>
#include <map>
#define ll long long
using namespace std;
map <int, ll> fir, sec;

int main()
{
    int n, k;
    ll ans = 0;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++)
    {
        int a;
        scanf("%d", &a);
        if(a % k == 0)
        {
            ans += fir[a / k];
            fir[a] += sec[a / k]; 
        }
        sec[a] ++;
    }
    printf("%I64d\n", ans);
}



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

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

题目大意:在一条长度为n的线上放船,最多放k只船,每只船的长度为a,且任意两只船不能重叠或者接触对方,现在给出m条hit指令,每条指令表示攻击线上xi的位置,问最少到第几条指令能肯定会打到一个船,若都可能打不到则输出-1


题目分析:因为不能接触,所以一条船占用的最小长度实际为a + 1,注意在一端的情况,先求出实际最多可以放的船数,然后对于每个查询的点,二分找到离它最近的两个点,修改能放的船的数目,一旦数目小于k,则找到答案,因为这里k是给定的最多能放的船数,如果小于这个值,说明一定有重复的地方出现


#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
set <int> s;
set <int> :: iterator it;

int main()
{
    int n, k, a;
    int ans = -1;
    scanf("%d %d %d", &n, &k, &a);
    int num = n / (a + 1);
    if(n % (a + 1) >= a)
        num ++;
    s.insert(0);
    s.insert(n + 1);
    int m;
    scanf("%d", &m);
    for(int i = 1; i <= m; i++)
    {
        int x;
        scanf("%d", &x);
        s.insert(x);
        it = s.lower_bound(x);
        it --;
        int l = *it;
        it = s.upper_bound(x);
        int r = *it;
        num -= (r - l) / (a + 1);
        num += (x - l) / (a + 1);
        num += (r - x) / (a + 1);
        if(num < k)
        {
            ans = i;
            break;
        }
    }
    printf("%d\n", ans);
}




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

Codeforces Round #Pi (Div. 2) (ABCD题解)

标签:codeforces

原文地址:http://blog.csdn.net/tc_to_top/article/details/47425543

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