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; 
}
相关推荐
沫璃染墨8 分钟前
从零入门计算机网络系列(十二):HTTP长连接与会话管理详解——从Connection到Cookie与Session
linux·服务器·c++·tcp/ip·ubuntu·http·交互
nm_zfyjt12 分钟前
信息流广告投放优化实践:线索成本评估模型与转化链路重构(家装建材案例)
算法·重构·内蒙古朋友圈广告服务商·内蒙古朋友圈获客
xiaoye-duck1 小时前
《C++ 项目:手写大模型接入 SDK》DeepSeek接口设计(下):流式响应实现和测试
开发语言·网络·c++·语言模型
w_zero_one1 小时前
算法--构造
java·数据结构·算法
其实防守也摸鱼2 小时前
信创安全竞赛知识总结与题库
大数据·网络·人工智能·算法·安全·php
旖旎夜光2 小时前
【C++ 基础入门】类和对象(上):封装、对象模型与 this 指针
开发语言·c++·笔记·学习
小超人诞生日记3 小时前
C++ 智能指针的使用及其原理详解
c++·算法
akinggw4 小时前
libagentcore - 一个基于c++的AI多agent系统升级到2.0版本
开发语言·jvm·c++
繁星蓝雨5 小时前
C++设计原理———多重继承(虚基类、菱形继承、委托、重命名、基类和成员初始化)
c++·委托·多重继承·mi·显式初始化列·虚基类·对象布局