2019summer系列 第13场【Codeforces1130】

http://codeforces.com/contest/1130

A Be Positive


非常水非常签到~

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cnp,cnn,n,x;
int main(){
	scanf("%d",&n);
	for(int i = 0;i <n;i ++){
		scanf("%d",&x);
		if(x>0) cnp ++;
		if(x < 0) cnn ++;
	} 
	if(cnp*2>=n) cout << 1;
	else if(cnn*2>=n) cout << -1;
	else cout<<0;
	return 0;
}

B Two Cakes


非常有趣。
自己瞎想了一个贪心,居然过了。
就是尽可能地使第一个人走得集中吧,第二个人爱莫能助了。。反正肯定要有人走得远的。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,fx[100005],f2[100005],a[200010];
bool vis[100005];
ll ans;
int main(){
	memset(vis,0,sizeof vis);
	scanf("%lld",&n);
	for(int i = 1;i <= n<<1;i ++){
		scanf("%lld",&a[i]);
		if(vis[a[i]]) f2[a[i]] = i;
		else{
			fx[a[i]] = i;
			vis[a[i]] = true;
		}
	}
	fx[0] = 1ll;
	for(int i=1;i <= n;i ++) ans += abs(fx[i]-fx[i-1]);
	f2[0] = 1ll;
	for(int i=1;i <= n;i ++) ans += abs(f2[i]-f2[i-1]);
	cout << ans;
    return 0;
}

C Connect


非常的暴力。。
找到起点所在、终点所在的陆地块块,然后两两算距离,取最小值。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool noneed=false;
char a[55][55];
bool vis[55][55];
int r1,c1,r2,c2,n,mx[]={0,1,0,-1},my[]={1,0,-1,0};
int p1x[2505],p1y[2505],p2x[2505],p2y[2505],tot1 = 0,tot2 = 0;
void color(int x,int y,int* px,int* py,int &tot){
	queue<int> xx,yy;
	memset(vis,0,sizeof vis);
	char cc=a[x][y];
	int i,j;
	xx.push(x);
	yy.push(y);
	vis[x][y] = true;
	while(!xx.empty()){
		i = xx.front();  xx.pop();
		j = yy.front();  yy.pop();
		px[++tot] = i;
		py[tot] = j;
		for(int k = 0;k < 4; k ++){
			if(a[i+mx[k]][j+my[k]] == cc && !vis[i+mx[k]][j+my[k]]){
				vis[i + mx[k]][j + my[k]] = true;
				xx.push(i+mx[k]);
				yy.push(j+my[k]);
			}
		}
	}
}
int main(){
	int ans = 2147483647;
	scanf("%d%d%d%d%d\n",&n,&r1,&c1,&r2,&c2);
	for(int i=1;i <= n;i ++)
		scanf("%s",a[i]+1);
	color(r1,c1,p1x,p1y,tot1);
	color(r2,c2,p2x,p2y,tot2);
	for(int i = 1;i <= tot1;i ++)
		for(int j = 1;j <= tot2;j ++)
			ans = min(ans,(p1x[i]-p2x[j])*(p1x[i]-p2x[j])+(p1y[i]-p2y[j])*(p1y[i]-p2y[j]));
	printf("%d\n",ans);
	return 0;
}

D1 Toy Train (Simplified) && D2 Toy Train


不想写了。。
大概就是找到货物最多的那几个站,假设是有k件货物吧,就至少需要转k-1个整圈。最后一圈的时候,剩下的货物应该是离自己最近的。

E Wrong Answer


我好菜啊,我不行啊。。
看看官方题解。。

Suppose a   0   = − 1 a~0~=−1 a 0 =1 and a   i   ≥ 1 a~i~≥1 a i 1 for each 1 ≤ i &lt; n 1≤i&lt;n 1i<n. Let S = ∑ i = 0 n − 1 a i S=∑_{i=0}^{n−1}a_i S=i=0n1ai.
Assume also that n ≥ 2 n≥2 n2.
It is easy to see that Alice’s algorithm produces ( n − 1 ) ( S + 1 ) (n−1)(S+1) (n1)(S+1) as the answer. Meanwhile, there are two possible correct answers: either nS or ( n − 1 ) ( S + 1 ) (n−1)(S+1) (n1)(S+1), whichever is greater.
Assume further that a 1 ≥ 2 a_1≥2 a12. The correct answer for this array is then n S nS nS. The difference between these two results is n S − ( n − 1 ) ( S + 1 ) = S − n + 1 nS−(n−1)(S+1)=S−n+1 nS(n1)(S+1)=Sn+1.
Now, we can easily create array a greedily so that S − n + 1 = k S−n+1=k Sn+1=k.
The time complexity is O ( n ) O(n) O(n).

让我来做一个勤劳的搬运工吧~


假设对于每个 1 ≤ i &lt; n 1≤i&lt;n 1i<n,有 a 0 = − 1 a_0=−1 a0=1 a i ≥ 1 a_i≥1 ai1 . 定义S为 a i a_i ai的和,即 S = ∑ i = 0 n − 1 a i S=∑_{i=0}^{n−1}a_i S=i=0n1ai
再假设n大于等于2。
容易看出,Alice的算法运行结果为 ( n − 1 ) ( S + 1 ) (n−1)(S+1) (n1)(S+1),但正确答案可能会有两种: n S nS nS ( n − 1 ) ( S + 1 ) (n−1)(S+1) (n1)(S+1)中较大的那个。
进一步假设 a 1 ≥ 2 a_1≥2 a12,数列的正确答案是 n S nS nS,那么正确答案和错误答案之间的差值为 n S − ( n − 1 ) ( S + 1 ) = S − n + 1 nS−(n−1)(S+1)=S−n+1 nS(n1)(S+1)=Sn+1
现在,我们可以贪心地构造一个数列,令 S − n + 1 = k S−n+1=k Sn+1=k
时间复杂度是 O ( n ) O(n) O(n)的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值