Codeforces Round #336 (Div. 2) 总结

来源:互联网 发布:淘宝网登录密码加密 编辑:程序博客网 时间:2024/06/07 11:44
A. Saitama Destroys Hotel
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.

The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.

Input

The first line of input contains two integers n and s (1 ≤ n ≤ 1001 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively.

The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.

Output

Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.

Sample test(s)
input
3 72 13 85 2
output
11
input
5 102 773 338 219 1210 64
output
79
Note

In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:

1. Move to floor 5: takes 2 seconds.

2. Pick up passenger 3.

3. Move to floor 3: takes 2 seconds.

4. Wait for passenger 2 to arrive: takes 4 seconds.

5. Pick up passenger 2.

6. Go to floor 2: takes 1 second.

7. Pick up passenger 1.

8. Go to floor 0: takes 2 seconds.

This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.

解:就是从最顶楼到底楼的时间花费。每下一层,花时间为1s。还有等待的人花的时间。求总时间
模拟就完事。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxm=1e3+10;struct node{    int x,y;}t[maxm];int cmp(node p,node q){    return p.x>q.x;}int main(){    int n,s;    while(scanf("%d%d",&n,&s)!=EOF)    {        int sum=0;        for(int i=0;i<n;i++)        {            scanf("%d%d",&t[i].x,&t[i].y);        }        sort(t,t+n,cmp);        for(int i=0;i<n;i++)        {            if(i==0)            {                sum+=(s-t[i].x);                if(sum<=t[i].y)                {                    sum+=(t[i].y-sum);                }            }            else            {                sum+=(t[i-1].x-t[i].x);                if(sum<=t[i].y)                {                    sum+=(t[i].y-sum);                }            }        }        sum+=(t[n-1].x);        printf("%d\n",sum);    }    return 0;}

B. Hamming Distance Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Genos needs your help. He was asked to solve the following programming problem by Saitama:

The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

Input

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).

The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).

Both strings are guaranteed to consist of characters '0' and '1' only.

Output

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

Sample test(s)
input
0100111
output
3
input
00110110
output
2
Note

For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is|0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.

解:求前缀和。然后用数学方法过。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define LL long longconst int maxm=1e6+10;char s1[maxm],s2[maxm];LL dp1[maxm];int main(){    while(scanf("%s%s",s1,s2)!=EOF)    {        memset(dp1,0,sizeof(dp1));        LL len1=strlen(s1),len2=strlen(s2);        for(LL i=1;i<=len2;i++)        {            dp1[i]=dp1[i-1]+s2[i-1]-'0';        }        LL sum=0;        for(LL i=0; i<len1; ++i)        {            if(s1[i]=='0')                sum+=(dp1[len2-len1+i+1]-dp1[i]);            else                sum+=(len2-len1+1-dp1[len2-len1+i+1]+dp1[i]);        }        printf("%lld\n",sum);    }    return 0;}
C. Chain Reaction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 0001 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output

Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

Sample test(s)
input
41 93 16 17 4
output
1
input
71 12 13 14 15 16 17 1
output
3
Note

For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9with power level 2.

For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position1337 with power level 42.

题意:

在一个数轴上,有n根杆子,每根杆子在ai位置,高bi

有一个人在推杆子,然后杆子会向左边倒下,使得被压中的杆子都会坏掉

然后让你在最右边加一个杆子,高度自己定

使得坏掉的杆子数量最少,问你最少的数量是多少

解:从右向左扫一遍,记录最小毁坏的就行了

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxm=1e6+10;int pow[maxm];int u[maxm];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        memset(pow,0,sizeof(pow));        memset(u,0,sizeof(u));        int a,b;        for(int i=0;i<n;i++)        {            scanf("%d%d",&a,&b);            pow[a]=b;        }        int Max=maxm;        for(int i=0;i<maxm;i++)        {            int t=i-pow[i]-1;            if(t<0)                u[i]=0;            else                u[i]=u[t];            if(pow[i])                u[i]++;            Max=min(n-u[i],Max);        }        printf("%d\n",Max);    }    return 0;}

1 0