[NOIP 2010] 引入入城
[题目链接]
[算法]
显然 , 每个第一行的成市控制的一定是一段区间
那么 , 问题就转化为了经典的区间覆盖问题 , 贪心即可 , 时间复杂度 : O(N^3)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 510
const int dx[] = {,,-,};
const int dy[] = {-,,,}; int n,m,len;
int a[MAXN][MAXN];
pair<int,int> range[MAXN];
bool mark[MAXN];
bool visited[MAXN][MAXN]; template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool valid(int x,int y)
{
return x >= && x <= n && y >= && y <= m;
}
inline pair<int,int> bfs(int x,int y)
{
int l = , r = ;
queue< pair<int,int> > q;
memset(visited,false,sizeof(visited));
q.push(make_pair(x,y));
visited[x][y] = true;
while (!q.empty())
{
pair<int,int> cur = q.front();
q.pop();
for (int i = ; i < ; i++)
{
int x = cur.first + dx[i] , y = cur.second + dy[i];
if (valid(x,y) && a[x][y] < a[cur.first][cur.second] && !visited[x][y])
{
visited[x][y] = true;
q.push(make_pair(x,y));
}
}
}
for (int i = ; i <= m; i++)
{
if (visited[n][i])
{
mark[i] = true;
if (!l) l = r = i;
else r++;
}
}
return make_pair(l,r);
}
inline bool cmp(pair<int,int> a,pair<int,int> b)
{
if (a.first != b.first) return a.first < b.first;
else return a.second > b.second;
} int main()
{ read(n); read(m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
read(a[i][j]);
}
}
for (int i = ; i <= m; i++)
{
pair<int,int> tmp = bfs(,i);
if (tmp.first) range[++len] = tmp;
}
int cnt = ;
for (int i = ; i <= m; i++)
{
if (mark[i])
cnt++;
}
if (cnt < m)
{
printf("0\n");
printf("%d\n",m - cnt);
return ;
}
sort(range + ,range + len + ,cmp);
int r = range[].second , ans = , pos = ;
while (r < m)
{
int mx = r;
while (pos <= len && range[pos].first <= r + )
{
mx = max(mx,range[pos].second);
pos++;
}
r = mx;
ans++;
}
printf("1\n%d\n",ans); return ; }
[NOIP 2010] 引入入城的更多相关文章
- [NOIP 2010]饮水入城 搜索+贪心
考试的时候写了个dfs找出来了,最后处理的时候想到了贪心,但是正确性没有想通.然后想了想动规,也没想通.最后没办法,用状态的话用了个状压,弄了40分. 正解是bfs+贪心.Dfs也有过的. 下面题解引 ...
- [NOIP 2010] 引水入城
搜索+贪心. 参考博客:http://blog.sina.com.cn/s/blog_8442ec3b0100xib1.html 主要是要看出来,如果有解的话,每个沿湖城市能够流到的范围是连续的区间. ...
- noip 2010 引水入城 贪心 + 搜索
不难分析出如果有解则每个蓄水厂所能覆盖到的干旱城市一定是连续的.否则,中间那些没被覆盖的部分永远都不能被覆盖到. 当然,每个蓄水厂所覆盖的城市有可能不连续,不过既然有解,则一定都是连续的.我们可以开一 ...
- Codevs 1066 引水入城 2010年NOIP全国联赛提高组
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 在一个遥远的国度 ...
- 引水入城 2010年NOIP全国联赛提高组(bfs+贪心)
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个遥远 ...
- CODEVS 1066/洛谷 P1514引水入城
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个遥远的国 ...
- CCF CSP 201703-5 引水入城(50分)
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-5 引水入城 问题描述 MF城建立在一片高原上.由于城市唯一的水源是位于河谷地带的 ...
- 洛谷P1514 引水入城
洛谷P1514 引水入城 原题链接 一道好题...细节真多 第一次提交90分,然后就GG了,不知从何改起 其实比较简单吧... 首先,一个点的水流向最后一排,一定可以形成一个区间. 不行的话肯定GG ...
- NOIP 2010
tags: NOIP 并查集 动态规划 搜索 categories: 信息学竞赛 总结 机器翻译 乌龟棋 关押罪犯 引水入城 机器翻译 Solution 维护一个队列, 每次从词典中查词时将单词加入队 ...
随机推荐
- xtu summer individual-4 A - Beautiful IP Addresses
Beautiful IP Addresses Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on Code ...
- oracle exp direct 执行机制
使用直接导出模式 direct=y exp 比传统模式导出快一倍 oracle提供2种模式导出表数据,传统模式CONVENTIONAL PATH和直接模式DIRECT PATH,有direct指定. ...
- 转盘抽奖 canvas & 抽奖 H5 源码
转盘抽奖 canvas https://github.com/givebest/wechat-turntalbe-canvas https://blog.givebest.cn/GB-canvas-t ...
- [luoguP1033] 自由落体(模拟?)
传送门 这不能算是数论题... 卡精度这事noip也做的出来.. 代码 #include <cmath> #include <cstdio> int n, ans; doubl ...
- SOJ 2818_QQ音速
[题意]两只手,一次只能用一只手按一个键子(0,1,2,3),给出从i键到j键所需的消耗的体力,求依次按下一系列键子所需最小体力. [分析] 法一:开一个三维数组,分别记录移动到位置及左右手按的键子. ...
- 更新数据库中数据时出现: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences 问题
使用workbench在数据库中更新数据时报错: You are using safe update mode and you tried to update a table without a WH ...
- modem&NIC&sound card
Rate: Phone:8 k hz radio:22050 hz Digital Video camcorder; miniDV; DAT LP mode:32 k hz Audio CD MP ...
- C#的SplitPanel如何设置上下和左右
定位到Orientation属性即可
- Android自己主动提示文本框(AutoCompleteTextView)
自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...
- hadoop(九) - hbase shell命令及Java接口
一. shell命令 1. 进入hbase命令行 ./hbase shell 2. 显示hbase中的表 list 3. 创建user表,包括info.data两个列族 create 'user' ...