HDU 3687 National Day Parade (暴力)
题意:给定 n 个人,在 n 列,问你移动最少的距离,使得他们形成一个n*n的矩阵。
析:这个题本来是要找中位数的,但是有特殊情况,所以改成暴力了,时间也很短,就是从第一个能够放左角的位置开始找,取最大值,挺简单暴力。
我一个同学竟然读对了题,WA了,然后又重新读题,把题意读错了,就是AC了。。。。
代码如下:
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 56 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> a[maxn]; int main(){
while(scanf("%d %d", &n, &m) == 2){
int x, y;
int cnt = 0;
if(!m && !n) break;
for(int i = 1; i <= n; ++i) a[i].clear();
for(int i = 0; i < n*n; ++i){
scanf("%d %d", &x, &y);
a[x].push_back(y);
}
for(int i = 1; i <= n; ++i) sort(a[i].begin(), a[i].end());
int ans = INF;
for(int i = 1; i <= m-n+1; ++i){
int cnt = 0;
for(int j = 1; j <= n; ++j){
for(int k = 0; k < a[j].size(); ++k){
cnt += abs(a[j][k]-i-k);
}
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
return 0;
} /*
3 3
1 1
1 1
2 1
1 1
2 1
2 1
3 1
3 1
3 1 */
HDU 3687 National Day Parade (暴力)的更多相关文章
- hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0
H - National Day Parade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 2920 分块底数优化 暴力
其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...
- HDU 3360 National Treasures 奇偶匹配的最低点覆盖
标题来源:pid=3360">HDU 3360 National Treasures 意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 ...
- hdu 5277 YJC counts stars 暴力
YJC counts stars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- HDU 5762 Teacher Bo (暴力)
Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hdu 4876 ZCC loves cards(暴力)
题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取当中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值 ...
- HDU 5442 Favorite Donut(暴力 or 后缀数组 or 最大表示法)
http://acm.hdu.edu.cn/showproblem.php?pid=5442 题意:给出一串字符串,它是循环的,现在要选定一个起点,使得该字符串字典序最大(顺时针和逆时针均可),如果有 ...
- HDU 5273 Dylans loves sequence 暴力递推
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5273 bc:http://bestcoder.hdu.edu.cn/contests/con ...
随机推荐
- Codeforces 383A - Milking cows
原题地址:http://codeforces.com/problemset/problem/383/A 题目大意:有 n 头奶牛,全部看着左边或者右边,现在开始给奶牛挤奶,给一头奶牛挤奶时,所有能看到 ...
- bzoj3275: Number
最小割...然后推一下可知不能的情况必定为一奇一偶,于是s->奇->偶->t.跑最小割即可. #include<cstdio> #include<cstring&g ...
- asp.net的decimal保留两位小数
C#的decimal保留两位小数 方法一: decimal d = 46.28111; string dStr = Math.Round( d,2 ).ToString(); 结果:dStr = 46 ...
- UVA 1659 Help Little Laura 帮助小劳拉 (最小费用流,最小循环流)
(同时也是HDU 2982,UVA的数据多) 题意:平面上有m条有向线段连接了n个点.你从某个点出发顺着有向线段行走,给走过的每条线段涂一种不同的颜色,最后回到起点.你可以多次行走,给多个回路涂色(要 ...
- HTMLParser 解析HTML
from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLP ...
- 随机变量的方差variance & 随机向量的协方差矩阵covariance matrix
1.样本矩阵 如果是一个随机变量,那么它的样本值可以用一个向量表示.相对的,如果针对一个随机向量,那么就需要利用矩阵表示,因为向量中的每一个变量的采样值,都可以利用一个向量表示. 然后,一个矩阵可以利 ...
- 《Python CookBook2》 第一章 文本 - 控制大小写 && 访问子字符串
控制大小写 任务: 将一个字符串由大写转成小写,或者泛起到而行之. 解决方案: >>> a = 'a'.upper() >>> a 'A' >>> ...
- C ~ 指针函数与函数指针的区别
一. 在学习arm过程中发现这“指针函数”与“函数指针”容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数,即本质是一个函数.函数返回类型是某 ...
- .NET开发工具
1.Two monitors 2.Visual Studio 3.TestDriven.NET 4.JetBrains ReSharper 5.VS样式 https://studiostyl.es/
- 关于“心脏出血”漏洞(heartbleed)的理解
前阵子“心脏出血”刚发生的时候读了下源代码,给出了自己觉得比较清楚的理解. -------------------------穿越时空的分割线--------------------------- ...