跳到正文

个人记录

优美的总结

Jacob20140429约 20 分钟
原文
文章信息与版本

文章信息

发布
2026年10月4日
收录
2026年10月4日
更新
2026年11月16日
篇幅
约 20 分钟

版本

  1. v1当前

Class reportClass\ report

zh-CN

给大家带来肥美的文章。

P14357 [CSP-J 2025] 拼数

这题我是得了满分的。我给大家蒋一下: 一个字符串,这题让你求出数字部分的最大值。 我们上Primary schoolPrimary\ school的时候学过,越大的数字在前面,这个数就越大。 那么按照数字从大到小排序,组成的数就一定最大。

完整代码:
C++
#include<bits/stdc++.h>
#define int64 long long
using namespace std;
bool f(char x,char y){
    return x>y;
}
string s;
char c[1000005];
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	cin>>s;
	int64 cnt=0;
	for(int i=0;i<s.size();i++)
        if(s[i]>='0' and s[i]<='9') c[++cnt]=s[i];
    sort(c+1,c+cnt+1,f);
    for(int i=1;i<=cnt;i++) cout<<c[i];
	return 0;
}

T827245 核弹棒等式(match)

这题就上难度了。我用最直白,最清晰,最不绕弯子的方式告诉你:

数字的核弹数量:

如图(https://cdn.luogu.com.cn/upload/image_hosting/p5hsawt2.png )所示,白每个数字对应的数量为:

数字数量数字数量
0655
1266
2573
3587
4496

我们就可以定义一个偏移量数组dx:

C++
int dx[10]={6,2,5,5,4,5,6,3,7,6};
位数的判DUANGDUANG

先用类似进制转换拆解的方式用stack存起来,再用综合计算对应核弹的数量。

C++
#define int64 long long
stack<int64>s;//此处"int64"应为"long long"。
int64 f(int64 a){
    if(a==0) return 6;
    while(a) {s.push(a%10); a/=10;}
    int tp=s.size(),sum=0;
    while(tp--){ sum+=dx[s.top()]; s.pop();}
    return sum;
}
循环匹配

用ii代表两个数的和,jj代表其中一个加数(即(i−j)(i-j)为另一个加数)。由于数据量不大(1≤n≤501≤n≤50、A,B,C都是≤1000的数字A,B,C 都是 ≤1000 的数字),我们可以用循环来逐个比对:

C++
for(int i=0;i<=1000;i++)
	    for(int j=0;j<=i;j++)
	        if(f(j)+f(i-j)+f(i)==n) cmd++;
完整代码
C++
#include<bits/stdc++.h>
#define int64 long long
using namespace std;
int dx[10]={6,2,5,5,4,5,6,3,7,6};
stack<int64>s;
int64 f(int64 a){
    if(a==0) return 6;
    while(a) {s.push(a%10); a/=10;}
    int tp=s.size(),sum=0;
    while(tp--){ sum+=dx[s.top()]; s.pop();}
    return sum;
}
int64 n,cmd=0;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	n-=4;
	for(int i=0;i<=1000;i++)
	    for(int j=0;j<=i;j++)
	        if(f(j)+f(i-j)+f(i)==n) cmd++;
	cout<<cmd;
	return 0;
}

T826903 美丽数(beauty)

这题的要求是: 求出符合要求(一个数中数字ii必须出现几次)最小的美丽书(每一位数字都不相邻的数)。 我们可以使用dfs。但是由于dfs的时间复杂度和空间复杂度实在太肥美了,就会TLE甚至MLE,还有~OLE~ 所以我们就使用模拟法,时间复杂度就没这么高。 具体来说,我们可以来判断两种情况:

  • 判断无解的情况
    • 当出现数字最多的数字>>其他数字出现的次数+1+1时,就一定会有重复的,所以无解
    • 当最多的是00时,就只能以‘0’‘0’开头,明显不合法。
  • 再来判断有解的情况(每一步先判断剩下的位置还够不够把剩余数字排的不相邻)
    • 如果位置紧张,必须优先放剩余最多的数字,防止后面装相同。
    • 如果位置宽敞,就选当前能选的最小数字,保证整体字典序最小。
C++
#include<bits/stdc++.h>
using namespace std;
int a[15];
void f(){
    int n=0,maxn=0,last=0;
    for(int i=0;i<=9;i++){
        cin>>a[i];
        n+=a[i];
        if(a[i]>a[maxn]) maxn=i;}
    int limit=n-a[maxn]+1;
    if(a[maxn]>limit or (maxn==0 and a[maxn]==limit)){
        cout<<"-1\n";//无解的情况
        return;}
    for(int i=1;i<=n;i++){
        int minn=-1,cur=0;
        for(int j=0;j<=9;j++){//有解的情况
            if(minn==-1 and j!=last and a[j]>0) minn=j;
            if(a[j]>a[cur]) cur=j;}
        if(a[cur]*2-1>=n-i+1){
            cout<<cur;
            a[cur]--;
            last=cur;}
        else{
            cout<<minn;
            a[minn]--;
            last=minn;} 
    }
    cout<<"\n";
}
int main(){
    int t;
    cin>>t;
    while(t--) f();
    return 0;}

备注:

nn是总位数 maxnmaxn是初始最多出现次数的数字 minnminn是当前最小可选数 curcur是当前剩余最多的数

T827260 泥泞路(road)

我们使用贪心策略,从左往右处理,每次找到没有被覆盖的泥泞最左端,从此处向右铺木板,让木板尽可能延申,最大化单次覆盖距离,从而木板总数最小。

  • 将所有泥泞区间按起点从小到大排序,从左到右依次处理。
  • 只要当前未覆盖还没有到达泥泞终点,就在当前位置铺木板dq+=l,木板计数+1+1。
C++
#include<bits/stdc++.h>
using namespace std;
struct node{
    int s,e;
};
int f(node x,node y){
    return x.s<y.s;
}
node a[10005];
int main(){
    int n,l,dq=0,cnt=0;
    cin>>n>>l;
    for(int i=1;i<=n;i++) cin>>a[i].s>>a[i].e;
    sort(a+1,a+n+1,f);
    for(int i=1;i<=n;i++){
        dq=max(dq,a[i].s);
        while(dq<a[i].e) {dq+=l; cnt++;}
    }
    cout<<cnt;
    return 0;
}

备注:

dq是木板已经覆盖到最右端的位置 cnt是模板总数 dq=max(dq,a[i].s);的动作是找到泥泞未被木板覆盖的最左位置。

en-US

Fat Problem Solutions for Everyone

P14357 [CSP-J 2025] Combine Numbers

I got full marks on this problem. Let me explain it to you: Given a string, the problem asks you to find the maximum value formed by its numeric digits.

We learned in primary school that larger digits placed earlier make a bigger number. So if we sort digits from largest to smallest and concatenate them, we get the maximum possible number.

Full Code:
C++
#include<bits/stdc++.h>
#define int64 long long
using namespace std;
bool f(char x,char y){
    return x>y;
}
string s;
char c[1000005];
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	cin>>s;
	int64 cnt=0;
	for(int i=0;i<s.size();i++)
        if(s[i]>='0' and s[i]<='9') c[++cnt]=s[i];
    sort(c+1,c+cnt+1,f);
    for(int i=1;i<=cnt;i++) cout<<c[i];
	return 0;
}

T827245 Matchstick Equation (match)

This problem ramps up the difficulty. I will explain it to you in the most straightforward, clear, and simple way:

Matchstick count for each digit:

As shown in the image (https://cdn.luogu.com.cn/upload/image_hosting/p5hsawt2.png), the number of matchsticks for each digit is:

表格

DigitMatchesDigitMatches
0655
1266
2573
3587
4496

We can define an offset array dx:

C++
int dx[10]={6,2,5,5,4,5,6,3,7,6};
Digit decomposition

We decompose numbers similar to base conversion and store digits in a stack, then calculate the total matchsticks required.

C++
#define int64 long long
stack<int64>s;//Note: "int64" here is alias for "long long".
int64 f(int64 a){
    if(a==0) return 6;
    while(a) {s.push(a%10); a/=10;}
    int tp=s.size(),sum=0;
    while(tp--){ sum+=dx[s.top()]; s.pop();}
    return sum;
}
Loop to check valid equations

Let i represent the sum of two numbers, and j represent one addend (so (i-j) is the other addend). Since the data range is small ((1≤n≤50), (A,B,C ≤1000)), we can loop and check each case one by one:

C++
for(int i=0;i<=1000;i++)
	    for(int j=0;j<=i;j++)
	        if(f(j)+f(i-j)+f(i)==n) cmd++;
Full Code
C++
#include<bits/stdc++.h>
#define int64 long long
using namespace std;
int dx[10]={6,2,5,5,4,5,6,3,7,6};
stack<int64>s;
int64 f(int64 a){
    if(a==0) return 6;
    while(a) {s.push(a%10); a/=10;}
    int tp=s.size(),sum=0;
    while(tp--){ sum+=dx[s.top()]; s.pop();}
    return sum;
}
int64 n,cmd=0;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	n-=4;
	for(int i=0;i<=1000;i++)
	    for(int j=0;j<=i;j++)
	        if(f(j)+f(i-j)+f(i)==n) cmd++;
	cout<<cmd;
	return 0;
}

T826903 Beautiful Number (beauty)

Problem statement: Find the smallest beautiful number satisfying the digit count requirements (each digit i must appear a specified number of times), where identical digits cannot be adjacent.

We can use DFS. But DFS has very high time and space complexity, which may lead to TLE, even MLE, or OLE. So we use a constructive greedy method with lower time complexity.

Specifically, we consider two cases:

  • Check impossible cases
    • If the digit with the maximum required count > sum of counts of other digits (+1), identical digits will inevitably be adjacent, so there is no solution.
    • If the most frequent digit is 0, the number would have to start with ‘0’, which is invalid.
  • If a solution exists (at each step, check whether remaining positions can arrange leftover digits without adjacency)
    • If positions are tight: we must place the digit with the largest remaining count first, to avoid identical digits ending up next to each other later.
    • If positions are ample: pick the smallest available digit to ensure the overall lexicographically smallest result.
C++
#include<bits/stdc++.h>
using namespace std;
int a[15];
void f(){
    int n=0,maxn=0,last=0;
    for(int i=0;i<=9;i++){
        cin>>a[i];
        n+=a[i];
        if(a[i]>a[maxn]) maxn=i;}
    int limit=n-a[maxn]+1;
    if(a[maxn]>limit or (maxn==0 and a[maxn]==limit)){
        cout<<"-1\n";//No solution
        return;}
    for(int i=1;i<=n;i++){
        int minn=-1,cur=0;
        for(int j=0;j<=9;j++){
            if(minn==-1 and j!=last and a[j]>0) minn=j;
            if(a[j]>a[cur]) cur=j;}
        if(a[cur]*2-1>=n-i+1){
            cout<<cur;
            a[cur]--;
            last=cur;}
        else{
            cout<<minn;
            a[minn]--;
            last=minn;} 
    }
    cout<<"\n";
}
int main(){
    int t;
    cin>>t;
    while(t--) f();
    return 0;}

Notes:

n: total number of digits maxn: digit with the largest required count initially minn: smallest digit available at current step cur: digit with the largest remaining count

T827260 Muddy Road (road)

We adopt a greedy strategy: process the road from left to right. Each time find the leftmost uncovered muddy segment, then lay a board starting at this position and extend it as far as possible, maximizing the covered length each time to minimize total boards used.

  • Sort all muddy intervals by starting point in ascending order and process left to right.
  • While our covered range has not reached the end of the muddy patch, extend coverage by the board length dq+=l and increment the board count.
C++
#include<bits/stdc++.h>
using namespace std;
struct node{
    int s,e;
};
int f(node x,node y){
    return x.s<y.s;
}
node a[10005];
int main(){
    int n,l,dq=0,cnt=0;
    cin>>n>>l;
    for(int i=1;i<=n;i++) cin>>a[i].s>>a[i].e;
    sort(a+1,a+n+1,f);
    for(int i=1;i<=n;i++){
        dq=max(dq,a[i].s);
        while(dq<a[i].e) {dq+=l; cnt++;}
    }
    cout<<cnt;
    return 0;
}

Notes:

dq: the rightmost position covered by planks cnt: total number of planks dq=max(dq,a[i].s);: find the leftmost muddy position not yet covered by planks

原文
抓取于
  1. Jacob20140429

    our hotline:+83 (0314) 6610 8610