USACO 2018 December Contest Silver 题解

T1 Convention

【题意】

n n n头奶牛,第 i i i头在 a [ i ] a[i] a[i]时刻到达,有 m m m量大巴车接他们,每辆最多载 k k k人(注意不需要坐满才能走),求等候时间最长的奶牛最短时间是多少

【分析】

又是最大值最小的问题,很容易想到二分答案

二分等候时间最长的奶牛等候 m i d mid mid分钟

c h e c k check check函数也很好想

先按到达时间把奶牛排序

记录第一个上车的时间和已上车的人数

如果当前奶牛和第一个上车的奶牛的间隔时间 > m i d >mid >mid或已上 k k k人就换下一辆,最后判断用的车子数量是否少于 m m m即可。

还是很裸的二分。

【代码】

#include 
using namespace std ;

#define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++)
#define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define clr(a) memset(a, 0, sizeof(a))
#define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp)
#define ass(a, sum) memset(a, sum, sizeof(a))

#define ls ((rt) << 1)
#define rs ((rt) << 1 | 1)
#define lowbit(x) (x & -x)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define ENDL cout << endl
#define SZ(x) ((int)x.size())

typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> vi ;
typedef pair <int, int> pii ;
typedef pair <ll, ll> pll ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
typedef map <ll, ll> mll ;

const int N = 100010 ;
const double eps = 1e-8 ;
const int iinf = INT_MAX ;
const ll linf = 2e18 ;
const double dinf = 1e30 ;
const int MOD = 1000000007 ;

inline int read(){
    int X = 0, w = 0 ;
    char ch = 0 ;
    while (!isdigit(ch)) { w |= ch == '-' ; ch = getchar() ; }
    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ;
    return w ? - X : X ;
}

void write(int x){
     if (x < 0) putchar('-'), x = - x ;
     if (x > 9) write(x / 10) ;
     putchar(x % 10 + '0') ;
}

void print(int x) {
    cout << x << endl ;
    exit(0) ;
}

void PRINT(string x) {
    cout << x << endl ;
    exit(0) ;
}

void douout(double x){
     printf("%lf\n", x + 0.0000000001) ;
}

int n, m, c, ans ;
int a[N] ;

bool check(int x) {
	int now = 1, fitake = a[1], peo = 1 ;
	for (int i = 2; i <= n; i++) {
		if (a[i] - fitake > x) now++, fitake = a[i], peo = 1 ;
		else {
			peo++ ;
			if (peo > c) now++, fitake = a[i], peo = 1 ;
		}
		  
	}
	if (now > m) return false ;
	return true ; 
}

int main() {
	scanf("%d%d%d", &n, &m, &c) ;
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]) ;
	sort(a + 1, a + n + 1) ;
	int l = 0, r = iinf / 2  ;
	while (l + 1 < r) {
		int mid = (l + r) >> 1 ;
		if (check(mid)) r = mid ;
		else l = mid ;
	} 
	printf("%d\n", r) ;
}

T2 Convention II

【题意】

有n头奶牛到x地。他们有秩序的吃草。吃草顺序很特殊:如果你到时没有其他奶牛,那么你就会先吃;如果已经有其他奶牛等候了,下一头进食的是资历最高的奶牛。

每头奶牛进食长度不等,询问等候时间最长的奶牛等了多久。

【分析】

很容易想到优先队列

先对所有牛按照到达时间排序。

然后我们就按照时间发展这样枚举下去,

当一头牛吃好后,我们把已经在等待的牛领出来,扔进优先队列里头,优先队列按照资历排序依次出列,进队时顺便记录时间戳,然后和现在吃草时间做差取max

写代码细节有一些,慢慢调吧。

大概思路就是这样吧,也没有卡什么,数据也蛮水的。

【代码】

#include 
using namespace std ;

#define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++)
#define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define clr(a) memset(a, 0, sizeof(a))
#define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp)
#define ass(a, sum) memset(a, sum, sizeof(a))

#define ls ((rt) << 1)
#define rs ((rt) << 1 | 1)
#define lowbit(x) (x & -x)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define ENDL cout << endl
#define SZ(x) ((int)x.size())

typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> vi ;
typedef pair <int, int> pii ;
typedef pair <ll, ll> pll ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
typedef map <ll, ll> mll ;

const int N = 100010 ;
const double eps = 1e-8 ;
const int iinf = INT_MAX ;
const ll linf = 2e18 ;
const double dinf = 1e30 ;
const int MOD = 1000000007 ;

inline int read(){
    int X = 0, w = 0 ;
    char ch = 0 ;
    while (!isdigit(ch)) { w |= ch == '-' ; ch = getchar() ; }
    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ;
    return w ? - X : X ;
}

void write(int x){
     if (x < 0) putchar('-'), x = - x ;
     if (x > 9) write(x / 10) ;
     putchar(x % 10 + '0') ;
}

void print(int x) {
    cout << x << endl ;
    exit(0) ;
}

void PRINT(string x) {
    cout << x << endl ;
    exit(0) ;
}

void douout(double x){
     printf("%lf\n", x + 0.0000000001) ;
}

struct cow {
	int tim, len, q ;
	bool operator <(const cow &a) const {
		return q > a.q ;
	}
} a[N << 1] ;

bool cmp(cow a, cow b) {
	if (a.tim == b.tim) return a.q < b.q ;
	else return a.tim < b.tim ;
}

int n, ans, cnt ;
priority_queue <cow> q ;

int main() {
	scanf("%d", &n) ;
	for (int i = 1, tim, len; i <= n; i++) {
		scanf("%d%d", &tim, &len) ;
		a[++cnt] = (cow) {tim, len, i} ;
	} 
	sort(a + 1, a + n + 1, cmp) ;
	int nowtim = 0, head = 1 ;
	while (head <= n) {
		while (!q.empty()) {
			cow now = q.top() ; q.pop() ;
			ans = max(ans, nowtim - now.tim) ;
			nowtim += now.len ;
			while (head <= n) {
				if (a[head].tim <= nowtim)  q.push(a[head]), head++ ;
				else break ;
			}
		}
		q.push(a[head]) ;
		if (head < n) head++ ;
		nowtim = q.top().tim ;
 	}
	printf("%d\n", ans) ;
}

T3 Mooyo Mooyo

【题意】

有一个游戏,定义连通块为相邻且颜色相同的元素集。

每一时刻,如果连通块大小>=k,那么该连通块元素消失,然后它上面的元素会全部掉落,然后组成新的连通块。

询问最后的图案长成什么样子。

【分析】

数据范围不大,就变成了一个非常水的题目,直接模拟即可。

直接枚举每个连通块,记录元素,然后如果块大小>=k,则按照记录的元素变成0,然后对于每个元素判断它能够降到哪里就行了。

枚举连通块通过dfs搞,别忘记标记哦。

时间复杂度肯定撑得下,毕竟数据太小了,本人也没有太详细的去算

【代码】

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std ;

#define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++)
#define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define clr(a) memset(a, 0, sizeof(a))
#define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp)
#define ass(a, sum) memset(a, sum, sizeof(a))

#define ls ((rt) << 1)
#define rs ((rt) << 1 | 1)
#define lowbit(x) (x & -x)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define ENDL cout << endl
#define SZ(x) ((int)x.size())

typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> vi ;
typedef pair <int, int> pii ;
typedef pair <ll, ll> pll ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
typedef map <ll, ll> mll ;

const int N = 2010 ;
const double eps = 1e-8 ;
const int iinf = INT_MAX ;
const ll linf = 2e18 ;
const double dinf = 1e30 ;
const int MOD = 1000000007 ;

const int dx[] = {-1, 0, 1, 0} ;
const int dy[] = {0, 1, 0, -1} ;

int n, m = 10, k ;
int a[N][20], vis[N][20] ;
vector <pii> vispl ;
string s ;

void dfs(int x, int y) {
	vis[x][y] = 1 ;
	for (int i = 0; i < 4; i++) {
		int tx = x + dx[i], ty = y + dy[i] ;
		if (tx < 1 || ty < 1 || tx > n || ty > m || a[tx][ty] != a[x][y] || vis[tx][ty]) continue ;
		vispl.pb(mp(tx, ty)) ;
		dfs(tx, ty) ;
	}
}

int main() {
	freopen("mooyomooyo.in", "r", stdin) ;
	freopen("mooyomooyo.out", "w", stdout) ;
	scanf("%d%d", &n, &k) ;
	for (int i = 1; i <= n; i++){
		cin >> s ;
		for (int j = 1; j <= m; j++) a[i][j] = s[j - 1] - '0' ;
	}
	while (1) {
		bool flag = false ;
		clr(vis) ;
		for (int i = 1; i <= n; i++) 
		for (int j = 1; j <= m; j++)
		if (!vis[i][j] && a[i][j]) {
			vispl.pb(mp(i, j)) ;
			dfs(i, j) ;
			if (SZ(vispl) >= k) {
				for (int l = 0; l < SZ(vispl); l++) a[vispl[l].fi][vispl[l].se] = 0 ;
				flag = true ;
			}
			vispl.clear() ; 
		}
		if (!flag) break ;
		for (int i = n - 1; i >= 1; i--)
		for (int j = 1; j <= m; j++)
		if (a[i][j]) {
			int tmp = a[i][j], ii = i ; 
			a[i][j] = 0 ;
			while (a[ii + 1][j] == 0 && ii < n) ii++ ;
			a[ii][j] = tmp ;
		} 
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) cout << a[i][j] ;
		ENDL ;
	}
}

你可能感兴趣的:(分治,二分,比赛,堆,DFS,深度优先搜索,模拟)