MG loves apple
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1066 Accepted Submission(s): 200
Problem Description
MG is a rich boy. He has n apples, each has a value of V(0<=V<=9).
A valid number does not contain a leading zero, and these apples have just made a valid N digit number.
MG has the right to take away K apples in the sequence, he wonders if there exists a solution: After exactly taking away K apples, the valid N−K digit number of remaining apples mod 3 is zero.
MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?
Input
The first line is an integer T which indicates the case number.(1<=T<=60)
And as for each case, there are 2 integer N(1<=N<=100000),K(0<=K<N) in the first line which indicate apple-number, and the number of apple you should take away.
MG also promises the sum of N will not exceed 1000000。
Then there are N integers X in the next line, the i-th integer means the i-th gold’s value(0<=X<=9).
Output
As for each case, you need to output a single line.
If the solution exists, print”yes”,else print “no”.(Excluding quotation marks)
Sample Input
2 5 2 11230 4 2 1000
Sample Output
yes no
题意:
从n个数中拿出k个能否使得剩下的数合法(没有前缀零)并且能被3整除
思路:
官方题解
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
const int MAXN=100010;
string s;
int num[MAXN];
int S[3];
int main()
{
std::ios::sync_with_stdio(0);
int A,B,C,n,k,T,t;
bool flag0,flag1,E1,E2;
cin>>T;
while(T--)
{
flag0=flag1=t=0;
S[0]=S[1]=S[2]=0;
E1=E2=0;
int sum=0;
cin>>n>>k;
cin>>s;
for(int i=0; i<s.size(); i++)
{
num[i]=(s[i]-'0')%3,sum+=s[i]-'0';
S[num[i]]++;
if(!num[i]&&s[i]!='0') flag0=1;
if(s[i]=='0')flag1=1;
if(!flag0&&s[i]=='0')
t++;
if(!flag1&&num[i]==1)
E1=1;
if(!flag1&&num[i]==2)
E2=1;
}
bool flag=0;
if(!flag0)t--;
for(C=0; C<=min(k,S[2]); C++)
{
B=(sum-C*2)%3;
while(B<=min(k,S[1])&&B+C<=k)
{
A=k-B-C;
if(A<=S[0])
if(A>=t||(E1&&B<S[1])||(E2&&C<S[2]))
{
cout<<"yes\n";
flag=1;
break;
}
B+=3;
}
if(flag)
break;
}
if(!flag)
cout<<"no\n";
}
return 0;
}