Problem A Thanks, TuSimple!

比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5969

补题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4090

题解:二分+思维

1、配对的人喜好肯定不一样;

2、按不同喜好对身高排序;

3、O(n)方法匹配

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=300000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
ll ans,cnt,flag,temp,sum;
char str;
struct node{
    int h,flag;
    bool operator <(const node&S)const{
        if(flag==S.flag){
            return h<S.h;
        }
        return flag<S.flag;
    }
}a[N],b[N];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i].h);
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&b[i].h);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i].flag);
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&b[i].flag);
        }
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        ans=0;
        l=n+1;
        for(int i=1;i<=n;i++){
            if(a[i].flag){
                l=i;
                break;
            }
        }
        for(int i=1;i<=m;i++){
            if(l>n)
                break;
            if(!b[i].flag){
                if(a[l].h<b[i].h){
                    ans++;
                    l++;
                }
            }else{
                break;
            }
        }
        l=m+1;
        for(int i=1;i<=m;i++){
            if(b[i].flag){
                l=i;
                break;
            }
        }
        for(int i=1;i<=n;i++){
            if(l>m)
                break;
            if(!a[i].flag){
                if(b[l].h<a[i].h){
                    ans++;
                    l++;
                }
            }else{
                break;
            }
        }
        cout<<ans<<endl;
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem B Potion

比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5970

补题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4091

C++版本一

题解:高精度

1、对原数循环除以2;

2、添加每次的结果到ans数组;

3、数据范围二进制大概2的3400次;

4、代码不是最优(比如如果原数已经0,可以直接退出);

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N],c[N];
char str[N];
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        //scanf("%d",&n);
        cin>>str;
        int len=strlen(str);
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        for(int i=1;i<=len;i++){
            a[i]=str[len-i]-'0';
        }
        c[0]=1;
        for(int i=0;i<3400+10;i++){
            int now=0;
            for(int j=len;j>=1;j--){
                a[j]+=now*10;
                now=0;
                if(a[j]%2==1){
                    now=1;
                }
                a[j]/=2;
            }
            int limit=max(len,c[0])+100;
            for(int j=1;j<=limit;j++){
                c[j]+=a[j];
                if(c[j]>=10){
                    c[j+1]+=c[j]/10;
                    c[j]%=10;
                }
                if(c[j]){
                    c[0]=max(c[0],j);
                }
            }
        }
        for(int i=c[0];i>=1;i--){
            cout<<c[i];
        }
        cout<<endl;
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Python版本一

t = int(input())
while t > 0:
    n = int(input())
    ans = int(0)
    t -= 1
    while n > 0:
        n //= 2
        ans += n
    print(ans)

Problem C Robot Cleaner I

比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5971

补题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4092

C++版本一

题解:

1、有个东西叫%1d;

2、哈希坐标;

3、剪枝;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
//#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=2000+100;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m;
ll k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a,b,mp[N][N];
int vis[N][300];
char str[N],str1[N];
struct node{
    int x,y,step;
}tmp,start,front1;
int command(int x,int y){
    return 81*mp[x][y]+27*mp[x-1][y]+9*mp[x+1][y]+3*mp[x][y-1]+mp[x][y+1];
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        scanf("%d%d%lld",&a,&b,&k);
        scanf("%s",str);
        sum=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%1d",&mp[i][j]);
                if(mp[i][j]==2)
                    sum++;
            }
        }
        start.x=a;
        start.y=b;
        start.step=0;
        memset(vis,-1,sizeof(vis));
        ans=0;
        front1=start;
        while(1){
            int opid=command(front1.x,front1.y);
            char op=str[opid];
            if(vis[(front1.x-1)*m+front1.y][opid]==ans)
                break;
            if(sum<=ans)
                break;
            vis[(front1.x-1)*m+front1.y][opid]=ans;
            tmp=front1;
            tmp.step++;
            if(tmp.step>k)
                break;
            //cout<<front1.x<<" "<<front1.y<<" "<<op<<" "<<mp[front1.x][front1.y]<<" "<<vis[front1.x][front1.y]<<endl;
            if(op=='U'){
                tmp.x--;
            }else if(op=='D'){
                tmp.x++;
            }else if(op=='L'){
                tmp.y--;
            }else if(op=='R'){
                tmp.y++;
            }else if(op=='P'){
                if(mp[front1.x][front1.y]==2){
                    mp[front1.x][front1.y]=0;
                    ans++;
                }
            }else if(op=='I'){
                break;
            }
            if(mp[tmp.x][tmp.y]==1){
                break;
            }
            front1=tmp;
        }
        printf("%d\n",ans);
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

题解:注意标记,vis[i][j] i表示对坐标的哈希,j表示当前计算的x,值为之前得到多少汽油了,因为如果再次经过这个位置,两次经过的中间得到多汽油,并且改变了图中的2,所以要继续走,知道,走到一个位置,汽油数不增加,然后就是注意一些剪枝。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; 
int n,m;
int a,b;
ll k;
char mp[2010][2010];
int vis[2100][255];
char step[255];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		scanf("%d%d%lld",&a,&b,&k);
		scanf("%s",step+1);
		int sum=0;
		for(int i=0;i<=n*m;i++)
			for(int j=0;j<=250;j++)
				vis[i][j]=-1;
		
		for(int i=1;i<=n;i++)
		{
			scanf("%s",mp[i]+1);
			for(int j=1;j<=m;j++)
			{
				if(mp[i][j]=='2')
					sum++;
			}
		}
		int ans=0;
		int x;
		int xx,yy;
		int st=0;
		while(1)
		{
			x=81*(mp[a][b]-'0')+27*(mp[a-1][b]-'0')+9*(mp[a+1][b]-'0')+3*(mp[a][b-1]-'0')+(mp[a][b+1]-'0');
			if(vis[(a-1)*m+b][x]==ans) break;
			vis[(a-1)*m+b][x]=ans;
			x++;
		//	cout<<a<<" "<<b<<" "<<x<<" "<<step[x]<<" "<<endl;
			if(step[x]=='U') xx=a-1,yy=b;
			if(step[x]=='D') xx=a+1,yy=b;
			if(step[x]=='L') xx=a,yy=b-1;
			if(step[x]=='R') xx=a,yy=b+1;
			if(step[x]=='P') 
			{
				if(mp[a][b]=='2')
				{
					ans++;
					mp[a][b]='0';	
				} 
				if(ans>=sum) break;
				xx=a,yy=b;
			}
			if(step[x]=='I') 
			{
				xx=a,yy=b;
				break;
			}
			if(mp[xx][yy]=='1') break;
			st++;
			if(st==k) break;
			a=xx;
			b=yy;
		}
		printf("%d\n",ans);
		
	}
	return 0;
}

 

Problem E Potion

比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5973

补题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4094

题解:

1、向上依次寻找可代替药水;

2、如果当前不能满足则找不到;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N],b[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&b[i]);
        }
        ans=1;
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                if(a[i]>b[j]){
                    a[i]=a[i]-b[j];
                    b[j]=0;
                }else{
                    b[j]=b[j]-a[i];
                    a[i]=0;
                    break;
                }
                if(j==n){
                    ans=0;
                }
            }//cout<<a[i]<<endl;
        }
        cout<<(ans?"Yes":"No")<<endl;
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem G Postman

比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5975

补题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4096

 

题意:x=0的位置是邮局,邮递员每次可以带k封信,需要回邮局拿信,最后送完信后不必回邮局,求最小路程

C++版本一

题解:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define FI first
#define SE second
#define PB push_back
typedef pair<int,LL>PII;
const int N=2e5+7;
const LL INF=1e17,mod=998244353;
int n,m,k;
LL a[N],b[N];
LL ans;
int main()
{
    int t;
    cin>>t;
    while(t--){
            ans=0;
        scanf("%d%d",&n,&k);
        LL x;
        int cnt=0,cot=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&x);
            if(x==0)continue;
            if(x<0)a[++cnt]=-x;
            else b[++cot]=x;
        }
        sort(a+1,a+cnt+1);
        sort(b+1,b+cot+1);
        if(a[cnt]>b[cot]){
            ans+=a[cnt];
            cnt=max(cnt-k,0);
        }
        else {
            ans+=b[cot];
            cot=max(cot-k,0);
        }
        //cout<<ans<<endl;
        while(cnt>0||cot>0){
            if(a[cnt]>b[cot]){
                ans+=a[cnt]*2;
                cnt=max(cnt-k,0);
            }
            else{
                ans+=b[cot]*2;
                cot=max(cot-k,0);
            }
        }
        printf("%lld\n",ans);
    }
	return 0;
}

C++版本二

题解:

1、从两边往中间走;

2、减掉一个绝对值最大的点;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
ll ans,cnt,flag,temp,sum;
ll c[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    srand((unsigned)time(NULL));
    scanf("%d",&t);
    //t=rand()%(2000)+10000;
    while(t--){
        scanf("%d%d",&n,&k);
        ll maxl=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&c[i]);
            maxl=max(maxl,abs(c[i]));
        }
        sort(c+1,c+n+1);
        int l=0;
        int r=n+1;
        ans=0;
        for(int i=n;i>=1;i--){
            if(c[i]>0){
                r=i;
            }else{
                break;
            }
        }
        for(int i=1;i<=n;i++){
            if(c[i]<0){
                l=i;
            }else{
                break;
            }
        }

        for(int i=n;i>=r;i-=k){
            ans+=2ll*abs(c[i]);
        }
        for(int i=1;i<=l;i+=k){
            ans+=2ll*abs(c[i]);
        }
        cout<<ans-maxl<<endl;
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem J Extended Twin Composite Number

比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5977

补题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4099

题意:找到两个数x,y使得满足x+n=y,并且x,y都是合数

题解:构造

1、特判n==1时;

2、任何数*2肯定合数,+n就是3*n,也肯定合数;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};


int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);


    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&n);
        if(n==1){
            cout<<9<<" "<<10<<endl;
            continue;
        }
        cout<<2*n<<" "<<3*n<<endl;
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}