Codeforces Round #672 (Div. 2) A - C1题解

[Codeforces Round #672 (Div. 2) A - C1 ]

题目链接#

A. Cubes Sorting

思路:

“ If Wheatley needs more than \(\frac{n \cdot (n-1)}{2}-1\) exchange operations, he won't do this boring work.”

例如:5,4,3,2,1

我们需要移动4+3+2+1=10次,也就是\(\frac{n \cdot (n-1)}{2}\)次,所以当整个数列是严格的单调递减的,才刚好不符合

只要枚举出一对相邻的数是\(a[i-1]<=a[i]\)就可以了

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
const int max_n=5e4;
ll a[max_n];
int main(){
	ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	ll T;
	cin>>T;
	while(T--)
	{
		ll n,ans=0;
		cin>>n;
		for(ll i=0;i<n;i++)
		{
				cin>>a[i]; 
		}
		bool pd=0;
		for(ll i=1;i<n;i++)
			if (a[i-1]<=a[i])
				{
					pd=1;
					break;
				}
		if (pd)
			cout<<"YES\n";
		else
			cout<<"NO\n";
	}
	return 0;
}

B. Rock and Lever

思路:

先暴力枚举一下吧。发现47与815是不符合的

再根据位运算,合理推测是二进制下相同位数才能满足。

example:

\(x=7,y=8\)

\(0111\) & $ 1000$=\(0\) \(<=\) \(0111\)^ $ 1000$=\(1111\)

反思一下:昨晚脑力不足,室友打游戏情况下好不容易找出的规律,结果开了个int的ans数组...第二个点就没过去。

第二天早早就醒了,突然知道哪错了,越想越气,上机!

改了下提交果然A掉了

\(QAQ\)

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll ans[10000];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll t;
	cin>>t;
	while(t--)
	{
		ll n,maxn=-INF,answer=0;
		memset(ans,0,sizeof(ans));
		cin>>n;
		ll k=1;
		for(ll i=0; i<n; i++)
		{
			ll x,k=0;
			cin>>x;
			while(x)
			{
				x/=2;
				k++;
			}
			ans[k]++;
			if (k>maxn)
				maxn=k;
		}
		for(ll i=0;i<=maxn;i++)
		{
			if (ans[i]>1)
				answer+=ans[i]*(ans[i]-1)/2;

		}
		cout<<answer<<"\n";
	}
	return 0;
}

C. Pokémon Army (easy version)

思路:Pokémon?还是挺有趣的

简而言之,就是在一串数组里找一对一对的数字,满足\((a_{b_i}-a_{b_j}>0)\)

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
const ll max_n=3e5+5;
ll a[max_n];
int main(){
	ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	ll t;
	cin>>t;
	while(t--)
	{
		ll n,q,ans=0;
		cin>>n>>q;
		for(ll i=1;i<=n;i++)
			cin>>a[i];
		ll l=0,r=0;	 //l记录大的值,r记录小的值
		for(ll i=1;i<=n;i++)
		{
			if (i==1)
				l=a[i];
			else
			{
				if (a[i-1]<a[i])
				{
					if (l&&r)
						ans+=l-r;
					l=a[i];
					r=0;
				}
				else
					r=a[i];
			}
		}
		ans+=l;
		cout<<ans<<"\n";
		
	}
	return 0;
}


posted @ 2020-09-25 15:42  william4s  阅读(163)  评论(0)    收藏  举报