题目链接

microhhh的回城

解题思路

这题挺有意思的。本来寻思放在\(DS\)第一次练习赛应该不会很难吧,结果愣是卡在数据范围上写不出来。

然后暴力过掉了,但是用了\(1019ms\)。感觉可以继续优化。(放一下暴力代码,不解释了)

#include<stdio.h>
#include<string.h>
int n,m;
char map[10010][10010];//char防止卡空间
int main(){
int i,T,j,x,c,b,l,k;
scanf("%d",&T);
for(k=1;k<=T;k++){
printf("Case %d #:",k);
memset(map,0,sizeof(map));
scanf("%d%d%d",&n,&m,&l);
for(i=0;i<m;i++){
scanf("%d%d",&c,&b);
map[c][b]=1;
map[c+1][b]=2;
}
for(i=0;i<n;i++){
x=i;
for(j=1;j<=l;j++){
if(map[x][j]==1)x++;
else if(map[x][j])x--;
}
printf(" %d",x);
}
printf("\n");
}
return 0;
}

又在想如果在暴力模拟的时候记录一下每列的头结点,可以进行二分查找,复杂度会下来一个\(logn\)。

码量略大,不过优化了不少。代码这里就不放了。

然后突然想到其实这就是一个交换终点的问题。从下面向上每一条边都对应一次终点的交换,去掉重边即可。\(7msAC\)。

AC代码

#include<cstdio>
#include<algorithm>
int n,m,l;
struct Path{
int c,k;
bool operator<(const Path&b)const{return k>b.k||(k==b.k&&c<b.c);}
}e[100010];
int pre[10010],end[10010],T,i,j,p,q,t;
int main(){
scanf("%d",&T);
for(j=1;j<=T;j++){
scanf("%d%d%d",&n,&m,&l);
for(i=0;i<m;i++)scanf("%d%d",&e[i].c,&e[i].k);
std::sort(e,m+e);
for(i=0;i<n;i++)pre[i]=i;
for(i=0;i<m;i++){
p=e[i].c,q=p+1;
if(i&&e[i].c==e[i-1].c&&e[i].k==e[i-1].k);//去重
else t=pre[p],pre[p]=pre[q],pre[q]=t;
}
printf("Case %d #:",j);
for(i=0;i<n;i++)printf(" %d",pre[i]);
putchar('\n');
}
return 0;
}

最新文章

  1. ubuntu与win10互换硬盘
  2. Request中的各种方法
  3. c#中对rgb的使用
  4. js 合并表格
  5. &quot;provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接&quot;错误的解决方法
  6. Java JTable 表格 获取存储路径,文件名 ,导出excel表格
  7. gulp - connect
  8. 使用AS3.0代码实现给图片添加滤镜的模糊与斜角效果
  9. php缓存
  10. 【转载】如何在C语言中调用shell命令
  11. SEO的URL如何优化才是最佳
  12. 今天修改 wifi hal 的时候碰见一个问题
  13. eclipse打包
  14. Java开源生鲜电商平台-监控模块的设计与架构(源码可下载)
  15. Python内置函数(47)——open
  16. How to Make a Computer Operating System
  17. Tensorflow 报错:tensorflow.python.framework.errors_impl.InternalError: Failed to create session.
  18. delete content on the right of cursor, Mac
  19. loadrunner&#160;场景设计-设置结果文件保存路径
  20. ConcurrentHashMap中的putIfAbsent方法的使用以及返回值的含义

热门文章

  1. 弹性计算服务(Elastic Compute Service) / 云服务器 ECS
  2. zoj-3872 Beauty of Array (dp)
  3. C++中函数的形式参数引用
  4. HTML &lt;keygen&gt; 标签(&#128078; 已废弃)
  5. Google Developer Profile
  6. array auto slice
  7. CSS 实现文本的竖向排版
  8. 使用控制台启动Android设备模拟器
  9. SPC空投火爆来袭!区块链技术落地加速!
  10. MySQL 修改数据表