JZOJ 3382. 【NOIP2013模拟】七夕祭
Description
TYVJ七夕祭和11区的夏祭的形式很像。矩形的祭典会场由N排M列共计N×M个摊点组成。虽然摊点种类繁多,不过cl只对其中的一部分摊点感兴趣,比如章鱼烧、苹果糖、棉花糖、射的屋……什么的。Vani预先联系了七夕祭的负责人zhq,希望能够通过恰当地布置会场,使得各行中cl感兴趣的摊点数一样多,并且各列中cl感兴趣的摊点数也一样多。
不过zhq告诉Vani,摊点已经随意布置完毕了,如果想满足cl的要求,唯一的调整方式就是交换两个相邻的摊点。两个摊点相邻,当且仅当他们处在同一行或者同一列的相邻位置上。由于zhq率领的TYVJ开发小组成功地扭曲了空间,每一行或每一列的第一个位置和最后一个位置也算作相邻。现在Vani想知道他的两个要求最多能满足多少个。在此前提下,至少需要交换多少次摊点。
Input
接下来T行,每行两个整数x, y,表示cl对处在第x行第y列的摊点感兴趣。
Output
如果输出的字符串不是impossible, 接下来输出最小交换次数,与字符串之间用一个空格隔开。
Sample Input
样例输入1
2 3 4
1 3
2 1
2 2
2 3
样例输入2
3 3 3
1 3
2 2
2 3
Sample Output
样例输出1
row 1
样例输出2
both 2
Data Constraint
对于70% 的数据,N, M≤1000。
对于100% 的数据,1≤N, M≤100000,0≤T≤min(NM, 100000),1≤x≤N,1≤y≤M。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#define N 100007
using namespace std;
long long n, m, k, r[N], c[N], rs[N], cs[N], ans; long long read()
{
long long s = ;
char ch = getchar();
while (ch < '' || ch > '') ch = getchar();
while (ch >= '' && ch <= '') s = s * + ch - '', ch = getchar();
return s;
} int abs(int a) {if (a < ) return -a; return a;} void work1()
{
long long s = k / m;
for (int i = ; i <= m; i++)
cs[i] += cs[i - ] + (c[i] - s);
sort(cs + , cs + m + );
long long sum = ;
for (int i = ; i <= m; i++)
sum += abs(cs[i] - cs[( + m) / ]);
ans += sum;
} void work2()
{
long long s = k / n;
for (int i = ; i <= n; i++)
rs[i] += rs[i - ] + (r[i] - s);
sort(rs + , rs + n + );
long long sum = ;
for (int i = ; i <= n; i++)
sum += abs(rs[i] - rs[( + n) / ]);
ans += sum;
} int main()
{
n = read(), m = read(), k = read();
int x, y;
for (int i = ; i <= k; i++)
{
x = read(), y = read();
r[x]++, c[y]++;
}
if (k % n == && k % m == )
{
work1();
work2();
printf("both ");
printf("%lld", ans);
}
else if (k % n == )
{
work2();
printf("row ");
printf("%lld", ans);
}
else if (k % m == )
{
work1();
printf("column ");
printf("%lld", ans);
}
else printf("impossible");
}
JZOJ 3382. 【NOIP2013模拟】七夕祭的更多相关文章
- [JZOJ3382] [NOIP2013模拟] 七夕祭 解题报告
Description 七夕节因牛郎织女的传说而被扣上了「情人节」的帽子.于是TYVJ今年举办了一次线下七夕祭.Vani同学今年成功邀请到了cl同学陪他来共度七夕,于是他们决定去TYVJ七夕祭游玩. ...
- JZOJ【NOIP2013模拟联考14】隐藏指令
JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...
- 【NOIP2013模拟】七夕祭
题目描述七夕节因牛郎织女的传说而被扣上了「情人节」的帽子.于是TYVJ今年举办了一次线下七夕祭.Vani同学今年成功邀请到了cl同学陪他来共度七夕,于是他们决定去TYVJ七夕祭游玩. TYVJ七夕祭和 ...
- JZOJ 3383. 【NOIP2013模拟】太鼓达人
3383. [NOIP2013模拟]太鼓达人 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits ...
- JZOJ 3493. 【NOIP2013模拟联考13】三角形
3493. [NOIP2013模拟联考13]三角形(triangle) (File IO): input:triangle.in output:triangle.out Time Limits: 10 ...
- JZOJ 3487. 【NOIP2013模拟联考11】剑与魔法(dragons)
3487. [NOIP2013模拟联考11]剑与魔法(dragons) (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB De ...
- JZOJ 3470. 【NOIP2013模拟联考8】最短路(path)
470. [NOIP2013模拟联考8]最短路(path) (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿
3388. [NOIP2013模拟]绿豆蛙的归宿 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limi ...
- JZOJ 3463. 【NOIP2013模拟联考5】军训
3463. [NOIP2013模拟联考5]军训(training) (Standard IO) Time Limits: 2000 ms Memory Limits: 262144 KB Deta ...
随机推荐
- C# this索引器
- Shell笔试题2
1. 从a.log文件中提取包含"WARNING"或"FATAL",同时不包含"IGNOR"的行,然后提取以":"分割的 ...
- a标签常用跳转
1.a标签跳转qq <a href="http://wpa.qq.com/msgrd?v=3&uin=123456789&site=qq&menu=yes&qu ...
- webpake-node-sass 报错
问题描述: npm run dev 就报错,在安装node-sass错误 解决方法 : 找到node_modules下的node-sass文件,进入,如果没有vendor文件夹,就创建一个空文件夹,命 ...
- Lua学习---函数定义
1.函数定义的格式: Lua使用function定义函数,语法如下: function function_name (arc) --arc表示参数列表,函数的参数列表可以为空 --body end 上 ...
- 【extjs6学习笔记】0.4 准备: 书籍与文档
Ext JS 6 By Example Ext JS Essentials Learning Ext JS - Fourth Edition Ext JS 6: Getting Started htt ...
- CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1
CentOS-7.3.1611编译安装 Nginx-1.12.1+mysql-5.7.19+PHP-7.1.8+zabbix-3.4.1 下载软件 1.下载nginx http://nginx.org ...
- ubuntu16.04解决屏幕适应问题
打开ubuntu登录进去后,输入: sudo apt-get installopen-vm-tools sudo apt-get install open-vm* 然后重启(reboot),即可解决 ...
- [Git] Create a new repository on the command line
echo "# xxx" >> README.md git init git add README.md git commit -m "first commi ...
- echarts 相关属性介绍
title: {//图表标题 x: 'left', //组件离容器左侧的距离,left的值可以是像20,这样的具体像素值, 可以是像 '20%' 这样相对于容器高宽的百分比,也可以是 'lef ...