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 ...
随机推荐
- 6 Javascript:函数
函数 函数是面向任务的. 当我们面临一个须要可问题的时候.往往无处下手.这时候.须要将问题分解为多个任务,从而逐一击破. 这里就须要函数的帮助. 语法 function Name() { Body() ...
- WebService概述和CXF入门小程序
一. 什么是WedService? WebService不是框架, 甚至不是一种技术, 而是一种跨平台,跨语言的规范, WebService的出现是为了解决这种需求场景: 不同平台, 不同语言所编写的 ...
- U盘安装 CentOS 64bit (dell c6100, CentOS6.3, 64bit)
在淘宝买了一个server,dell c6100,64bit, 曾经系统是black apple.近期又买了一块企业级硬盘打算装CentOS. 综合各方面原因决定安装6.3版本号. 我參考了http: ...
- hdu 4603 Color the Tree
这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...
- java中的NIO
使用传统的输入输出流,当读取输入流中的数据如果没有没有读到有效的数据时,程序将在此处阻塞该线程的执行(使用InputStream的read方法从流中读取数据时,如果数据源中没有数据,它也会阻塞该线程) ...
- 对于NAS,IP SAN以及iSCSCI SAN存储的一些认识和理解
一直以来用户对于在选购存储产品上有许多不清楚,市场上有NAS, FC SAN,IP SAN和iSCSCI SAN产品,到底哪种类型的产品更适合支撑企业的应用系统呢? 我们经常可以听到用户讲: “NAS ...
- C#将内容导出到Word到指定模板
昨天做了下导入导出Excel文件,今天研究了下导出Word文件. 从网上找了半天才找到了一个能导出到指定模板的,在这里总结下. 导出模板原理就是利用的替换占位符. 我这里先建立好了一个模板, 接下来写 ...
- ZBrush中标准几何体与Polymesh
通过对ZBrush的学习,相信您已经对这款软件有了一定的了解,文本我们主要学习ZBrush®的3D物体标准几何体的特性和使用方法.在ZBrush中只有Polymesh(多边形网格)物体才能使用雕刻笔刷 ...
- 一个helloword hibernate配置以及查询
搭建一个Hibernate环境,开发步骤: 1. 下载源码 版本:hibernate-distribution-3.6.0.Final 2. 引入jar文件 hibernate3.jar核心 + ...
- 使用layer.tips实现鼠标悬浮时触发事件提示消息实现
代码: <body> <label id="test" onmouseover="show('test')"> 你瞅啥!?过来试试! & ...