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的更多相关文章

  1. Codeforces Round 252 (Div. 2)

    layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. 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 ...

  3. Codeforces Round #252 (Div. 2) D

    http://codeforces.com/problemset/problem/441/D 置换群的基本问题,一个轮换内交换成正常顺序需要k-1次,k为轮换内元素个数 两个轮换之间交换元素,可以把两 ...

  4. codeforces Round #252 (Div. 2) C - Valera and Tubes

    贪心算法,每条路径最短2格,故前k-1步每次走2格,最后一步全走完 由于数据比较小,可以先打表 #include <iostream> #include <vector> #i ...

  5. Codeforces Round #252 (Div. 2) B. Valera and Fruits

    #include <iostream> #include <vector> #include <algorithm> #include <map> us ...

  6. Codeforces Round #252 (Div. 2) A - Valera and Antique Items

    水题 #include <iostream> #include <set> #include <vector> #include <algorithm> ...

  7. CodeForces 441E(Codeforces Round #252 (Div. 2))

    思路:dp[i][now][mark][len]   i 表示当前第i 次now存的是后8位,mark为第9位为0还是1 len第九位往高位还有几位和第9位相等.  只存后8位的原因:操作只有200次 ...

  8. Codeforces Round #252 (Div. 2) 441B. Valera and Fruits

    英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...

  9. 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 ...

随机推荐

  1. Qt on Android:资源文件系统qrc与assets

    使用 Qt 为 Android 开发应用时,有时我们的应用会携带一些资源文件,如 png . jpg 等,也可能有一些配置文件,如 xml 等.这些文件放在哪里呢? 有两种方式: qrc assets ...

  2. 杂项-DB:数据挖掘

    ylbtech-杂项-DB:数据挖掘 数据挖掘(Data mining)又译为资料探勘.数据采矿.它是数据库知识发现(英语:Knowledge-Discovery in Databases,简称:KD ...

  3. 捣鼓TinyMCE 粘贴图片并上传+Django后台

    前面一篇写了上传到Flask后台,但是我不熟悉Flask,原先想学习一下,据说是轻量级. 但是我发现,学习会浪费我大量的时间,因为我并不是以这个为生的,我的目标只是要完成功能,让我自己能尽早使用起来, ...

  4. python黏包解决方案

    解决方案 # 我们可以借助一个模块,这个模块可以把要发送的数据长度转换成固定长度的字节.这样客户端每次接 # 收消息之前只要先接受这个固定长度字节的内容看一看接下来要接收的信息大小,那么最终接受的数据 ...

  5. python网络编程三次握手和四次挥手

    TCP是因特网中的传输层协议,使用三次握手协议建立连接.当主动方发出SYN连接请求后,等待对方回答SYN+ACK[1],并最终对对方的 SYN 执行 ACK 确认.这种建立连接的方法可以防止产生错误的 ...

  6. C# 实现透明可移动窗体

    1.设置窗体属性 this.BackColor this.TransparencyKey = this.BackColor; 2.窗体加载图片 this.BackgroundImage = globa ...

  7. asp.net中Server.MapPath的使用

    http://www.studyofnet.com/news/184.html   本文导读:Server.MapPath()的全名是System.Web.HttpContext.Current.Se ...

  8. C++中关于文本内容的实用操作集合(新)(添加一些关于文件流的介绍)

    首先先给大家一个链接:http://baike.baidu.com/view/1679747.htm 主要是关于ios的使用,头文件要include<ios>,然后就可以调用下面的一些操作 ...

  9. Pyhton学习——Day9

    #文件操作的其他方法 ###############################################################################文件的内置操作函数# ...

  10. video标签实现简单视频背景+遇到问题(视频无法显示,不能自动播放)

    最近看网上有一些网站首页背景是炫酷的视频背景,就想模拟一个 1.video标签简介 video标签定义视频,就是可以在网页上实现视频的播放,详情见http://www.w3school.com.cn/ ...