2018國慶第五場個人賽
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n— the number of cards with digits that you have (1≤n≤100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s1,s2,…,sn. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Sample Input
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
#include <iostream> #include <algorithm> #include<cstdio> #include<stack> #include <deque> #include <cstdlib> #include <cstring> #include <string> #include <map> #include <deque> #include <vector> using namespace std; typedef long long ll; map<ll,ll> M; int main() { int n,i,ans=0,sum=0,len=0; char s[110]; cin>>n; ans=n/11; cin>>s; len=strlen(s); for(i=0;i<len;i++) { if(s[i]=='8') sum++; } ans=min(ans,sum); cout<<ans<<endl; return 0; }
You are given a positive integer n.
Let S(x) be sum of digits in base 10 representation of x, for example, S(123)=1+2+3=6, S(0)=0.
Your task is to find two integers a,b, such that 0≤a,b≤n, a+b=n and S(a)+S(b) is the largest possible among all such pairs.
Input
The only line of input contains an integer n(1≤n≤1012).
Output
Print largest S(a)+S(b) among all pairs of integers a,b, such that 0≤a,b≤n and a+b=n.
Sample Input
Input
35
Output
17
Input
10000000000
Output
91
Hint
In the first example, you can choose, for example, a=17 and b=18, so that S(17)+S(18)=1+7+1+8=17. It can be shown that it is impossible to get a larger answer.
In the second test example, you can choose, for example, a=5000000001 and b=4999999999, with S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.
這是一個錯誤的程式碼 樣例123
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
ll a,b,i,n,ans1=0,ans2=0,ans=0;
cin>>n;
if(n%2!=0)
{
a=n/2;
b=n/2+1;
}
else
{
a=n/2-1;
b=n/2+1;
}
while(a)
{
ans1+=a%10;
a/=10;
}
while(b)
{
ans2+=b%10;
b/=10;
}
ans=ans1+ans2;
cout<<ans<<endl;
return 0;
}
這是AC程式碼
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
ll n,k=0,kk=0,ans1=0,ans2=0,t;
cin>>n;
while(k<=n)
{
k=k*10+9;
t=k;
if(k>n)
{
t=(t-9)/10;
break;
}
}
k=t;
kk=n-k;
//cout<<k<<" "<<kk<<endl;
while(k)
{
ans1+=k%10;
k/=10;
}
while(kk)
{
ans2+=kk%10;
kk/=10;
}
cout<<ans1+ans2<<endl;
return 0;
}
use MathJax to parse formulas
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:
- An 'L' indicates he should move one unit left.
- An 'R' indicates he should move one unit right.
- A 'U' indicates he should move one unit up.
- A 'D' indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.
Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
Output
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.
Sample Input
Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2
Hint
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.
In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
char s[100100];
int main()
{
memset(s,0,sizeof(s));
ll n,ans=0,i,j,l=0,r=0,u=0,d=0;
cin>>s;
n=strlen(s);
if(n%2!=0)
cout<<"-1"<<endl;
else
{
for(i=0;i<n;i++)
{
if(s[i]=='L')
l++;
else
if(s[i]=='R')
r++;
else
if(s[i]=='U')
u++;
else
if(s[i]=='D')
d++;
}
ll ans1=0,ans2=0;
ans1=max(l,r)-min(l,r);
ans2=max(u,d)-min(u,d);
if((ans1+ans2)%2==1)
cout<<"-1"<<endl;
else
cout<<(ans1+ans2)/2<<endl;
}
return 0;
}
Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?
Input
The first and only line contains two integers x and y (3 ≤ y < x ≤ 100 000) — the starting and ending equilateral triangle side lengths respectively.
Output
Print a single integer — the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.
Sample Input
Input
6 3
Output
4
Input
8 5
Output
3
Input
22 4
Output
6
Hint
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do .
In the second sample test, Memory can do .
In the third sample test, Memory can do:
.
【題意】
現有邊長為x的等邊三角形,Memory想要將其變成邊長為y的等邊三角形
現規定Memory每秒能夠改變一條邊的大小,但要保證改變後的三條邊仍能構成一個三角形
問,最少需要多少時間才能變為邊長為y的等邊三角形
【型別】
貪心,逆推
【分析】
一開始,正常人的想法就是如何改變邊長為x的等邊三角形的邊,使得其變為邊長為y的等邊三角形
但是在不斷嘗試過程中,就會發現,貌似不是非常好處理
根據定理"三角形兩邊之和大於第三邊,兩邊之差小於第三邊"
就很難確定最初應該把x減少為多少合適,如果減少太多,勢必會使得另外兩條邊減少速度過慢
(22,22,22)→(4,22,22)→(4,19,22)→(4,19,16)→(4,13,16)→(4,13,10)→(4,7,10)→(4,7,4)→(4,4,4)
但如果減少太少,同樣會影響速率
應該是個怎麼樣的度,總是把握不好
這時,把問題反過來想想,發現有出路
減法控制不好,相對來說加法就簡單得多
為了儘快使y->x,那每秒都要使得y增加得儘可能多
所以每次選取最短邊,將其變為另兩條邊之和-1("三角形兩邊之和大於第三邊")
直到三條邊都大於等於x為止
AC程式碼
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stack>
#include <deque>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
using namespace std;
typedef long long ll;
map<ll,ll> M;
int main()
{
ll x,y,a,b,c,ans=0;
cin>>x>>y;
a=b=c=y;
while(1)
{
if(a>=x&&b>=x&&c>=x)
break;
if(a<=b&&a<=c)
a=b+c-1;
else
if(b<=a&&b<=c)
b=a+c-1;
else
if(c<=a&&c<=b)
c=a+b-1;
ans++;
}
cout<<ans<<endl;
return 0;
}