Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot.
The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.
Print the only integer d — the minimal number of steps to get the finish position.
0 0 4 5
5
3 4 6 1
3
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxm=1e6+10;
int main()
{
int x1,y1,x2,y2;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
int m=max(abs(x1-x2),abs(y1-y2));
printf("%d\n",m);
}
return 0;
}
Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (https://en.wikipedia.org/wiki/Seven-segment_display).
Max starts to type all the values from a to b. After typing each number Max resets the calculator. Find the total number of segments printed on the calculator.
For example if a = 1 and b = 3 then at first the calculator will print 2 segments, then — 5 segments and at last it will print 5 segments. So the total number of printed segments is 12.
The only line contains two integers a, b (1 ≤ a ≤ b ≤ 106) — the first and the last number typed by Max.
Print the only integer a — the total number of printed segments.
1 3
12
10 15
39
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int N(int x)
{
if(x==0)
return 6;
else if(x==1)
return 2;
else if(x==2)
return 5;
else if(x==3)
return 5;
else if(x==4)
return 4;
else if(x==5)
return 5;
else if(x==6)
return 6;
else if(x==7)
return 3;
else if(x==8)
return 7;
else if(x==9)
return 6;
}
int main()
{
int x,y;
while(scanf("%d%d",&x,&y)!=EOF)
{
int sum=0;
for(int i=x;i<=y;i++)
{
int t=i;
while(t)
{
sum+=N(t%10);
t/=10;
}
}
printf("%d\n",sum);
}
return 0;
}
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.
Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.
Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row.
The second line contains n integers ai (1 ≤ ai ≤ 109) – the type of the i-th pearl.
On the first line print integer k — the maximal number of segments in a partition of the row.
Each of the next k lines should contain two integers lj, rj (1 ≤ lj ≤ rj ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment.
Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type.
If there are several optimal solutions print any of them. You can print the segments in any order.
If there are no correct partitions of the row print the number "-1".
5 1 2 3 4 1
1 1 5
5 1 2 3 4 5
-1
7 1 2 1 3 1 2 1
2 1 3 4 7
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
const int maxm=1e6+10;
int a[maxm];
struct node
{
int l,r;
}t[maxm];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int start=0,cnt=0;
map<int,int>q;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(q[a[i]])
{
q.clear();
t[cnt].l=start+1;
t[cnt++].r=i;
start=i;
}
else
{
q[a[i]]=1;
}
}
if(!cnt)
printf("-1\n");
else
{
printf("%d\n",cnt);
for(int i=0;i<cnt-1;i++)
{
printf("%d %d\n",t[i].l,t[i].r);
}
printf("%d %d\n",t[cnt-1].l,n);
}
}
return 0;
}
。