zoukankan      html  css  js  c++  java
  • Codeforces Round #Pi (Div. 2) ABCDEF已更新

    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 Oxaxis. 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 ofn 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 inascending 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

    题解:水题
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cstring>
     7 #define PAU putchar(' ')
     8 #define ENT putchar('
    ')
     9 using namespace std;
    10 const int maxn=100000+10,inf=-1u>>1;
    11 int A[maxn],n;
    12 inline int read(){
    13     int x=0,sig=1;char ch=getchar();
    14     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
    15     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
    16     return sig?x:-x;
    17 }
    18 inline void write(int x){
    19     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    20     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
    21     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    22 }
    23 void init(){
    24     n=read();for(int i=1;i<=n;i++)A[i]=read();
    25     write(A[2]-A[1]);PAU;
    26     write(A[n]-A[1]);ENT;
    27     for(int i=2;i<n;i++){
    28         write(min(A[i]-A[i-1],A[i+1]-A[i]));PAU;
    29         write(max(A[i]-A[1],A[n]-A[i]));ENT;
    30     }
    31     write(A[n]-A[n-1]);PAU;
    32     write(A[n]-A[1]);ENT;
    33     return;
    34 }
    35 void work(){
    36     return;
    37 }
    38 void print(){
    39     return;
    40 }
    41 int main(){init();work();print();return 0;}
    42 /*
    43 4
    44 -5 -2 2 7
    45 */
     
    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 aregistration 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, 1200and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.

    题解:我的神奇的维护贡献的做法:窝萌设A[i]表示第i个时刻的人数,每次来了一个没有出现过的减号就把它前面所有的贡献维护一下,来了一个出现过的减号就把它后面的贡献维护一下,来了一个加号就把当前贡献加1好了。。。。。。。。。QAQ。。。。

    当时在pretest上奋斗了很久。。。。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cstring>
     7 #define PAU putchar(' ')
     8 #define ENT putchar('
    ')
     9 using namespace std;
    10 const int maxn=10010,maxm=200+10;
    11 const int HASHSIZE=1276537;
    12 int first[HASHSIZE],next[maxn],v[maxn],sz;
    13 bool find(int val){
    14     int x=val%HASHSIZE;
    15     if(x<0) x+=HASHSIZE;
    16     for(int i=first[x];i;i=next[i]) if(v[i]==val)return true;
    17     return false;
    18 }
    19 void insert(int val){
    20     int x=val%HASHSIZE;
    21     if(x<0) x+=HASHSIZE;
    22     for(int i=first[x];i;i=next[i]) if(v[i]==val) {return;}
    23     v[++sz]=val;
    24     next[sz]=first[x];
    25     first[x]=sz;
    26 }
    27 void remove(int val){
    28     int x=val%HASHSIZE;
    29     if(x<0) x+=HASHSIZE;
    30     if(!first[x]) {return;}
    31     if(v[first[x]]==val){
    32         first[x]=next[first[x]];
    33         return;
    34     }
    35     for(int i=first[x];next[i];i=next[i]) if(v[next[i]]==val) {next[i]=next[next[i]];return;}
    36 }
    37 inline int read(){
    38     int x=0,sig=1;char ch=getchar();
    39     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
    40     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
    41     return sig?x:-x;
    42 }
    43 inline void write(int x){
    44     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    45     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
    46     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    47 }
    48 int n,A[maxm];
    49 void init(){
    50     n=read();int x;int ans=0;char s[10];
    51     for(int i=1;i<=n;i++){
    52         scanf("%s",s);A[i]+=A[i-1];
    53         if(s[0]=='+'){
    54             x=read();insert(x);A[i]++;
    55         }else{
    56             x=read();
    57             if(find(x)){remove(x);A[i+1]--;
    58                 //for(int j=i;j<=n;j++)A[j]--; 
    59             } 
    60             else for(int j=0;j<i;j++){
    61                 A[j]++;
    62             }
    63         }
    64         //printf("---%d---
    ",i);for(int i=1;i<=n;i++)write(A[i]),PAU;ENT;
    65     }
    66     for(int i=0;i<=n;i++)ans=max(ans,A[i]);write(ans);
    67     //ENT;
    68     //for(int i=1;i<=n;i++)write(A[i]),PAU;ENT;
    69     
    70     return;
    71 }
    72 void work(){
    73     return;
    74 }
    75 void print(){
    76     return;
    77 }
    78 int main(){init();work();print();return 0;}
    79 /*
    80 6
    81 + 12001
    82 - 12001
    83 - 1
    84 - 1200
    85 + 1
    86 + 7
    87 
    88 2
    89 - 1
    90 + 2
    91 
    92 2
    93 - 1
    94 - 2
    95 
    96 2
    97 + 1
    98 - 1
    99 */
     
    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.

    题解:STL大法好,sort一下然后疯狂的upper,lower就行。。。懒得写代码了。。。

     1 #include <bits/stdc++.h>
     2 #define Pii pair <ll, int>
     3 #define ll long long
     4 using namespace std;
     5 const int MAXN = 200007;
     6 Pii arr[MAXN];
     7 int main()
     8 {
     9     ll n, k;
    10     while(~scanf("%I64d%I64d", &n, &k))
    11     {
    12         for(int i = 0; i < n; ++i)
    13         {
    14             scanf("%I64d", &arr[i].first);
    15             arr[i].second = i;
    16         }
    17         sort(arr, arr + n);
    18         ll ans = 0;
    19         for(int i = 1; i < n - 1; ++i)
    20         {
    21             if(arr[i].first % k == 0)//枚举中间的数
    22             {
    23                 //arr[i]/k的个数
    24                 ll cnt1 = upper_bound(arr, arr + n, Pii(arr[i].first / k, arr[i].second - 1)) - lower_bound(arr, arr + n, Pii(arr[i].first / k, 0));
    25                 //arr[i]×k的个数
    26                 ll cnt2 = upper_bound(arr, arr + n, Pii(arr[i].first * k, n)) - lower_bound(arr, arr + n, Pii(arr[i].first * k, arr[i].second + 1));
    27                 ans += cnt1 * cnt2;
    28             }
    29         }
    30         printf("%I64d
    ", ans);
    31     }
    32     return 0;
    33 }
    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: nk 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 nk 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

    题解:二分练习题

    E. President and Roads
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.

    Once a year the President visited his historic home town t, for which his motorcade passes along some path from s to t (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from s to t, along which he will travel the fastest.

    The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.

    Input

    The first lines contain four integers nms and t (2 ≤ n ≤ 105; 1 ≤ m ≤ 105; 1 ≤ s, t ≤ n) — the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (s ≠ t).

    Next m lines contain the roads. Each road is given as a group of three integers ai, bi, li (1 ≤ ai, bi ≤ nai ≠ bi; 1 ≤ li ≤ 106) — the cities that are connected by the i-th road and the time needed to ride along it. The road is directed from city ai to city bi.

    The cities are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path froms to t along the roads.

    Output

    Print m lines. The i-th line should contain information about the i-th road (the roads are numbered in the order of appearance in the input).

    If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes).

    Otherwise, if the i-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing.

    If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes).

    Sample test(s)
    input
    6 7 1 6
    1 2 2
    1 3 10
    2 3 7
    2 4 8
    3 5 3
    4 5 2
    5 6 1
    output
    YES
    CAN 2
    CAN 1
    CAN 1
    CAN 1
    CAN 1
    YES
    input
    3 3 1 3
    1 2 10
    2 3 10
    1 3 100
    output
    YES
    YES
    CAN 81
    input
    2 2 1 2
    1 2 1
    1 2 2
    output
    YES
    NO
    Note

    The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing.

    In the first sample president initially may choose one of the two following ways for a ride: 1 → 2 → 4 → 5 → 6 or1 → 2 → 3 → 5 → 6.

    题解:太神奇了!正反跑最短路,然后怎么弄数量呢?然后有:总数量=s出发的*t出发的,妈妈呀。。。

    F. Mausoleum
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    King of Berland Berl IV has recently died. Hail Berl V! As a sign of the highest achievements of the deceased king the new king decided to build a mausoleum with Berl IV's body on the main square of the capital.

    The mausoleum will be constructed from 2n blocks, each of them has the shape of a cuboid. Each block has the bottom base of a 1 × 1meter square. Among the blocks, exactly two of them have the height of one meter, exactly two have the height of two meters, ..., exactly two have the height of n meters.

    The blocks are arranged in a row without spacing one after the other. Of course, not every arrangement of blocks has the form of a mausoleum. In order to make the given arrangement in the form of the mausoleum, it is necessary that when you pass along the mausoleum, from one end to the other, the heights of the blocks first were non-decreasing (i.e., increasing or remained the same), and then — non-increasing (decrease or remained unchanged). It is possible that any of these two areas will be omitted. For example, the following sequences of block height meet this requirement:

    • [1, 2, 2, 3, 4, 4, 3, 1];
    • [1, 1];
    • [2, 2, 1, 1];
    • [1, 2, 3, 3, 2, 1].

    Suddenly, k more requirements appeared. Each of the requirements has the form: "h[xisigni h[yi]", where h[t] is the height of the t-th block, and a signi is one of the five possible signs: '=' (equals), '<' (less than), '>' (more than), '<=' (less than or equals), '>=' (more than or equals). Thus, each of the k additional requirements is given by a pair of indexes xiyi (1 ≤ xi, yi ≤ 2n) and sign signi.

    Find the number of possible ways to rearrange the blocks so that both the requirement about the shape of the mausoleum (see paragraph 3) and the k additional requirements were met.

    Input

    The first line of the input contains integers n and k (1 ≤ n ≤ 35, 0 ≤ k ≤ 100) — the number of pairs of blocks and the number of additional requirements.

    Next k lines contain listed additional requirements, one per line in the format "xi signi yi" (1 ≤ xi, yi ≤ 2n), and the sign is on of the list of the five possible signs.

    Output

    Print the sought number of ways.

    Sample test(s)
    input
    3 0
    output
    9
    input
    3 1
    2 > 3
    output
    1
    input
    4 1
    3 = 6
    output
    3


    题解:窝萌观察这个数列,最小的数一定是在两端或者是挤在一边,窝萌删掉这样两个数后仍然满足。。。
    可以设dp[i][j]表示数列中i~j这一段的合法数量,按刚才的规律分治即可。至于约束条件狂讨论就行了,(然而明显不可写。。。
  • 相关阅读:
    day01-python基础
    python3.5爬虫实例:根据网站的反爬虫策略,启用代理来防止爬虫被禁用
    python3.5爬虫实例:根据城市名称来获取该城市最近七天的天气预报
    python3.5爬虫基础urllib结合beautifulsoup实例
    python3.5爬虫基础urllib实例
    面向对象相关知识及常用操作(二)
    面向对象相关知识点及常见的操作
    常用的基础模块介绍
    利用正则表达式来实现求一个数学表达式的和
    正则表达式的方法及其匹配规则
  • 原文地址:https://www.cnblogs.com/chxer/p/4706312.html
Copyright © 2011-2022 走看看