Codeforces Round #687 (Div. 2)
A
题意:有一个n*m个牢房的监狱,(r,c)处有一个逃生通道,每一个人一秒可以上下左右移动一格,问最短多少秒所有人可以到逃生通道。
思路:地图四个角一定有一个离(r,c)最远的,取时间最长的那个就是答案。
代码:
#include <bits/stdc++.h>
using namespace std;
#define bug(x) cerr<<#x<<" : "<<x<<endl;
#define x first
#define y second
const int N=2e4+10;
const int mod=1e9+7;
typedef long long ll;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt=1;
cin >> tt;
while(tt--)
{
int n,m,x,y;
cin >> n >> m >> x >> y;
cout << max(x-1,n-x)+max(y-1,m-y) << endl;
}
}B
题意:有n个房子,每个房子有自己初始颜色。画家每次可以画连续的
个房子,可以画成任意颜色,或者不变。问最少画几次可以使所有的房子都变成一样的颜色。
思路:一开始的想法是贪心,把所有房子都变成开始最多颜色的,没看到题目说颜色只有一百种。我们只需要枚举每一种颜色,都刷成这种颜色需要多少天,去最小的的答案,暴力枚举。
代码:
#include <bits/stdc++.h>
using namespace std;
#define bug(x) cerr<<#x<<" : "<<x<<endl;
const int N=2e6+10;
const int mod=1e9+7;
typedef long long ll;
int a[N];
int main(){
int T;
cin>>T;
while(T--){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
int res=1e9;
for(int i=0;i<=100;i++){
queue<int>q;
for(int j=1;j<=n;j++){
if(a[j]!=i) q.push(j);
}
q.push(1e9);
int start=q.front();
q.pop();
int ans=0;
while(!q.empty()){
int x=q.front();
if(x-start<k){
q.pop();
}
else{
start=x;
q.pop();
ans++;
}
}
res=min(res,ans);
}
cout<<res<<endl;
}
}


查看8道真题和解析