题意:有一堆数据,某些是样例数据(假设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. Zen Cart、Joy-Cart、Magento、ShopEX、ECshop电子商务系统比较

    1.Zen Cart 优点:历史较久,系统经过长时间充分的测试,比较成熟:免费开源便于功能二次开发:基础功能强大:安装插件简单,修改文件很少,甚至不用修改文件:应用非常广泛,插件.模块更新快,其中多为 ...

  2. 【HNOI】 小A的树 tree-dp

    [题目描述]给定一颗树,每个点有各自的权值,任意选取两个点,要求算出这两个点路径上所有点的and,or,xor的期望值. [数据范围]n<=10^5 首先期望可以转化为求树上所有点对的and,o ...

  3. javascript 变量类型判断

    一.typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 "); typeof arr ...

  4. TypeError: expected string or buffer的解决方法

    错误种类:TypeError: expected string or buffer 具体错误解释:这是因为返回的变量不是字符类型,而导致此错误 具体解决方法:在具体程序段前加if判断语句,判断程序返回 ...

  5. 從 kernel source code 查出 版本號碼

    kernel/Makefile 1 VERSION = 4 2 PATCHLEVEL = 4 3 SUBLEVEL = 21 4 EXTRAVERSION = 5 NAME = Blurry Fish ...

  6. python基础===类的私有属性(伪私有)

    说在前面的一点: python明明有私有的定义方法就是在变量或者方法的面前加上双下滑线__,这个实际上是python的伪私有.只是一种程序员约定俗称的规定,加了就表示私有变量,但是你如果要在外部调用的 ...

  7. 压缩LDF档

    --压缩LDF档 USE VoucherServer; GO -- Truncate the log by changing the database recovery model to SIMPLE ...

  8. Mac iphone 使用 如何修改apple 用户名 XXX的mac Mac 与iphone如何连接 传递文件 为iphone增加铃声 iphone铃声的制作---城

    1.更改mac apple id Apple ID 即用户名称,您可以将其用于与 Apple 有关的所有操作.为某个 Apple 服务(如 iCloud 或 App Store)创建帐户时即创建了 A ...

  9. vue做购物车

    写一点废话,昨天敲代码找bug,找了好久都没找到,后来一哥们找到他说,找代码的bug就像男女朋友吵架,女问男你错了没,男说错啦,女再问错哪了,男傻眼了不知道错哪.在找代码的过程中一直知道我错啦就是找不 ...

  10. BlockingQueue drainTo()

    BlockingQueue BlockingQueue的核心方法:放入数据: offer(anObject):表示如果可能的话,将anObject加到BlockingQueue里,即如果Blockin ...