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. 深入分析JavaWeb Item39 -- 监听器(Listener)学习进阶

    一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...

  2. Linux以下基于TCP多线程聊天室(client)

    不怎么会弄这个博客的排版,就直接将代码附上: 主要是使用多线程去等待接受数据和发送数据.以下是client的代码: tcpsed.h文件 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  3. lightoj--1410--Consistent Verdicts(技巧)

    Consistent Verdicts Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Su ...

  4. NSURLSession简介

    NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...

  5. [makefile]如何设置不同目录的代码(.c),生成到指定目录下(./debug/.o))

    部分代码跟makefile不在同一目录,有没有好的方法来设置依赖关系,我找到三种方法,但感觉都不完美,下面我会把他列出来并加以说明,不知有没有更好的方法,makefile本身也不是很熟,请大家指教: ...

  6. div纵向居中的方法(转载)

    方法一这个方法把一些 div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align property 属性. <div id="wrapper"> ...

  7. CentOS 7.5安装pycharm

    环境 安装环境: windows 10 Pro CentOS Linux release 7.6.1810 (Core) VMWare Workstation 15 Pro 安装图形化界面包 首先更新 ...

  8. Python笔记25-----------创建二维列表【浅copy】和转置

    一.创建二维列表 1.二维列表创建第二维的时候,如果采用*2这种方式,这是一种浅复制的方式,同时引用到同一个list,如上图的C. 这种形式,不方便修改C[ i ][ j ]的数据,如果改C[ 0 ] ...

  9. [置顶] 谷歌大牛 Jeff Dean 是如何成为互联网战神的

    谷歌大牛 Jeff Dean 是如何成为互联网战神的 原文链接: Will Oremus   翻译: 伯乐在线- Lex Lian 译文链接: http://blog.jobbole.com/4772 ...

  10. 趣学算法 PDF pdf 下载 陈小玉版

    趣学算法pdf高清无水印版下载 最近在网上找趣学算法pdf,最后还是买了完整版,今天将本书分享出来,分享给那些和我一样在网上苦苦寻找的小可爱们,有条件的话请支持正版! 链接:https://pan.b ...