河西 CSP-J 国庆集训D05课后总结

· · 学习·文化课

100+50+10+20=180

省流:rk4

T1

题意:在一个字符串矩阵中分出若干个 3 \times 3 矩阵,看这里有多少个不同种类的矩阵。

思路:因为我们需要去重,所以我们很轻易就可以想到用桶,而数组又不能用char类型作为下标,因此我们用map来解决,下表类型我们可以定义成string

因为二维下标只能用vector,所以我们将这个矩阵降维成一个字符串来进行存储。

得到这个结论之后,就很简单一个桶计数问题,切了。

std:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define N 3005
using namespace std;
char s[N][N];
map<string,int>t;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("painting.in","r",stdin);
    freopen("painting.out","w",stdout); 
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>s[i][j];
        }   
    } 
    int cnt=0;
    for(int i=1;i<=n;i+=3)
    {
        for(int j=1;j<=m;j+=3)
        {
            string ans="";
            ans+=s[i][j];
            ans+=s[i][j+1];
            ans+=s[i][j+2];
            ans+=s[i+1][j];
            ans+=s[i+1][j+1];
            ans+=s[i+1][j+2];
            ans+=s[i+2][j];
            ans+=s[i+2][j+1];
            ans+=s[i+2][j+2];
            t[ans]++;
            if(t[ans]==1) cnt++;
        }
    }
    cout<<cnt<<endl;
    return 0;
}

T2

题意:n 本书带走与不带走的收益给出,最多能带走 m 本,问:他能获得的最大收益。

思路:因为是最多带走 m 本,所以带走的本数可以是 0 \sim m 本。因此,我们需要考虑很多种情况。

但是哪种情况是最趋近最优解的呢?

首先我们肯定要让收益尽可能多,而带走与不带走两种选择我们肯定要选其中能拿更多钱的,所以,我们需要按照带走与不带走的差值从大到小排序,反正差距越大,我们能拿到的钱就越多。

30pts: 数据范围很小,所以直接选和不选的板子就过了。

正解:如果不带走,收益为 b_i ,如果带走,收益增加 a_i-b_i,这时候直接一手贪心。当然了,sort然后遍历选也可。 ::::info[std]

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=5e5+5;
struct book
{
    int tk,ntk;
}a[N];
int n,m;
bool cmp(book x,book y)
{
    return x.tk>y.tk;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("book.in","r",stdin);
    freopen("book.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i].tk;
    for(int i=1;i<=n;i++) cin>>a[i].ntk;
    int choose=0,ans=0;
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        if(choose<m&&a[i].tk>a[i].ntk)
        {
            ans+=a[i].tk;
            choose++;
        }
        else ans+=a[i].ntk;
    }
    cout<<ans<<endl;
    return 0;
}

::::

T3

30pts思路:每一次输入 l,r 之后,直接将这一段数组映射到临时数组里,然后排序得到,而注意不能用原数组进行排序!然后进行计数,判定 v_i-v_{i-1}>1,然后输出符合要求的数量就可以。 ps:这里如果遇到不符合要求的需要立刻跳出,具体原因见题面。

特殊性质20pts:这里说 a_i \leq 2 ,说明 a_i \in [1,2],因此我们可以利用这个性质推导得出:∵ 2-1=1 ,所以区间内的乐曲,我们都是可以练完的,所以直接输出r-l+1即可。

30+20pts:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e5+4;
int a[N],v[N];
int n,m,maxx=INT_MIN;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//  freopen("instrument.in","r",stdin);
//  freopen("instrument.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++) 
    {
        cin>>a[i];
        maxx=max(maxx,a[i]);
    }
    cin>>m;
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        if(maxx<=2)
        {
            cout<<r-l+1<<endl;
            continue;
        }
        else 
        {
            int id=0,cnt=1;
            for(int i=l;i<=r;i++)
            {
                id++; 
                v[id]=a[i];
            }
            sort(v+1,v+1+id);
            for(int i=2;i<=id;i++)
            {
                if(v[i]-v[i-1]>1) break;
                else cnt++;
            }
            cout<<cnt<<endl;    
        }
    }
    return 0;
}

::::success[正解]{open} 其实这道题目的本质上是统计在一个区间中有多少种连续的整数,因此,我们可以利用到桶的思想,把重复的数字放进一个计数数组中,定义 s_{i,j} 为前 i 个数中出现了多少次 j ,由此得到差值计算方程s[i][r]-s[i][l-1] ,没错,这也是前缀和。

然后考虑计数的角度。在这里的话,首先如果这个差值 =0 ,那么说明数没有出现过,而如果这时候计数过程已经开始,说明遇到难度跨越过高的乐曲了,这时候就不练了,而如果还没开始计数,说明没有遇到可以开始计数的难度,这时候就继续。

std:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e5+4,V=65;
int a[N],s[V][N];
int n,m,maxx=INT_MIN;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//  freopen("instrument.in","r",stdin);
//  freopen("instrument.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++) 
    {
        cin>>a[i];
        for(int v=1;v<=50;v++)
        {
            s[v][i]=s[v][i-1]+(v==a[i]?1:0);
        }
    }
    cin>>m;
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        int cnt=0;
        for(int i=1;i<=50;i++)
        {
            int cha=s[i][r]-s[i][l-1];
            if(cha==0)
            {
                if(cnt>0) break;
                else continue;
            }
            cnt+=cha;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

::::

T4

20pts: n \leq 5 ,是个人都能想到暴力枚举。 ::::error[暴力代码]{open}

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[305];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//  freopen("cabinet.in","r",stdin);
//  freopen("cabinet.out","w",stdout);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    if(n<=5)
    {
        if(m==1)
        {
            sort(a+1,a+1+n);
            cout<<a[n]<<endl;
            return 0;
        }
        else if(m==2)
        {
            int maxn=INT_MIN;
            for(int i=1;i<n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    maxn=max(maxn,abs(a[i]-a[j]));
                }
            }
            cout<<maxn<<endl;
            return 0;
        }
        else if(m==3)
        {
            int maxn=INT_MIN;
            for(int i=1;i<n-1;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    for(int k=j+1;k<=n;k++)
                    {
                        maxn=max(maxn,abs(a[i]-a[j])+abs(a[j]-a[k]));
                    }
                }
            }
            cout<<maxn<<endl;
            return 0;       
        }
        else if(m==4)
        {
            int maxn=INT_MIN;
            for(int i=1;i<n-2;i++)
            {
                for(int j=i+1;j<n-1;j++)
                {
                    for(int k=j+1;k<n;k++)
                    {
                        for(int l=k+1;l<=n;l++)
                        {
                            maxn=max(maxn,abs(a[i]-a[j])+abs(a[j]-a[k])+abs(a[k]=a[l]));
                        }
                    }
                }
            }
            cout<<maxn<<endl;
            return 0;                   
        }
        else if(m==5)
        {
            cout<<abs(a[1]-a[2])+abs(a[2]-a[3])+abs(a[3]-a[4])+abs(a[4]-a[5])<<endl;
            return 0; 
        }
    }
    return 0;
}

::::

60pts思路:

首先,我们可以确定,对于每一个 a_i ,有放进橱窗和不放进橱窗两种状态,那么这时候我们就会想到选和不选,当然了,选的状态是在目前选的数量不超过 m 的前提条件下才能选,不然只能不选。而这个跟全排列不一样的点在于,这个不能调换前后顺序。

最后计算吸引力的时候就无需多讲。

接下来是剪枝。对于目前需要判定状态的 a_i ,如果接下来就算全部选都 <m 的话,这个方案就无法成立,所以直接return。

::::info[60pts Code]{open}

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[305],vis[305];
int n,m,maxn=INT_MIN;
void dfs(int i,int cnt) 
{
    if(i>n)
    {
        if(cnt==m)
        {
            int sum=0;
            for(int j=1;j<cnt;j++)
            {
                int d=vis[j]-vis[j-1];
                if(d<0) d*=-1;
                sum+=d;
            }   
            maxn=max(maxn,sum);
        } 
        return;
    }
    if(cnt+(n-i+1)<m) return ;
    dfs(i+1,cnt);
    if(cnt<m)
    {
        vis[cnt]=a[i];
        dfs(i+1,cnt+1);
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//  freopen("cabinet.in","r",stdin);
//  freopen("cabinet.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    dfs(1,0);
    cout<<maxn<<endl;
    return 0;
}

::::

::::success[正解]{open}

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 305;
const int inf = 0x3f3f3f3f3f3f3f3f; // 极小值,用于初始化 dp
int n, m;
int a[N]; // 存储每种糖果的吸引力
int dp[N][N]; // dp[i][j]: 前 i 个数中,以第 i 个数结尾,已选 j 个数的最大吸引力
signed main()
{
    freopen("cabinet.in", "r", stdin);
    freopen("cabinet.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    //初始化 dp[i][2]
    // 选 2 个数时,吸引力就是这两个数之差的绝对值
    // dp[i][2] = max_{1 <= j < i} |a[i] - a[j]|
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < i; j++)
        {
            dp[i][2] = max(dp[i][2], abs(a[i] - a[j]));
        }
    }
    // j >= 3
    // 转移方程:dp[i][j] = max_{j-1 <= l < i} (dp[l][j-1] + |a[i] - a[l]|)
    // 即:以第 l 个数结尾选了 j-1 个,再接上第 i 个数,增加 |a[i] - a[l]| 的吸引力
    for (int i = 3; i <= n; i++) // 枚举结尾位置 i
    {
        for (int j = 3; j <= min(i, m); j++) // 枚举已选个数 j,最多选 min(i, m) 个
        {
            dp[i][j] = -inf; // 先赋极小值,表示暂时无解
            for (int l = j - 1; l < i; l++) // 枚举前一个数的位置 l
            {
                // 只有当 dp[l][j-1] 有解时才能转移
                if (dp[l][j - 1] != -inf)
                {
                    dp[i][j] = max(dp[i][j], dp[l][j - 1] + abs(a[i] - a[l]));
                }
            }
        }
    }
    // 答案是以任意位置 i 结尾、恰好选了 m 个数的最大值
    int ans = -inf;
    for (int i = m; i <= n; i++)
    {
        ans = max(ans, dp[i][m]);
    }
    cout << ans << '\n';
    return 0;
}

::::

对于这五天的收获:

省流:

185/600-110/600-68/600-311/400-180/400

其实装弱是一种美德这几天的表现还可以但是有进步空间万恶的廖老师给我们出这么难的题。

大事祭

day1:上午跟数轴动点对峙然后犯唐185.

day2:已经被数轴动点搞破防了下午卡了110.

day3:早上吃的太撑了68遗憾离场。

day4:人生中第一道青题,拿下五天中最高分

day5:中规中矩,感受到DP的difficult.