Codeforces Round #672 (Div. 2)

A
http://codeforces.com/contest/1420/problem/A
他给的那个数加一就是排好序所需最大的操作数,所以判断一下原数列是否严格递减即可。

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
long long T,n,a[500005],b[500005],ans;
bool cmp(int x,int y){return a[x]<a[y];}

int main()
{
	scanf("%lld",&T);
	while (T--)
	{
		scanf("%lld",&n);
		ans=0;
		for (long long i=1; i<=n; i++)
		{
			scanf("%lld",&a[i]);
		}
		for (int i=1; i<n; i++)
			if (a[i]<=a[i+1])
				ans=1;
		
		if (ans) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

B
http://codeforces.com/contest/1420/problem/B
题目给的那个不等式可以等价为两个数二进制最高位1的位置是否相同,那么统计一下 各位 最高位在那的数 的数量,算算组合就行了。

#include <cstdio>
#include <cstring>
using namespace std;
long long T,n,a,k,ans,s[50];

int main()
{
	//freopen("1.txt","r",stdin);
	scanf("%lld",&T);
	while (T--)
	{
		ans=0;
		memset(s,0,sizeof(s));
		scanf("%lld",&n);
		for (int i=1; i<=n; i++)
		{
			scanf("%lld",&a);
			for (k=31; (1ll<<k)>a; k--);
			s[k]++;
		}
		for (int i=0; i<=30; i++)
			ans+=s[i]*(s[i]-1)/2;
		printf("%lld\n",ans);
	}
	return 0;
}

C_1
http://codeforces.com/contest/1420/problem/C1
简单版本,直接普通dp

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
long long T,n,q,ans,a[300005],dp[300005][2];
//dp[i][0]	到i且上一个符号为-的最大值
//dp[i][1]	到i且上一个符号为+的最大值 

int main()
{
	scanf("%lld",&T);
	while (T--)
	{
		scanf("%lld%lld",&n,&q);
		for (int i=1; i<=n; i++)
			scanf("%lld",&a[i]);
		for (int i=1; i<=q; i++)
			scanf("%lld%lld",&L[i],&R[i]);
		for (int i=1; i<=n; i++)
		{
			dp[i][0]=max(dp[i-1][0],dp[i-1][1]-a[i]);
			dp[i][1]=max(dp[i-1][1],dp[i-1][0]+a[i]);
		}
		ans=max(dp[n][1],dp[n][0]);
		printf("%lld\n",ans);
	}
}

C_2
http://codeforces.com/contest/1420/problem/C2
看了题解,这里把题解的方法放上来吧:
先定义两种特别的位置:谷i(a[i]<a[i-1] && a[i]<a[i+1]),峰i(a[i]>a[i-1] && a[i]>a[i+1]),这里将a[0]和a[n+1]定义为负无穷(对这题,因为数都大于0,所以赋值为0就行了)
有这么几个结论:
1.对于一个数列,选奇数个数更优。(不然最后是减一个数,不选它会更大)
2.数列a中,总有偶数个谷,奇数个峰。(因为a[0]和a[n+1]我们人为地定义为了负无穷)
3.那么我们将所有峰和所有谷选上,这个数列就是最优的数列了。(峰的位置全是加,谷的位置全是减,而且是交替相隔的)
就这么打就行了,维护询问也很简单,因为对于每个点,对答案的贡献只与它以及它左边和右边共三个数有关,而它所影响到的也只是这三个数对答案的贡献,支持撤销、跟新功能。具体实现看看代码就清楚了。

#include <cstdio>
int a[300005],T,n,q,l,r,f[300005];
long long ans;

void swap(int &x,int &y)
{
	int tmp=x;
	x=y,y=tmp;
}

void ins(int x)
{
	if (x==0 || x>n || f[x])
		return;
	f[x]=1;
	if (a[x]<a[x+1] && a[x]<a[x-1])
		ans-=a[x];
	if (a[x]>a[x+1] && a[x]>a[x-1])
		ans+=a[x];
}

void del(int x)
{
	if (x==0 || x>n || !f[x])
		return;
	f[x]=0;
	if (a[x]<a[x+1] && a[x]<a[x-1])
		ans+=a[x];
	if (a[x]>a[x+1] && a[x]>a[x-1])
		ans-=a[x];
}

int main()
{
	//freopen("1.txt","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&q);
		for (int i=1; i<=n; i++)
			scanf("%d",&a[i]),f[i]=0;
		a[0]=a[n+1]=ans=0;
		for (int i=1; i<=n; i++)
			ins(i);
		printf("%lld\n",ans);
		while (q--)
		{
			scanf("%d%d",&l,&r);
			del(l-1); del(l); del(l+1);
			del(r-1); del(r); del(r+1);
			swap(a[l],a[r]);
			ins(l-1); ins(l); ins(l+1);
			ins(r-1); ins(r); ins(r+1);
			printf("%lld\n",ans);
		}
	}
}

D
http://codeforces.com/contest/1420/problem/D
排序加优先队列

E
http://codeforces.com/contest/1420/problem/E

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值