A. Sum of Round Numbers
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int n,a[100005];
int main()
{
int t;read(t);
while(t--){
read(n);
int ans=0;
int b=n;
while(n){
if(n%10){
ans++;
}
n/=10;
}
cout<<ans<<endl;
int i=1;
while(b){
if(b%10){
cout<<(b%10)*i<<" ";
}
b/=10;
i*=10;
}
cout<<endl;
}
return 0;
}
B. Same Parity Summands
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int main()
{
int t;read(t);
while(t--){
int a,b;
cin>>a>>b;
if( (a%2 && !(b%2)) || ( (!(a%2)) && b%2 && a<b*2 ) || a<b) cout<<"NO\n";
else if((!(a%2))&&b%2){
cout<<"YES\n";
for(int i=1;i<b;i++) cout<<2<<" ";
cout<<a-2*(b-1)<<endl;
}
else{
cout<<"YES\n";
for(int i=1;i<b;i++) cout<<1<<" ";
cout<<a-b+1<<endl;
}
}
return 0;
}
C. K-th Not Divisible by n
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int main()
{
int t;read(t);
while(t--){
ll a,b;
cin>>a>>b;
ll l=1,r=2e9;
while(l<r){
ll mid=(l+r)/2;
if(mid-mid/a>=b){
r=mid;
}
else{
l=mid+1;
}
}
cout<<l<<endl;
}
return 0;
}
D. Alice, Bob and Candies
**node:**记得答案是最后的总量,而比较的是单次吃的数目。
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int a[1005];
int main()
{
int t;read(t);
while(t--){
int n;read(n);
for(int i=1;i<=n;i++){
read(a[i]);
}
int ans=0,l=1,r=n,a1=0,a2=0;
int num1=0,num2=0;
while(l<=r){
if(ans%2==0){
ans++;
while(num1<=num2&&l<=r){
num1+=a[l];
a1+=a[l];
l++;
}
num2=num1;
num1=0;
}
else{
ans++;
while(num1<=num2&&l<=r){
num1+=a[r];
a2+=a[r];
r--;
}
num2=num1;
num1=0;
}
}
cout<<ans<<" "<<a1<<" "<<a2<<endl;
}
return 0;
}
E. Special Elements
题意:找出连续的序列和等于该组元素的个数。
思路:先用前缀和维护区间和,然后O(n2)可以过,发现内存限制了,就把结果存进c中,每寻找到一次,就把答案给更新。
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int a;
int b[8005];
int c[8005];
int main()
{
int t;read(t);
while(t--){
int n;read(n);
for(int i=1;i<=n;i++) c[i]=0;
for(int i=1;i<=n;i++){
read(a);
b[i]=a;
b[i]+=b[i-1];
c[a]++;
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=i+2;j<=n;j++){
if(b[j]-b[i]>=1&&b[j]-b[i]<=n&&c[b[j]-b[i]]) ans+=c[b[j]-b[i]],c[b[j]-b[i]]=0;
}
}
cout<<ans<<endl;
}
return 0;
}
F. Binary String Reconstruction
把 0 放在前面,把 1 放在后面,可以发现中间有01对n2贡献了1,再插入10就会对n2再贡献10,所以就可以把n1,n2,n3给分别模拟出来了。
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int main()
{
int t;read(t);
while(t--){
int a,b,c;
cin>>a>>b>>c;
string s1,s2,s3;
s1=s2=s3="";
for(int i=0;i<=a;i++) s1+="0";//if(a) s1+="0";
for(int i=0;i<=c;i++) s3+="1";//if(c) s3+="1";
if(b>=2&&b%2==0) s3+="0";
for(int i=1;i<=(b-1)/2;i++) s2+="10";
//if(a==0) s2+="10";
if(b)
cout<<s1<<s2<<s3<<endl;
else if(a) cout<<s1<<endl;
else cout<<s3<<endl;
}
return 0;
}
G. Special Permutation
只能说外国佬思路巧妙,小于等于3是肯定不行的,那就等于4的时候,大于4的时候就首位分别插入。
#include <bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define db printf("where!\n");
#define pb push_back
using namespace std;
#define ll long long
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
int main()
{
int t;read(t);
while(t--){
int n; cin>>n;
if(n<=3)
{
cout<<-1<<'\n';
}
else
{
deque<int> dq = {3,1,4,2};
for(int i=5;i<=n;i++)
{
if(i%2==1) dq.pb(i);
else dq.push_front(i);
}
for(int x:dq)
{
cout<<x<<' ';
}
cout<<'\n';
}
}
return 0;
}