There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Print one integer — the minimum total distance the friends need to travel in order to meet together.
7 1 4
6
30 20 10
20
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
题意:
题解:排序输出a[2]-a[0]
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 #include<bits/stdc++.h> 8 #include<map> 9 #include<set> 10 #include<cmath> 11 #include<queue> 12 #include<bitset> 13 #include<math.h> 14 #include<vector> 15 #include<string> 16 #include<stdio.h> 17 #include<cstring> 18 #include<iostream> 19 #include<algorithm> 20 #pragma comment(linker, "/STACK:102400000,102400000") 21 using namespace std; 22 #define A first 23 #define B second 24 const int mod=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 //typedef long long ll; 29 typedef __int64 ll; 30 const ll MOD=1000000007; 31 const int INF=1000000010; 32 const ll MAX=1ll<<55; 33 const double eps=1e-5; 34 const double inf=~0u>>1; 35 const double pi=acos(-1.0); 36 typedef double db; 37 typedef unsigned int uint; 38 typedef unsigned long long ull; 39 int n; 40 int a[3]; 41 int main() 42 { 43 scanf("%d %d %d",&a[0],&a[1],&a[2]); 44 sort(a,a+3); 45 cout<<a[1]-a[0]+a[2]-a[1]<<endl; 46 return 0; 47 }
Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters.
In this problem you should implement the similar functionality.
You are given a string which only consists of:
- uppercase and lowercase English letters,
- underscore symbols (they are used as separators),
- parentheses (both opening and closing).
It is guaranteed that each opening parenthesis has a succeeding closing parenthesis. Similarly, each closing parentheses has a preceding opening parentheses matching it. For each pair of matching parentheses there are no other parenthesis between them. In other words, each parenthesis in the string belongs to a matching "opening-closing" pair, and such pairs can't be nested.
For example, the following string is valid: "_Hello_Vasya(and_Petya)__bye_(and_OK)".
Word is a maximal sequence of consecutive letters, i.e. such sequence that the first character to the left and the first character to the right of it is an underscore, a parenthesis, or it just does not exist. For example, the string above consists of seven words: "Hello", "Vasya", "and", "Petya", "bye", "and" and "OK". Write a program that finds:
- the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses),
- the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
The first line of the input contains a single integer n (1 ≤ n ≤ 255) — the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols.
Print two space-separated integers:
- the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses),
- the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
37
_Hello_Vasya(and_Petya)__bye_(and_OK)
5 4
37
_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__
2 6
27
(LoooonG)__shOrt__(LoooonG)
5 2
5
(___)
0 0
In the first sample, the words "Hello", "Vasya" and "bye" are outside any of the parentheses, and the words "and", "Petya", "and" and "OK" are inside. Note, that the word "and" is given twice and you should count it twice in the answer.
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 #include<bits/stdc++.h> 8 #include<map> 9 #include<set> 10 #include<cmath> 11 #include<queue> 12 #include<bitset> 13 #include<math.h> 14 #include<vector> 15 #include<string> 16 #include<stdio.h> 17 #include<cstring> 18 #include<iostream> 19 #include<algorithm> 20 #pragma comment(linker, "/STACK:102400000,102400000") 21 using namespace std; 22 #define A first 23 #define B second 24 const int mod=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 //typedef long long ll; 29 typedef __int64 ll; 30 const ll MOD=1000000007; 31 const int INF=1000000010; 32 const ll MAX=1ll<<55; 33 const double eps=1e-5; 34 const double inf=~0u>>1; 35 const double pi=acos(-1.0); 36 typedef double db; 37 typedef unsigned int uint; 38 typedef unsigned long long ull; 39 int n; 40 char a[300]; 41 char b[300]; 42 int main() 43 { 44 scanf("%d",&n); 45 scanf("%s",a); 46 for(int i=0;i<n;i++) 47 b[i]=a[i]; 48 for(int i=0;i<n;i++) 49 { 50 int j=i; 51 if(a[i]=='(') 52 { 53 a[i]='_'; 54 while(1) 55 { 56 j++; 57 if(a[j]==')') 58 break; 59 a[j]='_'; 60 } 61 a[j]='_'; 62 } 63 i=j; 64 } 65 for(int i=0;i<n;i++) 66 { 67 if(a[i]==b[i]) 68 b[i]='_'; 69 if(b[i]=='('||b[i]==')') 70 b[i]='_'; 71 } 72 int ans1=0,ans2=0; 73 for(int i=0;i<n;i++) 74 { 75 int j=i; 76 int exm=0; 77 if(a[i]!='_') 78 { 79 exm++; 80 j++; 81 while(1) 82 { 83 84 if(a[j]=='_'||j>=n) 85 break; 86 j++; 87 exm++; 88 } 89 } 90 i=j; 91 ans1=max(ans1,exm); 92 } 93 for(int i=0;i<n;i++) 94 { 95 int j=i; 96 if(b[i]!='_') 97 { 98 j++; 99 while(1) 100 { 101 102 if(b[j]=='_'||j>=n) 103 break; 104 j++; 105 } 106 ans2++; 107 } 108 i=j; 109 } 110 cout<<ans1<<" "<<ans2<<endl; 111 return 0; 112 }
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.
In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
4 2
1 2 3 2
2 1
1 2 1 2
7 3
1 3 2 2 2 2 1
2 1
1 3 3 2 2 2 1
4 4
1000000000 100 7 1000000000
1 4
1 2 3 4
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
题意:歌单中有n首歌 一个人喜欢喜欢1~m 乐队 分别给出你n首歌的乐队 要求歌单中来自喜欢的乐队的歌曲的数量的最小值尽量大 现在可以更改歌单之中的歌 输出这个最小值以及更改歌曲的次数(尽量少)以及更改之后的歌单
题解:贪心处理 最小值为一个定值n/m 先处理m之后的乐队 后处理1~m的乐队 对于不需要更改的m之后的乐队直接输出
数据
4 3
1 2 3 4
answer
1 0
1 2 3 4
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 #include<bits/stdc++.h> 8 #include<map> 9 #include<set> 10 #include<cmath> 11 #include<queue> 12 #include<bitset> 13 #include<math.h> 14 #include<vector> 15 #include<string> 16 #include<stdio.h> 17 #include<cstring> 18 #include<iostream> 19 #include<algorithm> 20 #pragma comment(linker, "/STACK:102400000,102400000") 21 using namespace std; 22 #define A first 23 #define B second 24 const int mod=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 //typedef long long ll; 29 typedef __int64 ll; 30 const ll MOD=1000000007; 31 const int INF=1000000010; 32 const ll MAX=1ll<<55; 33 const double eps=1e-5; 34 const double inf=~0u>>1; 35 const double pi=acos(-1.0); 36 typedef double db; 37 typedef unsigned int uint; 38 typedef unsigned long long ull; 39 int n,m; 40 int b[2005]; 41 int a[2005]; 42 int main() 43 { 44 scanf("%d %d",&n,&m); 45 int ans1=0; 46 int ans2=0; 47 ans1=n/m; 48 memset(b,0,sizeof(b)); 49 for(int i=1;i<=n;i++) 50 { 51 scanf("%d",&a[i]); 52 if(a[i]<=m) 53 {b[a[i]]++;} 54 } 55 for(int i=1;i<=n;i++) 56 { 57 if(a[i]>m) 58 { 59 for(int j=1;j<=m;j++) 60 { 61 if(b[j]<ans1) 62 { 63 b[j]++; 64 a[i]=j; 65 ans2++; 66 break; 67 } 68 } 69 70 } 71 } 72 int flag=1; 73 for(int i=1;i<=n;i++) 74 { 75 if(a[i]>m){ 76 flag=0; 77 break; 78 } 79 } 80 if(flag){ 81 for(int i=1;i<=n;i++) 82 { 83 if(b[a[i]]>ans1) 84 { 85 for(int j=1;j<=m;j++) 86 { 87 if(b[j]<ans1) 88 { 89 b[a[i]]--; 90 a[i]=j; 91 b[j]++; 92 ans2++; 93 break; 94 } 95 } 96 } 97 } 98 } 99 cout<<ans1<<" "<<ans2<<endl; 100 for(int i=1;i<=n;i++) 101 printf("%d ",a[i]); 102 printf(" "); 103 return 0; 104 }