洛谷习题V^V

news2025/7/21 7:19:55

1.帮贡排序

解题思路:按照题意,排序模拟即可

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

struct Member {
    string name;
    string position;
    int contribution;
    int level;
    int original_order;
};

 
int getPriority(const string& pos) {
    if (pos == "BangZhu") return 0;
    if (pos == "FuBangZhu") return 1;
    if (pos == "HuFa") return 2;
    if (pos == "ZhangLao") return 3;
    if (pos == "TangZhu") return 4;
    if (pos == "JingYing") return 5;
    return 6; // BangZhong
}

 
bool compareDisplay(const Member& a, const Member& b) {
    int pa = getPriority(a.position);
    int pb = getPriority(b.position);
    if (pa != pb) return pa < pb;
    if (a.level != b.level) return a.level > b.level;
    return a.original_order < b.original_order;
}

 
bool compareContribution(const Member& a, const Member& b) {
    if (a.contribution != b.contribution) return a.contribution > b.contribution;
    return a.original_order < b.original_order;
}

int main() {
    int n;
    cin >> n;
    vector<Member> members(n);
    vector<Member> leaders;  
    vector<Member> others;  

    for (int i = 0; i < n; ++i) {
        cin >> members[i].name >> members[i].position >> members[i].contribution >> members[i].level;
        members[i].original_order = i;
        if (members[i].position == "BangZhu" || members[i].position == "FuBangZhu") {
            leaders.push_back(members[i]);
        } else {
            others.push_back(members[i]);
        }
    }

     
    sort(others.begin(), others.end(), compareContribution);

    
    int huFaCount = 0, zhangLaoCount = 0, tangZhuCount = 0, jingYingCount = 0;
    for (auto& member : others) {
        if (huFaCount < 2) {
            member.position = "HuFa";
            huFaCount++;
        } else if (zhangLaoCount < 4) {
            member.position = "ZhangLao";
            zhangLaoCount++;
        } else if (tangZhuCount < 7) {
            member.position = "TangZhu";
            tangZhuCount++;
        } else if (jingYingCount < 25) {
            member.position = "JingYing";
            jingYingCount++;
        } else {
            member.position = "BangZhong";
        }
    }

     
    vector<Member> allMembers = leaders;
    allMembers.insert(allMembers.end(), others.begin(), others.end());

    
    sort(allMembers.begin(), allMembers.end(), compareDisplay);

     
    for (const auto& member : allMembers) {
        cout << member.name << " " << member.position << " " << member.level << endl;
    }

    return 0;
}

2.阶乘数码

解题思路:高精度,单精度问题,套用模版,最后统计一遍即可

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;
void dis_time() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}


void mult(vector<int>& ans, int x) {
    reverse(ans.begin(), ans.end());

    int up = 0;
    for (int i = 0; i < ans.size(); i++) {
        ans[i] = ans[i] * x + up;
        up = ans[i] / 10;
        ans[i] %= 10;
    }
    while (up) {
        int cur = up % 10;
        ans.push_back(cur);
        up /= 10;
    }
    reverse(ans.begin(), ans.end());
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int>ans;
    ans.push_back(1);
    for (int i = 2; i <= n; i++) {
        mult(ans, i);
    }

    int res = 0;
    for (auto i : ans) {
        if (i == m)
            res++;
    }
    cout << res << endl;
}

int main() {
    dis_time();
    int t; cin >> t;

    while(t--)
        solve();
    
    
    return 0;
}


3.最大乘积

解题思路:对于一个数学结论,如果可以相同经量分成3的多少次方,但是这里不一样,由

可知,尽可能把数变的小,这样就多,乘积也就最大,即用贪心的思路去解题即可

#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 10003;   
const int MAXL = 100003;  
const int INF = 2147483647;

 
struct BigNumber {
    int digits[MAXL]; 
    int length;     
    
    BigNumber() : length(0) {
        memset(digits, 0, sizeof(digits));
    }
    
    BigNumber multiply(int num) {
        BigNumber result;
        for (int i = 1; i <= length; i++) {
            result.digits[i] = digits[i] * num;
        }
        
      
        for (int i = 1; i <= length + 10; i++) {
            result.digits[i + 1] += result.digits[i] / 10;
            result.digits[i] %= 10;
            if (result.digits[i]) {
                result.length = i;
            }
        }
        return result;
    }
};

int main() {
    int n;
    cin >> n;
    
    
    double logValues[MAXN];
    for (int i = 1; i <= n; i++) {
        logValues[i] = log(i);
    }
    
    
    double dp[MAXN];
    int path[MAXN]; 
    
    
    for (int i = 1; i <= n; i++) {
        dp[i] = -INF;
    }
    dp[0] = 0;
    
   
    for (int i = 1; i <= n; i++) {
        for (int j = n; j >= i; j--) {
            if (dp[j - i] + logValues[i] > dp[j]) {
                dp[j] = dp[j - i] + logValues[i];
                path[j] = j - i;
            }
        }
    }
    
    
    vector<int> decomposition;
    for (int p = n; p != 0; p = path[p]) {
        decomposition.push_back(p - path[p]);
    }
    
    
    sort(decomposition.begin(), decomposition.end());
    for (int num : decomposition) {
        cout << num << " ";
    }
    cout << endl;
    
    
    BigNumber product;
    product.length = 1;
    product.digits[1] = 1;
    
    for (int num : decomposition) {
        product = product.multiply(num);
    }
    
    
    for (int i = product.length; i >= 1; i--) {
        cout << product.digits[i];
    }
    cout << endl;
    
    return 0;
}

4.[NOIP 1998 提高组] 拼数

解题思路:按照ASCII码来排序,排序一下,输出即可

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

string s[21];int n;
bool cmp(const string &a,const string &b) { 
    return (a+b > b+a);
}
int main(void) {
    cin >> n;
    for(int i=1;i<=n;++i) cin >> s[i];
    sort(s+1,s+n+1,cmp);
    for (int i=1;i<=n;++i) cout << s[i];
    return 0;
}

5.统计方形(数据加强版)

解题思路:找规律,对于每个格子,假设都为方形的右下角,对于正方形,有这个格子在内的正方形个数为min(x,y),对于这个格子的所有的长方形(包括正方形)为x*y,既如此,遍历每个格子为右下角的情况,相加求解

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int main(){
	ll n,m,i,j,sum=0,sum1=0;
	cin>>n>>m;
	for(i=1;i<=n;i++){
	  for(j=1;j<=m;j++){
	    sum+=min(i,j);
	    sum1+=i*j;
	  }
	}
	
	cout<<sum<<" "<<sum1-sum<<endl;
	return 0;
}

6.P1618 三连击(升级版)

解题思路:这个就dfs,找全排列在A:B:C的条件下有多少个

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;
void dis_time() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}
bool vis[10];
int num[11];
int a, b, c;
int key = 0;
void dfs(int x) {
    if (x > 9) {
        int num1 = num[1] * 100 + num[2] * 10 + num[3];
        int num2 = num[4] * 100 + num[5] * 10 + num[6];
        int num3 = num[7] * 100 + num[8] * 10 + num[9];
        if (num1 * b == num2 * a && num1 * c == num3 * a) {
            key = 1;
            cout << num1 << " " << num2 << " " << num3 << endl;
        }
        
        return;
    }

    for (int i = 1; i <= 9; i++) {
        if (!vis[i]) {
            vis[i] = true;
            num[x] = i;
            dfs(x + 1);
            vis[i] = false;
        }
    }
}

void solve() {
    
    cin >> a >> b >> c;
    dfs(1); 
    if (!key)
        cout << "No!!!" << endl;
}

int main() {
    dis_time();
    solve();


    return 0;
}


6.P3654 First Step (ファーストステップ)

解题思路:对于这个题,可以直接对没列,每行统计就行,但是我最开始写的是dfs,计算出所有的可能然后/2,但是要特判k为1时

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;
void dis_time() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
}
int ans = 0;
char graph[110][110];
int n, m, k;

void dfs(int x, int y) {
	if (x >= k) {
		int key = 0;
		for (int i = x; i > x - k; i--) {
			if (graph[i][y] != '.') {
				key = 1;
				break;
			}
		}
		if (!key)
			ans++;
	}
	if (n - x >= k - 1) {
		int key = 0;
		for (int i = x; i <= x + k - 1; i++) {
			if (graph[i][y] != '.') {
				key = 1;
				break;
			}
		}
		if (!key)
			ans++;
	}
	if (y >= k) {
		int key = 0;
		for (int i = y; i > y - k; i--) {
			if (graph[x][i] != '.') {
				key = 1;
				break;
			}
		}
		if (!key)
			ans++;
	}
	if (m - y >= k - 1) {
		int key = 0;
		for (int i = y; i <= y + k - 1; i++) {
			if (graph[x][i] != '.') {
				key = 1;
				break;
			}
		}
		if (!key)
			ans++;
	}

}

void solve() {
	cin >> n >> m >> k;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> graph[i][j];
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (graph[i][j] == '.' && k != 1)
				dfs(i, j);
		}
	}
	if (k != 1)
		cout << ans / 2;
	else
		cout << ans/4;
	
}

int main() {
	dis_time();
	solve();
	return 0;
}

6.P1149 [NOIP 2008 提高组] 火柴棒等式

解题思路:就是枚举出每个数字他们所需的火柴个数,然后暴力即可

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;
void dis_time() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
}

int num[2500] = { 6,2,5,5,4,5,6,3,7,6 };
void solve() {
	int n; cin >> n;
	n -= 4;
	for (int i = 10; i <= 2000; i++) {
		int temp = i;
		while (temp) {
			num[i] += num[temp % 10];
			temp /= 10;
		}
	}
	int ans = 0;
	int t = 0;
	for (int i = 0; i <= 1000; i++) {
		for (int j = 0; j <= 1000; j++) {
			int temp = i + j;
			if (num[i] + num[j] + num[temp] == n && i != j) {
				ans ++;
			}
			else if (num[i] + num[j] + num[temp] == n && i == j) {
				ans++; t++;
			}
		}
	}
	cout << ans - t / 2;
	
}

int main() {
	dis_time();
	solve();
	return 0;
}

7.P3799 小 Y 拼木棒

解题思路:对于这个题,有a = b = c + d,只要去找a,c就够了,也就是双层for循环,但是常规的双层for循环会时间超限,所有可以用离散化map来存储,元素和元素个数,然后求解

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;

const int MOD = 1e9 + 7;
void dis_time(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

void solve() {
    int n; 
    cin >> n;
    unordered_map<int, int> freq;
    int max_len = 0;
    for (int i = 0; i < n; ++i) {
        int temp; 
        cin >> temp;
        freq[temp]++;
        if (temp > max_len) {
            max_len = temp;
        }
    }

    ll ans = 0;
    for (auto& a : freq) {
        if (a.second < 2) continue;
        ll cnt_a = (ll)a.second * (a.second - 1) / 2 % MOD;
        for (auto& c : freq) {
            int d = a.first - c.first;
            if (d <= 0 || c.first >= d) continue;
            if (freq.count(d)) {
                ll cnt_cd = (ll)c.second * freq[d] % MOD;
                ans = (ans + cnt_a * cnt_cd % MOD) % MOD;
            }
        }
        if (a.first % 2 == 0) {
            int c = a.first / 2;
            if (freq.count(c) && freq[c] >= 2) {
                ll cnt_c = (ll)freq[c] * (freq[c] - 1) / 2 % MOD;
                ans = (ans + cnt_a * cnt_c % MOD) % MOD;
            }
        }
    }
    cout << ans << endl;
}

int main() {
    dis_time();
    solve();
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2392693.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Wireshark 在 macOS 上使用及问题解决

wireshark概述 Wireshark 是被广泛使用的免费开源网络协议分析软件&#xff08;network protocol analyzer&#xff09;或网络数据包分析工具&#xff0c;它可以让你在微观层面上查看网络上发生的事情。它的主要功能是截取网络数据包&#xff0c;并尽可能详细地展示网络数据包…

不同电脑同一个网络ip地址一样吗?如何更改

想象一下&#xff0c;你住在同一栋公寓楼里&#xff0c;所有住户对外共享一个统一的小区地址&#xff08;类似公网IP&#xff09;&#xff0c;但每家每户又有独立的门牌号&#xff08;类似内网IP&#xff09;。网络世界中的IP地址也遵循这一逻辑&#xff1a;同一局域网内的设备…

微软 Azure AI Foundry(国际版)十大重要更新

2025 年被广泛视为 “AI 智能体元年”。在过去半年&#xff0c;微软密集发布众多创新技术&#xff0c;构建起从基础设施层、开发工具层到场景应用层的完整技术矩阵&#xff0c;加速推动诸多具备自主决策能力的 “超级助理” 智能体落地&#xff0c;形成完整的 AI 赋能生态&…

PostgreSQL如何更新和删除表数据

这节说下怎样更新和删除表数据&#xff0c;当然认识命令了&#xff0c;可以问AI帮忙写。 接上节先看下天气表weather的数据&#xff0c;增加了杭州和西安的数据&#xff1a; 一.UPDATE更新命令 用UPDATE命令更新现有的行。 假设所有 杭州 5月12日的温度低了两度&#xff0c;用…

Golang | 运用分布式搜索引擎实现视频搜索业务

把前面所设计好的搜索引擎引用进来开发一个简单的具体的视频搜索业务。代码结构&#xff1a; handler目录&#xff1a;后端接口&#xff0c;负责接收请求并返回结果&#xff0c;不存在具体的搜索逻辑。video_search目录&#xff1a;具体的搜索逻辑存放在这&#xff0c;包括reca…

【笔记】Trae+Andrioid Studio+Kotlin开发安卓WebView应用

文章目录 简介依赖步骤AS(Andriod Studio)创建项目AS创建虚拟机TRAE CN 修改项目新增按键捕获功能 新增WebViewWebView加载本地资源在按键回调中向WebView注入JS代码 最终关键代码吐槽 简介 使用Trae配合Andriod Studio开发一个内嵌WebView的安卓应用, 在WebView中加载本地资源…

Github上一些使用技巧(缩写、Issue的Highlight)自用

1. GIthub中的一些缩写 LGTM ! 最近经常看到一些迷之缩写&#xff0c;感觉挺有意思的&#xff0c;但是有时候看到一些没见过的缩写还是有点懵逼&#xff0c;不过缩写确实也是很方便去review&#xff0c;这里就记录汇总一下&#xff1b;顺便加了一些git的基操单词&#xff08;加…

TextIn OCR Frontend前端开源组件库发布!

为什么开源 TextIn OCR Frontend 前端组件库&#xff1f; 在 TextIn 社群中&#xff0c;我们时常接到用户反馈&#xff0c;调取 API 进行票据等文件批量识别后&#xff0c;需要另行完成前端工程&#xff0c;实现比对环节。为助力用户节省工程成本&#xff0c;TextIn 团队正式开…

C#中数据绑定的简单例子

数据绑定允许将控件的属性和数据链接起来——控件属性值发生改变&#xff0c;会导致数据跟着自动改变。 数据绑定还可以是双向的——控件属性值发生改变&#xff0c;会导致数据跟着自动改变&#xff1b;数据发生改变&#xff0c;也会导致控件属性值跟着自动改变。 1、数据绑定…

VR 技术在农业领域或许是一抹新曙光​

在科技日新月异的今天&#xff0c;VR(虚拟现实)技术已不再局限于游戏、影视等娱乐范畴&#xff0c;正逐步渗透到各个传统行业&#xff0c;为其带来全新的发展契机&#xff0c;农业领域便是其中之一。VR 技术利用计算机生成三维虚拟世界&#xff0c;给予用户视觉、听觉、触觉等多…

【JVM】Java程序运行时数据区

运行时数据区 运行时数据区是Java程序执行过程中管理的内存区域 Java 运行时数据区组成&#xff08;JVM 内存结构&#xff09; Java 虚拟机&#xff08;JVM&#xff09;的运行时数据区由以下核心部分组成&#xff1a; 线程私有&#xff1a;程序计数器、Java虚拟机栈、本地方…

计算机视觉入门:OpenCV与YOLO目标检测

计算机视觉入门&#xff1a;OpenCV与YOLO目标检测 系统化学习人工智能网站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目录 计算机视觉入门&#xff1a;OpenCV与YOLO目标检测摘要引言技术原理对比1. OpenCV&#xff1a;传统图像处理与机器学…

【Prometheus+Grafana实战:搭建监控系统(含告警配置)】

什么是Prometheus和Grafana&#xff1f; Prometheus&#xff1a;一款开源的监控告警工具&#xff0c;擅长时序数据存储和多维度查询&#xff08;通过PromQL&#xff09;&#xff0c;采用Pull模型主动抓取目标指标。Grafana&#xff1a;数据可视化平台&#xff0c;支持多种数据…

一文速通Python并行计算:11 Python多进程编程-进程之间的数据安全传输-基于队列和管道

一文速通 Python 并行计算&#xff1a;11 Python 多进程编程-进程之间的数据安全传输-基于队列和管道 摘要&#xff1a; Python 多进程中&#xff0c;Queue 和 Pipe 提供进程间安全通信。Queue 依赖锁和缓冲区&#xff0c;保障数据原子性和有序性&#xff1b;Pipe 实现点对点单…

LangChain-Tool和Agent结合智谱AI大模型应用实例2

1.Tool(工具) 定义与功能 单一功能模块:Tool是完成特定任务的独立工具,每个工具专注于一项具体的操作,例如:搜索、计算、API调用等 无决策能力:工具本身不决定何时被调用,仅在被触发时执行预设操作 输入输出明确:每个工具需明确定义输入、输出参数及格式 2.Agent(…

centos7.6阿里云镜像各个版本介绍

&#xff08;水一期&#xff09; Index of /centos-vault/centos/7.6.1810/isos/x86_64/ File NameFile SizeDateParent directory/--0_README.txt2.4 KB2018-12-01 21:21CentOS-7-x86_64-DVD-1810.iso4.3 GB2018-11-26 07:55CentOS-7-x86_64-DVD-1810.torrent86.0 KB2018-12-…

InnoDB引擎逻辑存储结构及架构

简化理解版 想象 InnoDB 是一个高效运转的仓库&#xff1a; 核心内存区 (大脑 & 高速缓存 - 干活超快的地方) 缓冲池 Buffer Pool (最最核心&#xff01;)&#xff1a; 作用&#xff1a; 相当于仓库的“高频货架”。把最常用的数据&#xff08;表数据、索引&#xff09;从…

第4讲、Odoo 18 模块系统源码全解与架构深度剖析【modules】

引言 Odoo 是一款强大的开源企业资源规划&#xff08;ERP&#xff09;与客户关系管理&#xff08;CRM&#xff09;系统&#xff0c;其核心竞争力之一在于高度模块化的架构设计。模块系统不仅是 Odoo 框架的基石&#xff0c;更是实现功能灵活扩展与定制的关键。本文将结合 Odoo…

pytorch简单线性回归模型

模型五步走 1、获取数据 1. 数据预处理 2.归一化 3.转换为张量 2、定义模型 3、定义损失函数和优化器 4、模型训练 5、模型评估和调优 调优方法 6、可视化&#xff08;可选&#xff09; 示例代码 import torch import torch.nn as nn import numpy as np import matplot…

四、web安全-行业术语

1. 肉鸡 所谓“肉鸡”是一种很形象的比喻&#xff0c;比喻那些可以随意被我们控制的电脑&#xff0c;对方可以是WINDOWS系统&#xff0c;也可以是UNIX/LINUX系统&#xff0c;可以是普通的个人电脑&#xff0c;也可以是大型的服务器&#xff0c;我们可以象操作自己的电脑那样来…