4.1总结

· · 个人记录

||A|B|C|D|E|F|总分| |:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:| |预期得分|100|100|0|100|90|100|490| |实际得分|100|8|0|60|90|30|288| |挂分|0|92|0|40|0|70|202|

反正就是挂分惨烈。

A:[GESP样题 三级] 进制转换

这道题就是要你把十进制数 N 转换成 R 进制数。

注意:

  1. 大于等于 10 的数字用字母表示。
  2. 最后答案倒序输出。

这道题还好,没有挂分。

code:

#include <iostream>
using namespace std;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    int x;
    cin>>x;
    string ans;
    while(n){
        int now =  n%x;
        if(now<=9){
            ans+=(now+'0');
        }else{
            ans+=(now-10+'A');
        }
        n/=x;
    }
    for(int i = ans.length()-1;i>=0;i--){
        cout<<ans[i];
    }
    return 0;
}

B:【CSGRound2】光骓者的荣耀

这道题就是贪心跳过最大的一段。

维护一个前缀和数组,然后求出最大区间,跳过最大区间,计算答案即可。

我挂分的原因:

计算最大区间时计算错误。我一开始是用双指针确定最大区间范围,但是我的 l 指针没有移动,导致错误。

code:

#include <iostream>
#define int long long
using namespace std;

int a[1000005];
int n,k;
int qianzhui[1000005];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>k;
    int sum = 0;
    int l = 1,r = 0;
    if(k>=n) return !(cout<<0);
    bool flag = true;
    for(int i = 1;i<n;i++){
        cin>>a[i];
        sum+=a[i];
        qianzhui[i] = qianzhui[i-1]+a[i];
    }
    int maxn = 0;
    for(int i = 1;i<n;i++){
        maxn = max(maxn,qianzhui[i+k-1]-qianzhui[i-1]);
    }
    if(k==0) return !(cout<<sum);
    cout<<sum-maxn;
    return 0;
}

C:[GESP202412 七级] 武器购买

考场上想到了 DP,但不会写就很难受。

我们先用 01 背包模板把花费不超过 Q 的最大武器强度求出来,然后再去找合法的(也就是武器强度大于等于 P 的)最小花费,输出那个花费。如果没有输出,也就是没有合法答案,输出 -1。

考场上没想到这一步 qwq。

code:

#include <iostream>
#include <algorithm>
using namespace std;

int dp[105][50005];
int n,p,q;
int c[50005],w[50005];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        fill(dp[0],dp[n+1],0);
        cin>>n>>p>>q;
        for(int i = 1;i<=n;i++){
            cin>>c[i]>>w[i];
        }
        for(int i = 1;i<=n;i++){
            for(int j = 0;j<=q;j++){
                dp[i][j] = dp[i-1][j];
                if(j>=w[i]){
                    dp[i][j] = max(dp[i][j],dp[i-1][j-w[i]]+c[i]);
                }
            }
        }
        if(dp[n][q]<p){
            cout<<-1<<endl;
            continue;
        }
        bool flag = false;
        for(int j = 0;j<=q;j++){
            if(dp[n][j]>=p){
                cout<<j<<endl;
                flag = true;
                break;
            }
        }
    }
    return 0;
}

D:[语言月赛202301] 新年快乐

就是对字符串进行处理。

首先,我们先把要处理的子字符串取出来。如果子字符串里面全是 a,那么直接输出 NULL\nHappy Chinese New Year!\n,进行下一次截取。否则就把字符串往前推一位。

怎么往前推呢?从后往前遍历,如果当前的字符是 a,那么把它变成 z,否则就把它的 ASCII 码减 1 并退出遍历。搞完之后,直接输出当前字符串,并换行。

然后就把推完的字符串放在原字符串里面比较,看有没有推完的字符串。如果有,输出 Happy New Year!,否则输出 Happy Chinese New Year!。

我挂分的原因:

在最后手写遍历的时候算法有误。用字符串自带的 find 函数会好用得多,手写还会出 bug。

code:

#include <iostream>
using namespace std;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--){
        int l,r;
        cin>>l>>r;
        l--,r--;
        string now;
        bool flag = true;
        for(int i = l;i<=r;i++){
            now+=s[i];
            if(s[i]!='a') flag = false;
        }
        if(flag){
            cout<<"NULL\nHappy Chinese New Year!\n";
            continue;
        }
        for(int i = now.length()-1;i>=0;i--){
            if(now[i]=='a'){
                now[i] = 'z';
            }else{
                now[i]--;
                break;
            }
        }
        cout<<now<<endl;
        flag = false;
        int cnt = 0;
        /*for(int i = 0;i<s.length();i++){//这是我手写的遍历,有问题
            if(cnt==now.length()) break;
            if(s[i]==now[cnt]){
                cnt++;
                flag = true;
            }else{
                cnt = 0;
                flag = false;
            }
        }*/
        if(s.find(now)!=string::npos){
            cout<<"Happy New Year!\n";
        }else cout<<"Happy Chinese New Year!\n";
    }
    return 0;
}

E:[SHOI2002] 滑雪

这道题是记忆化搜索模板。

我的 90pts 是因为没有使用记忆化,直接暴力 dfs。

使用记忆化数组 preans_{i,j} 表示第 i 行第 j 列最多能够划出去多远。下一次遍历到这个点的时候直接调用。注意要减一。

code:

#include <iostream>
#include <algorithm>
using namespace std;

int a[105][105];
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
int n,m;
int maxn = -1;
int cnt = 0;
bool vis[105][105];
int preans[105][105];
void dfs(int x,int y,int step){
    if(preans[x][y]!=0){
        cnt = max(cnt,step+preans[x][y]-1);
        return ;
    }
    vis[x][y] = true;
    cnt = max(cnt,step);
    for(int i = 0;i<4;i++){
        int tx = x+dx[i],ty = y+dy[i];
        if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&vis[tx][ty]==false&&a[x][y]>a[tx][ty]){
            dfs(tx,ty,step+1);
        }
    }
    vis[x][y] = false;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++){
//          fill(vis[0],vis[n+1],false);
            cnt = 0;
            dfs(i,j,1);
            maxn = max(maxn,cnt);
            preans[i][j] = cnt;
        }
    }
    cout<<maxn;
    return 0;
}

F:[USACO19OPEN] Fence Planning S

这道题我的做法是 dfs 所有连通块,用一个数组保存一个连通块的最大 x,最小 x,最大 y 和最小 y。后面计算答案时遍历每个连通块,利用公式 C=2\cdot(x_{max}-x_{min}+y_{max}-y_{min}),然后打擂台求出最小周长即可。

我挂分的原因:

dfs 的时候没有正确使用标记数组,导致 dfs 的时候有些情况会死循环,导致 MLE。

code:

#include <iostream>
#include <vector>
using namespace std;

struct node{
    int x,y;
}cow[100005];
bool belong[100005];
int cnt = 1;
int n,m;
struct node2{
    int xx,yy;
    int xxx = 0x7fffffff,yyy = 0x7fffffff;
}result[100005];
vector<int> g[100005];
void dfs(int now,int fa){
    result[cnt].xx = max(result[cnt].xx,cow[now].x),result[cnt].yy = max(result[cnt].yy,cow[now].y);
    result[cnt].xxx = min(result[cnt].xxx,cow[now].x),result[cnt].yyy = min(result[cnt].yyy,cow[now].y);
    belong[now] = true;
    for(auto it:g[now]){
        if(it!=fa&&belong[it]==false) dfs(it,now);
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        cin>>cow[i].x>>cow[i].y;
    }
    for(int i = 1;i<=m;i++){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b),g[b].push_back(a);
    }
    for(int i = 1;i<=n;i++){
        if(belong[i]!=0) continue;
        dfs(i,0);
        cnt++;
    }
    int ans = 0x7fffffff;
    for(int i = 1;i<cnt;i++){
        ans = min(ans,2*(result[i].xx-result[i].xxx+result[i].yy-result[i].yyy));
    }
    cout<<ans;
    return 0;
}

完结撒花!