Codeforces Round #252 (Div. 2)-C,D
C题就是一个简单的模拟。首先给每一个人两个。然后把剩下的都给一个人就好了。
给的时候蛇形给。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define LL __int64
#define maxn 330000
int main()
{
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k))
{
int leap=1;
int stx=1;
int sty=1;
int ms=n*m-(k*2)+2;
printf("%d",ms);
while(ms--)
{
printf(" %d %d",stx,sty);
sty+=leap;
if(sty<1||sty>m)
{
if(sty<1)sty=1;
if(sty>m)sty=m;
stx++;leap=-leap;
}
}
cout<<endl;
k--;
while(k--)
{
printf("%d ",2);
printf("%d %d ",stx,sty);
sty+=leap;
if(sty<1||sty>m)
{
if(sty<1)sty=1;
if(sty>m)sty=m;
stx++;leap=-leap;
}
printf("%d %d\n",stx,sty);
sty+=leap;
if(sty<1||sty>m)
{
if(sty<1)sty=1;
if(sty>m)sty=m;
stx++;leap=-leap;
}
}
}
return 0;
}
D:首先依据环。把每个环分成一组。记录下此时至少须要交换all次,才干回归到恒等排列。
1。假设all大于p。
那么我们就应该把all降低。
对于一个环。随意两个点交换都能够把环分成两份。all-1;
对于每次降低,我们寻找环的最小值最小的环,然后在这个环中寻找最小值,然后交换这两个点。
2,假设all小于p。那么我们应该把all增大。
那么我们就能够把1号节点和随意节点交换。来达到增大all的目的。
注意,1号节点不和本身的环交换。
而且1号节点和随意一个环仅仅交换一次。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define LL __int64
#define maxn 3300
int a[maxn];
int b[maxn];
int vis[maxn];
vector<int>vec;
vector< vector<int> >ans;
struct list
{
int x,y;
} node;
vector<list>pr;
bool cmp(vector<int>a,vector<int>b)
{
return a[0]<b[0];
}
struct listt
{
int x;
int index;
int l,r;
friend bool operator <(const listt &a,const listt &b)
{
return a.x>b.x;
}
}tt;
priority_queue<listt>que;
int main()
{
int n,m;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
scanf("%d",&m);
memset(vis,0,sizeof(vis));
vec.clear();
ans.clear();
int all=0;
for(int i=1; i<=n; i++)
{
if(vis[i])continue;
int j=i;
vec.clear();
while(b[j]!=j&&vis[j]==0)
{
vec.push_back(j);
vis[j]=1;
j=b[j];
}
if(vec.size())
{
// sort(vec.begin(),vec.end());
ans.push_back(vec);
all+=vec.size()-1;
} }
sort(ans.begin(),ans.end(),cmp);
pr.clear();
if(all<=m)
{
all=m-all;
if(ans.size()==0)
{
node.x=1;
for(int i=2; i<=all+1; i++)
{
node.y=i;
pr.push_back(node);
}
}
else
{
node.x=1;
int j=0;
if(ans[0][0]==1)j++;
for(int i=2; i<=n&&all>0; i++)
{
if(b[i]==i)
{
all--;
node.y=i;
pr.push_back(node);
}
if(ans.size()>j&&ans[j][0]==i)
{
all--;
node.y=i;
j++;
pr.push_back(node);
}
}
}
}
else
{
int qian=all;
all=all-m;
int i=0;
while(!que.empty())que.pop();
for(i=0;i<ans.size();i++)
{
tt.index=i;
tt.x=ans[i][0];
que.push(tt);
}
while(all)
{
tt=que.top();
que.pop();
i=tt.index;
node.x=ans[i][0];
int minn=9999;
int st=0;
for(int j=1;j<ans[i].size()&&all>0;j++)
{
if(minn>ans[i][j])
{
minn=ans[i][j];
st=j;
}
}
node.y=minn;
all--;
pr.push_back(node);
vec.clear();
minn=9999;
vec.push_back(ans[i][st]);
for(int j=1;j<st;j++)
{
vec.push_back(ans[i][j]);
}
if(vec.size()>1)
{
ans.push_back(vec);
tt.index=ans.size()-1;
tt.x=vec[0];
que.push(tt);
}
vec.clear();
vec.push_back(ans[i][0]);
for(int j=st+1;j<ans[i].size();j++)
{
vec.push_back(ans[i][j]);
}
if(vec.size()>1)
{
ans[i]=vec;
tt.index=i;
tt.x=vec[0];
que.push(tt);
}
i++;
}
}
cout<<pr.size()<<endl;
for(int i=0; i<pr.size(); i++)
{
printf("%d %d\n",pr[i].x,pr[i].y);
}
}
return 0;
}
Codeforces Round #252 (Div. 2)-C,D的更多相关文章
- Codeforces Round 252 (Div. 2)
layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #252 (Div. 2) D
http://codeforces.com/problemset/problem/441/D 置换群的基本问题,一个轮换内交换成正常顺序需要k-1次,k为轮换内元素个数 两个轮换之间交换元素,可以把两 ...
- codeforces Round #252 (Div. 2) C - Valera and Tubes
贪心算法,每条路径最短2格,故前k-1步每次走2格,最后一步全走完 由于数据比较小,可以先打表 #include <iostream> #include <vector> #i ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits
#include <iostream> #include <vector> #include <algorithm> #include <map> us ...
- Codeforces Round #252 (Div. 2) A - Valera and Antique Items
水题 #include <iostream> #include <set> #include <vector> #include <algorithm> ...
- CodeForces 441E(Codeforces Round #252 (Div. 2))
思路:dp[i][now][mark][len] i 表示当前第i 次now存的是后8位,mark为第9位为0还是1 len第九位往高位还有几位和第9位相等. 只存后8位的原因:操作只有200次 ...
- Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放具体解释
本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说,主要通过MFC单文档视图实现显示BMP图片空间几何变换.包含图像平移.图形 ...
- AlertDialog自己定义View的使用方法+怎样改变弹出框的大小
android系统定义了弹出框,支持我们自己定义布局: public AlertDialog getEditCustomDialog() { LayoutInflater inflater = get ...
- 技术总结--android篇(一)--MVC模式
先介绍下MVC模式:MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显 ...
- Windows环境下msysgit安装git flow
git flow是git的一个扩展集,它基于Vincent Driessen的分支模型,能够用来简化代码的版本号公布流程. 本文讲述怎样为msysgit安装git flow. 下载getopt.exe ...
- Android应用之——微信微博第三方sdk登录分享使用过程中的一些常见问题
前言 近期在使用第三方登录和分享的过程中遇到了非常多问题,一方面能够归结为自己经验的不足,还有一方面事实上也说明了官方文档的含糊不清.这篇博文不会写关于怎样使用第三方登录分享,由于官方文档已经写明了步 ...
- 杂项-DB:ETL(数据库仓库技术)
ylbtech-杂项-DB:ETL(数据库仓库技术) ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).交互转换(transfor ...
- 操作系统-容器-引擎容器-百科:Docker
ylbtech-操作系统-容器-引擎容器-百科:Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上 ...
- Java入门第一季
慕课网:http://www.imooc.com/learn/85 Java入门知识第一季 1.Java开发环境和IDE的使用: 2.变量和常量 3.常用的运算符 4.流程控制语句 5.数组:使用Ar ...
- 达夫设备之js
最近阅读<高性能JavaScript>时,在书中的“达夫设备“ . 对此,有些感悟,同时有些疑问,希望看到的朋友,能帮忙解释下,在此先提前感谢了. 1. 先说自己的理解吧: ”达夫设备“的 ...
- C语言文件操作函数之ferror & feof & clearerr
这些函数都是和文件读写时发生错误有关,下面一一分析: 1:ferror 原型:int ferror(FILE * fp) 作用:测试一个文件流是否被设置了错误标识符,如果是返回非 0 整数,否则返回 ...