CF1728C Digital Logarithm #E135(div.2)
题目链接
https://codeforces.com/problemset/problem/1728/C
这是一道
题意简述
定义
给定2个长度为
给定操作,从数组
问最小需要修改多少次,才能使得a和b数组在升序排序后,是完全相等的。
数据范围
点击查看算法标签
贪心
样例
点击查看样例
对于第一组样例
1
1000
操作一次1000后变成3,再操作一次3后变成1
此时两数组相等.
分析
这道题当时花了不少时间AC掉了,我自己的思路是发现由于范围是
代码
点击查看代码
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int N=1e6+10;
int a[N];
int b[N];
int c[N];
int d[N];
int vis[10];
int f(int x)
{
int res=0;
while(x>0)
{
x/=10;
res++;
}
return res;
}
int main()
{
//freopen("uva.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof vis);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
sort(a+1,a+n+1);
sort(b+1,b+n+1);
int i=1,j=1;
int lc=0,ld=0;
while(i<=n&&j<=n)
{
if(a[i]<b[j])
{
c[++lc]=a[i];
i++;
}
else if(a[i]>b[j])
{
d[++ld]=b[j];
j++;
}
else
{
i++;
j++;
}
}
while(i<=n)
{
c[++lc]=a[i];
i++;
}
while(j<=n)
{
d[++ld]=b[j];
j++;
}
int cnt=0;
for(int i=1;i<=lc;i++)
{
if(c[i]==1)continue;
if(c[i]<10)vis[c[i]]++;
else
{
vis[f(c[i])]++;
cnt++;
}
}
j=0;
for(int i=1;i<=ld;i++)
{
if(d[i]==1)continue;
if(d[i]<10)vis[d[i]]--;
else
{
vis[f(d[i])]--;
cnt++;
}
}
for(int i=2;i<=9;i++)
{
cnt+=abs(vis[i]);
};
printf("%d\n",cnt);
}
return 0;
}
而标准答案很简单,直接贪心+优先数组
注意 ,优先队列 priority_queue
默认堆顶是取的是更大的数
首先,把两个序列中的数分别存入优先队列中,从两个队列中分别取出最大的数,如果两个最大的数
代码如下
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;
//#define debug
int n, x;
void solve() {
scanf("%d", &n);
priority_queue<int> qa, qb;
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
qa.push(x);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
qb.push(x);
}
int res = 0;
while (!qa.empty() && !qb.empty()) {
int a = qa.top(), b = qb.top();
// printf("{%d %d}\n", a, b);
if (a == b) {
qa.pop();
qb.pop();
continue;
}
++res;
if (a > b) {
qa.pop();
qa.push(to_string(a).length());
} else {
qb.pop();
qb.push(to_string(b).length());
}
}
printf("%d\n", res);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
分类:
CodeForces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统