题意:有一堆数据,某些是样例数据(假设X个),某些是大数据(假设Y个),但这些数据文件的命名非常混乱。要你给它们一个一个地重命名,保证任意时刻没有重名文件的前提之下,使得样例数据命名为1~X,大数据命名为X+1~X+Y。

先把未使用的名字压进两个栈。

分为三轮:第一轮把占用了对方名字的样例数据以及占用了对方名字的大数据放进两个队列,然后不断反复尝试对这两个队列进行出队操作,每次将占用对方名字的改成一个未被使用的正确名字(从栈里取出),然后将占用的名字压进另一个栈。由于每个数据只会出队一次,所以是O(n)。

第二轮尝试把不正确的文件名尽可能改成正确的,直到栈为空,只需循环一次即可。

剩下的必然是这种情况:

形成了循环占用,假设剩下N个,只需要用N+1次改名即可,类似这样

WA 144的注意:最初文件名可能含前导零。

#include<cstdio>
#include<queue>
#include<stack>
#include<iostream>
#include<string>
using namespace std;
int lim;
bool used[100005];
int trans(string name,int type){
int len=name.length();
for(int i=0;i<len;++i){
if(name[i]<'0' || name[i]>'9'){
return -1;
}
}
if(name[0]=='0'){
return -1;
}
int shu=0;
for(int i=0;i<len;++i){
shu=shu*10+name[i]-'0';
}
return shu;
}
struct data{
int id;
string name;
int type;
data(const int &id,const string &name,const int &type){
this->id=id;
this->name=name;
this->type=type;
}
data(){}
}a[100005];
stack<int>st[2];
queue<data>q[2];
int n;
bool changed[100005];
queue<string>ans[2];
int path[15],e;
string trans(int x){
e=0;
string res="";
while(x){
path[++e]=x%10;
x/=10;
}
for(int i=e;i>=1;--i){
res+=(path[i]+'0');
}
return res;
}
int anss;
int main(){
//freopen("c.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;++i){
cin>>a[i].name>>a[i].type;
a[i].id=i;
if(a[i].type==1){
++lim;
}
}
for(int i=1;i<=n;++i){
if(a[i].type==1){
int t=trans(a[i].name,a[i].type);
if(t>=1 && t<=n) used[t]=1;
if(t>lim && t<=n){
q[1].push(a[i]);
}
}
else{
int t=trans(a[i].name,a[i].type);
if(t>=1 && t<=n) used[t]=1;
if(t<=lim && t>=1){
q[0].push(a[i]);
}
}
}
for(int i=1;i<=n;++i){
if(!used[i]){
if(i<=lim){
st[1].push(i);
}
else{
st[0].push(i);
}
}
}
while(1){
bool flag=0;
while(!q[1].empty()){
data U=q[1].front();
if(st[1].empty()){
break;
}
int to=st[1].top(); st[1].pop();
ans[0].push(U.name);
ans[1].push(trans(to));
++anss;
changed[U.id]=1;
flag=1;
st[0].push(trans(U.name,U.type));
q[1].pop();
}
while(!q[0].empty()){
data U=q[0].front();
if(st[0].empty()){
break;
}
int to=st[0].top(); st[0].pop();
ans[0].push(U.name);
ans[1].push(trans(to));
++anss;
changed[U.id]=1;
flag=1;
st[1].push(trans(U.name,U.type));
q[0].pop();
}
if(!flag){
break;
}
}
int tmp=0;
for(int i=1;i<=n;++i){
if(!changed[i]){
if(a[i].type==1){
int t=trans(a[i].name,a[i].type);
if(!(t<=lim && t>=1)){
if(!st[1].empty()){
ans[0].push(a[i].name);
ans[1].push(trans(st[1].top()));
++anss;
++tmp;
changed[i]=1;
st[1].pop();
}
}
}
else{
int t=trans(a[i].name,a[i].type);
if(!(t>lim && t<=n)){
if(!st[0].empty()){
ans[0].push(a[i].name);
ans[1].push(trans(st[0].top()));
++anss;
++tmp;
changed[i]=1;
st[0].pop();
}
}
}
}
}
// if(a[1].name=="7x6kel"){
// for(int i=1;i<=n;++i){
// if(trans(a[i].name,a[i].type)>=1 && trans(a[i].name,a[i].type)<=n){
// cout<<a[i].name<<endl;
// }
// }
// printf("%d\n",tmp);
// return 0;
// }
while(!q[1].empty()) q[1].pop();
while(!q[0].empty()) q[0].pop();
for(int i=1;i<=n;++i){
if(!changed[i]){
if(a[i].type==1){
int t=trans(a[i].name,a[i].type);
if(!(t>=1 && t<=lim)){
q[1].push(a[i]);
}
}
else{
int t=trans(a[i].name,a[i].type);
if(!(t>lim && t<=n)){
q[0].push(a[i]);
}
}
}
}
string last;
int cnt=0;
// if(q[0].size()!=q[1].size()){
// return 0;
// }
while(!q[0].empty()){
++cnt;
data U=q[1].front(),V=q[0].front();
ans[0].push(U.name);
if(cnt==1){
ans[1].push(trans(n+1));
}
else{
ans[1].push(last);
}
last=U.name;
ans[0].push(V.name);
ans[1].push(last);
last=V.name;
anss+=2;
q[0].pop(); q[1].pop();
}
if(cnt){
ans[0].push(trans(n+1));
ans[1].push(last);
++anss;
}
printf("%d\n",anss);
while(!ans[0].empty()){
cout<<"move "<<ans[0].front()<<' '<<ans[1].front()<<endl;
ans[0].pop(); ans[1].pop();
}
return 0;
}

【模拟】 Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) C. Tests Renumeration的更多相关文章

  1. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)

    A. k-rounding 题目意思:给两个数n和m,现在让你输出一个数ans,ans是n倍数且末尾要有m个0; 题目思路:我们知道一个数末尾0的个数和其质因数中2的数量和5的数量的最小值有关系,所以 ...

  2. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】

    C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  3. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】

    B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  4. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】

    A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  5. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...

  6. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries

    地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...

  7. ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers You are given two lists of non-zero digits. Let's call an integer pret ...

  8. Codeforces Round #440 (Div. 1, based on Technocup 2018 Elimination Round 2) C - Points, Lines and Ready-made Titles

    C - Points, Lines and Ready-made Titles 把行列看成是图上的点, 一个点(x, y)就相当于x行 向 y列建立一条边, 我们能得出如果一个联通块是一棵树方案数是2 ...

  9. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) C. Maximum splitting

    地址: 题目: C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. Codeforces Round #494 (Div. 3)

    刚好在考完当天有一场div3,就开了个小号打了,打的途中被辅导员喊去帮忙,搞了二十分钟-_-||,最后就出了四题,题解如下:题目链接:http://codeforces.com/contest/100 ...

  2. ”未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项

    引用:http://www.cnblogs.com/joey0210/archive/2012/09/29/2708420.html 上一篇文章说到了DLL引用问题,主要是说的程序中如果使用过了反射, ...

  3. docker 环境

    1.docker 升级 步骤: docker -v service docker stop yum -y update docker.io

  4. setTimeOut、setInterval与clearInterval函数

    1.setTimeOut 在指定毫秒数后调用函数或计算表达式,函数或计算表达式只执行一次 setTimeout("alert('5 seconds!')",5000) 2.setI ...

  5. Java中class的初始化顺序

     由于Java 中的一切东西都是对象,所以许多活动 变得更加简单,这个问题便是其中的一例. 除非真的需要代码,否则那个文件是不会载入的.通常,我们可认为除非那个类的一个对象构造完毕,  否则代码不会真 ...

  6. Python中的subprocess模块

    Subprocess干嘛用的? subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*.comm ...

  7. 【swupdate文档 一】嵌入式系统的软件管理

    嵌入式系统的软件管理 嵌入式系统变得越来越复杂, 它们的软件也反映了这种复杂性的增加. 为了支持新的特性和修复,很有必要让嵌入式系统上的软件 能够以绝对可靠的方式更新. 在基于linux的系统上,我们 ...

  8. linux指令和文件系统

    linux root用户的主目录是 /root , 其余用户在 /home 中: tar 常用 tar -zxvf : 安装使用 yum or wget website: mv a.g b.g 重命名 ...

  9. 2017多校第7场 HDU 6129 Just do it 找规律

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6129 题意:求a序列后m次xor前缀和 解法: 手动对1位置对每个位置的贡献打表发现 第一次 贡献为 ...

  10. Linux安装mysql.8.0.12

    1. linux安装mysql8.0.12,亲测可用. 以下是安装过程中出现的问题: 1 [root@localtest1 file]# systemctl start mysqld 2 Job fo ...