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.
The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ 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 ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.
Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.
3 7 2 1 3 8 5 2
11
5 10 2 77 3 33 8 21 9 12 10 64
79
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.
#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;
}
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|.
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.
Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.
01 00111
3
0011 0110
2
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 long
const 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;
}
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.
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 000, 1 ≤ 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.
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
4 1 9 3 1 6 1 7 4
1
7 1 1 2 1 3 1 4 1 5 1 6 1 7 1
3
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;
}