【模拟】 Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) C. Tests Renumeration
题意:有一堆数据,某些是样例数据(假设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的更多相关文章
- 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的数量的最小值有关系,所以 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Little Mathematics Knowledge 数学小常识
The sum of arithmetic sequence The sum of geometric sequence A special formula : n·n! = (n+1)! - n! ...
- node起本地服务器以及实现代理,前端接口转发
上一篇文章写了使用docker来做nginx镜像实现本地的页面代理以及接口转发,但是需要下载docker,这个对于很多人来说还是显得比较麻烦,于是这个文章就是介绍如何只用node就可以代理本地的页面和 ...
- GCC在C语言中内嵌汇编 asm __volatile__ 【转】
转自:http://blog.csdn.net/pbymw8iwm/article/details/8227839 在内嵌汇编中,可以将C语言表达式指定为汇编指令的操作数,而且不用去管如何将C语言表达 ...
- android CVE 漏洞汇总
arm exploits 技术教程: Learning Pentesting for Android Devices CVE-2015-1530 ,CVE-2015-1474 两个android整数溢 ...
- Linux 内核通知链随笔【中】【转】
转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...
- python--tesseract
tesseract的介绍 我们爬虫会受到阻碍,其中一个便是我们在模拟登陆或者请求一些数据的时候,出现的图形验证码,因此我们需要一种能叫图形验证码识别成文本的技术.将图片翻译成文字一般称为光学文字识别( ...
- Dubbo之旅--注册中心
在介绍Dubbo的内部逻辑的时候提到很多次注册中心的概念.实现注册中心的有很多,主要是以下四个注册中心分别是: Multicast注册中心 Zookeeper注册中心 Redis注册中心 Simple ...
- golang-指针,函数,map
指针 普通类型变量存的就是值,也叫值类型.指针类型存的是地址,即指针的值是一个变量的地址.一个指针只是值所保存的位置,不是所有的值都有地址,但是所有的变量都有.使用指针可以在无需知道变量名字的情况下, ...
- Django_admin源码流程
admin.py from django.contrib import admin from . import models """ 通过原生的django admin来 ...
- 设置iframe 载入页面的效果跟直接打开这个页面一样
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...