noi-2026年5月12号小测验

**题目:**P1097 NOIP 2007 提高组 统计数字

网址: https://www.luogu.com.cn/problem/P1097

**思路:**使用map来记录每个数出现了几次

**知识点:**map的使用

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define cer(a) cerr<<#a<<'='<<(a)<<"@ line"<<__LINE__<<endl
#define cer2(a,b) cerr<<#a<<'='<<(a)<<','<<#b<<'='<<(b)<<"@ line"<<__LINE__<<endl
#define se second
#define db double 
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn=2e5+100;
const ll mode2=1e9+7; 

string str; 
ll n,m;
vector<int>vt[maxn];
int vis[maxn];
ll ans,sum;

void solve()
{
   cin>>n;
   map<ll,ll>p;
   for(int i=1;i<=n;i++)
   {
   	  ll x;
   	  cin>>x;
   	  p[x]++;
   }
   for(auto it:p)
   {
   	 cout<<it.fi<<" "<<it.se<<'\n';
   }
   
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int _=1;
//	cin>>_;
	while(_--)
	solve();  
	return 0; 
}

**题目:**P1889 CEOI 1998 士兵站队

网址: https://www.luogu.com.cn/problem/P1889

**思路:**对于Y来说,取中位数就行。对于X来说, |x1-k|+|x2-(k+1)|+|x3-(k+2)|+...+|xn-(k+n-1)|=|(x1-0)-k|+|(x2-1)-k|+|(x3-2)-k|+...+|(xn-n-1)-k|,那么k也是取中位数就行。

**知识点:**思维

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define cer(a) cerr<<#a<<'='<<(a)<<"@ line"<<__LINE__<<endl
#define cer2(a,b) cerr<<#a<<'='<<(a)<<','<<#b<<'='<<(b)<<"@ line"<<__LINE__<<endl
#define se second
#define db double 
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn=2e5+100;
const ll mode2=1e9+7; 

string str; 
ll n,m;
vector<int>vt[maxn];
int vis[maxn];
ll ans,sum;
ll X[maxn],Y[maxn];
void solve()
{
   cin>>n;
   for(int i=1;i<=n;i++)
   {
     cin>>X[i]>>Y[i];	
   } 
   sort(X+1,X+1+n);
   sort(Y+1,Y+1+n);
   for(int i=1;i<=n;i++)
   X[i]-=i-1;
   sort(X+1,X+1+n);
   ll sx,sy;
   if(n%2==0)
   {
   	  sx=(X[n/2]+X[n/2+1])/2;
   	  sy=(Y[n/2]+Y[n/2+1])/2;
   }else{
   		sx=(X[n/2+1]);
   		sy=(Y[n/2+1]);
   }
   ans=0;
   for(int i=1;i<=n;i++)
   {
   	 ans+=abs(X[i]-sx)+abs(Y[i]-sy);
   }
   cout<<ans;
   
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int _=1;
//	cin>>_;
	while(_--)
	solve();  
	return 0; 
}

**题目:**P1862 输油管道问题

网址: https://www.luogu.com.cn/problem/P1862

**思路:**因为输油管道是向南或向北,所有x坐标没用,只考虑y坐标,y坐标取中位数就行。

**知识点:**思维

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define cer(a) cerr<<#a<<'='<<(a)<<"@ line"<<__LINE__<<endl
#define cer2(a,b) cerr<<#a<<'='<<(a)<<','<<#b<<'='<<(b)<<"@ line"<<__LINE__<<endl
#define se second
#define db double 
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn=2e5+100;
const ll mode2=1e9+7; 

string str; 
ll n,m;
vector<int>vt[maxn];
int vis[maxn];
ll ans,sum;
ll a[maxn]; 
void solve()
{
   cin>>n;
   for(int i=1;i<=n;i++)
   {
     ll x;
	 cin>>x; 
     cin>>a[i];	
   } 
   sort(a+1,a+1+n);
   ll y=a[n/2+1];
   if(n%2==0)
   {
   	 y=(a[n/2]+a[n/2+1])/2;
   }
   ans=0;
   for(int i=1;i<=n;i++)
   ans+=abs(a[i]-y);
   cout<<ans;
   
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int _=1;
//	cin>>_;
	while(_--)
	solve();  
	return 0; 
}
相关推荐
鸿芯微控科技11 小时前
MFC关断后还有流量怎么办?零流量、阀门泄漏、压差与Python分析
c++·python·mfc·质量流量控制器·关断泄漏·零流量测试
lazily-c11 小时前
数据结构模板
数据结构
祖力5519 小时前
数据结构的基本概念与单向链表(链表数据类型构造、创建、头插、尾插、头删、尾删)
数据结构·链表
大明者省20 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
白狐_7981 天前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander2581 天前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
ShineWinsu1 天前
对于C++:C++11中lambda、function、bind的解析
c++·面试·笔试·开发·lambda·bind·function
码匠许师傅1 天前
【C++ 面试真题】聊聊 C++ 的拷贝构造与拷贝赋值
java·c++·面试
Shell运维手记1 天前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
杨航 AI1 天前
O(n log n):线性对数原理拆解 这个是排序算法的黄金复杂度之一。
算法·排序算法