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; 
}
相关推荐
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛3 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
m0_547486663 天前
《数据结构教程》全套 PPT课件2026
数据结构
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·3 天前
C++ 萌新语法入门篇
c++·算法比赛