Codeforces 1236C. Labs
注意到 $f(X,Y)+f(Y,X)$ 是一个定值(因为每个元素都不相同)
所以如果能让 $f(X,Y)$ 与 $f(Y,X)$ 尽可能接近,那么一定是最优的
所以可以这样构造:把 $n^2$ 的序列每 $n$ 个分成一组,一共 $n$ 组
对于第一个集合,拿出当前第 $1$ 组最大的,第 $2$ 组最小的,第 $3$ 组最大的...以此类推
对于第二个集合,拿出当前第 $1$ 组最小的,第 $2$ 组最大的,第 $3$ 组最小的...以此类推
对于第三个集合,拿出当前第 $1$ 组最大的,第 $2$ 组最小的,第 $3$ 组最大的...以此类推
以此类推
然后可以发现,对于任意两个集合 $X,Y$ ,如果集合大小为偶数,$f(X,Y)$ 和 $f(Y,X)$ 的差值为 $0$ 否则为 $1$
因为显然集合大小为奇数时 $f(X,Y)$ 和 $f(Y,X)$ 不可能相等,那么此方案一定为最优方案
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=;
int n,A[N][N],L[N],R[N];
int main()
{
n=read(); int tot=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++) A[i][j]=++tot;
L[i]=,R[i]=n;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if((i^j)&) printf("%d ",A[j][L[j]++]);
else printf("%d ",A[j][R[j]--]);
}
puts("");
}
return ;
}
Codeforces 1236C. Labs的更多相关文章
- Codeforces Round #593 (Div. 2) C. Labs A. Stones
题目:https://codeforces.com/contest/1236/problem/A 思路:两种操作收益都是3 且都会消耗b 操作2对b消耗较小 则可优先选择操作2 再进行操作1 即可得到 ...
- Codeforces Round #593 (Div. 2) C. Labs
题目:https://codeforces.com/contest/1236/problem/C 思路:将 n ^ 2 个 lab 平分为 n 个 group group A 和 B 组成的 有序对 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- iOS 8 - Can't Install Enterprise App
http://stackoverflow.com/questions/25733299/ios-8-cant-install-enterprise-app 版权声明:本文为博主原创文章,未经博主允许不 ...
- mysql数据库的索引
什么是索引 索引就是一种优化查询的数据结构: 为什么要加索引 因为创建索引可以大大提高系统的查询性能. 怎么提高查询性能的 简单的理解:一张数据量比较大的表格如果没有添加任何索引,那我们在执行查询的时 ...
- centos-Hadoop集群 安装同步时间
1.安装 yum search cache ntpdate xcall.sh "yum install -y ntpdate.x86_64" 2.同步 xcall.sh " ...
- smarty section 循环不同的四个样式
<div class="moban_spzs"> <{section name=goodslist loop=$strdata6}> <{if $sm ...
- 分布式存储ceph介绍(1)
一.Ceph简介: Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统.ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展.在国内一些公司的云环 ...
- wxPython在frame窗口修改图标
self.m_panel4 = wx.Panel( self.m_notebook5, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TR ...
- Perfect-Server-Swift学习记录
开发环境搭建: https://github.com/Perfect-Server-Swift-LearnGuide/PerfectLearnGuide 中文文档学习: https://www.per ...
- Code First EF 多对多时拆分两个一对多
(*)多对多中还可以为中间表建立一个实体方式映射.当然如果中间关系表还想有其他字段,则要必须为中间表建立实体类(中间表和两个表之间就是两个一对多的关系了). demo项目 WebApp22 GitHu ...
- .net 开源 FTP 组件 edtFTPnet
edtFTPnet官方网站:http://www.enterprisedt.com/products/edtftpnet/ 目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll ...
- Java和SQL取两个字符间的值
Java String str = "abcdefg"; String result = str.substring(str.indexOf(">")+1 ...