2024第一次测试题解

题组链接:

第一次测试 - Virtual Judge (vjudge.net)

一.A - Determinant

直接输出a*d-b*c

#include<stdio.h>


int main(){
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    printf("%d\n",a*d-b*c);
    return 0;
}

二.B - Quizzes

#include<stdio.h>
int main()
{
    int n,x,i,j,ans=0;
    char k;
    scanf("%d%d",&n,&x);
    ans+=x;
    getchar();
    for(i=0;i<n;i++)
    {
        scanf("%c",&k);
        if(k=='o')
        {
            ans++;
        }
        else if(k=='x')
        {
            ans--;
            if(ans<0)
                ans=0;
        }
    }
    printf("%d\n",ans);
    return 0;
}

三.C - Super Ryuma

可以将题目中的三种情况,转化为两种:

1.斜着走
2.两坐标绝对值差之和小于等于3

两两匹配刚好种情况:

所以一共对应的走两步的情况:1+2, 1+1, 2+2;

其余的就是走三步的情况(所有两点都可以走三部来到达,斜着走两次再走一步)

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

#define int long long
typedef pair<int,int> pr;
#define fr(i,l,r) for(int i=l;i<=r;i++)
#define fe(i,r,l) for(int i=r;i>=l;i--)
#define pb(x) push_back(x)
#define all(a) a.begin(),a.end()
#define fi first
#define se second

const int N = 1e6+10;
string YES="YES",Yes="Yes",yes="yes",NO="NO",No="No",no="no";
const int mod=998244353,inf=LONG_LONG_MAX;
int dx[]={0,0,-1,0,1},dy[]={0,-1,0,1,0};
int n,m,k;

int a[N];

void solve()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    c -= a, d -= b, a = 0, b = 0;
    int ans = 0;
    c = abs(c), d = abs(d);//转化到第一象限中
    if(c == 0 && d == 0){
        ans = 0;
    }
    else if (c == d || c + d <= 3){//1或者2
        ans = 1;
    }
    else if (abs(c - d) <= 3 || c + d <= 6){//1 + 2 , 2 + 2
        ans = 2;
    }
    else if (!((c & 1) ^ (d & 1))){//c和d奇偶同性,1 + 1
        ans = 2;
    }
    else{
        ans = 3;
    }
    cout << ans << endl;
}

signed main()
{
    ios;
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}

四.D - One Time Swap

该题意转化后为:进行交换的两个字母要是不相等就ans++,否则则只保留第一次的情况;

所以采用map进行存储 i 之前的所有值字母个数,要是不相等就加上,要是相等就记上一次;

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

#define int long long
typedef pair<int,int> pr;
#define fr(i,l,r) for(int i=l;i<=r;i++)
#define fe(i,r,l) for(int i=r;i>=l;i--)
#define pb(x) push_back(x)
#define all(a) a.begin(),a.end()
#define fi first
#define se second

const int N = 1e6+10;
string YES="YES",Yes="Yes",yes="yes",NO="NO",No="No",no="no";
const int mod=998244353,inf=LONG_LONG_MAX;
int dx[]={0,0,-1,0,1},dy[]={0,-1,0,1,0};
int n,m,k;

int a[N];

void solve()
{
    string s;
    cin>>s;
    n = s.size();
    s = " " + s;
    map<int,int>mp;
    int ans = 0;
    int f = 0;
    fr(i,1,n){
        fr(j,0,25){
            char c = j + 'a';
            if(s[i] == c){
                if(mp[s[i]-'a']&&f ==0){
                    f = 1;
                }
                else{
                    continue;
                }
            }
            ans += mp[j];
        }
        mp[s[i]-'a'] ++;
    }
    cout<<ans<<endl;
}

signed main()
{
    ios;
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}

五.E - increment of coins(简单dp)

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
#define ll long long
char a;
double dp[101][101][101];
ll ans;
int main()
{
	int a, b, c, total;
	cin >> a >> b >> c;
	for (int i = 99; i >= a; i--)
		for (int j = 99; j >= b; j--)
			for (int k = 99; k >= c; k--) {
				double sum = i + j + k;
				dp[i][j][k] = ((double)i / sum) * (dp[i + 1][j][k] + 1) + ((double)j / sum) * (dp[i][j + 1][k] + 1) + ((double)k / sum ) * (dp[i][j][k + 1] + 1);
			}
			printf("%.9lf",dp[a][b][c]);
	return 0;
}

六.F - Third Avenue(简单搜索)

用一个数组存储所有字母的传送门,然后遇到字母就遍历该数组进行传送即可,当然也可以不进行传送

#include<stdio.h>
int n,m,x,y,X,Y;
bool vis[2001][2001];
char a[2001][2001];
int b[100],dx[4]= {0,0,-1,1},dy[4]= {1,-1,0,0};
struct node
{
    int x,y;
} chuang[30][10000];

struct Node
{
    int x,y,t;
} p[4000001];

int bfs()
{
    int r=1,l=-1,i,j;
    vis[x][y]=1;
    p[0].x=x,p[0].y=y;
    while(++l<r)
    {
        if(a[p[l].x][p[l].y]=='G')
        {
            return p[l].t;
        }
        for(i=0; i<4; i++)
        {
            int xx=p[l].x+dx[i],yy=p[l].y+dy[i],tt=p[l].t+1;
            if(xx<0||xx>=n||yy<0||yy>=m) continue;
            if(vis[xx][yy]==0&&a[xx][yy]!='#')
            {
                //printf("%d,%d,%d\n",xx,yy,tt);
                vis[xx][yy]=1;
                p[r].x=xx,p[r].y=yy,p[r++].t=tt;
            }
        }
        if(a[p[l].x][p[l].y]>='a'&&a[p[l].x][p[l].y]<='z')
        {
            int xx=p[l].x,yy=p[l].y,tt=p[l].t+1;

            char k=a[xx][yy]-'a';
            for(i=0; i<b[k]; i++)
            {
                //printf("%d,%d,%d\n",xx,yy,tt);
                if(xx==chuang[k][i].x&&yy==chuang[k][i].y||vis[chuang[k][i].x][chuang[k][i].y]!=0) continue;

                //printf(" %d,%d,%d\n",xx,yy,tt);
                xx=chuang[k][i].x,yy=chuang[k][i].y;
                vis[xx][yy]=1;
                //printf(" %d,%d,%d\n",xx,yy,tt);
                p[r].x=xx,p[r].y=yy,p[r].t=tt;
                r++;
            }
        }
    }
    return -1;
}

int main()
{
    int i,j;
    scanf("%d%d",&n,&m);
    for(i=0; i<n; i++)
    {
        getchar();
        for(j=0; j<m; j++)
        {
            scanf("%c",&a[i][j]);
            if(a[i][j]=='S')
            {
                x=i,y=j;
            }
            else if(a[i][j]=='G')
            {
                X=i,Y=j;
            }
            else if(a[i][j]>='a'&&a[i][j]<='z')
            {
                chuang[a[i][j]-'a'][b[a[i][j]-'a']].x=i;
                chuang[a[i][j]-'a'][b[a[i][j]-'a']++].y=j;
            }
        }
    }
    //for(i=0;i<n;i++)
    //printf("%s\n",a[i]);
    printf("%d\n",bfs());
    return 0;
}

七.G - Programming Contest(01背包变式)

其实考察了01背包的部分性质;只是由于给的数据过大,无法采用普通数组进行转化,我代码是直接采用map数字来进行实现的;

由于数据N=40,在最坏的情况下,所有组合的数都不会重合的话,那么将会产生2^40 - 1次方个数,空间和时间都会超限;

所以我们可以采取将数据一分2,那么将会得到两个2 ^ 20 - 1的数组a和b,然后从中知道和最大并且小于T的值,遍历其中一个数组a,在那个数组中找到比T - a[i]小于等于中最大的数;

我的代码:

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

#define int long long
typedef pair<int,int> pr;
#define fr(i,l,r) for(int i=l;i<=r;i++)
#define fe(i,r,l) for(int i=r;i>=l;i--)
#define pb(x) push_back(x)
#define all(a) a.begin(),a.end()
#define fi first
#define se second

const int N = 1e6+10;
string YES="YES",Yes="Yes",yes="yes",NO="NO",No="No",no="no";
const int mod=998244353,inf=LONG_LONG_MAX;
int dx[]={0,0,-1,0,1},dy[]={0,-1,0,1,0};
int n,m,k;

int a[N];

void solve()
{
    cin >> n >> k;
    map<int, int> mp1,mp2;
    fr(i,1,n) cin>>a[i];
    mp1[0] = 1,mp2[0] = 1;
    fr(i,1,n/2){
        vector<int>vt;
        for(auto [x,y] : mp1){
            if(x + a[i] <= k){
                vt.push_back(x + a[i]);
            }
        }
        for(auto x : vt){
            mp1[x] = 1;
        }
    }
    fr(i,n/2 + 1,n){
        vector<int>vt;
        for(auto [x,y] : mp2){
            if(x + a[i] <= k){
                vt.push_back(x + a[i]);
            }
        }
        for(auto x : vt){
            mp2[x] = 1;
        }
    }
    int ans = 0;
    for(auto [x,y] : mp1){
        auto [xx,yy] = *prev(mp2.upper_bound(k - x));
        ans = max(ans, xx + x);
    }
    cout << ans << endl;
}

signed main()
{
    ios;
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}

 题解中代码:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
void chmax(ll& a, ll b){ if(a < b) a = b; }

int main(){
    ll N, T;
    cin >> N >> T;
    vector<ll> A(N);
    for(ll& a : A) cin >> a;
    vector<ll> B{0}, C{0};
    for(ll a : A){
        for(ll i = B.size(); i--; ) B.push_back(B[i] + a);
        swap(B, C);
    }
    sort(B.begin(), B.end(), greater());
    
    ll ans = 0;
    for(ll x : C){
        if(x > T) continue;
        x += *lower_bound(B.begin(), B.end(), T - x, greater());
        chmax(ans, x);
    }
    cout << ans << endl;
}

建议多去使用c++容器以及函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值