算法每日一题——08.17

Codeforces_Round92_Div2

A

#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int a, b, c;
		int input;
		for (int i = 0; i < n; i++)
		{
			cin >> input;
			if (i == 0)
				a = input;
			else if (i == 1)
				b = input;
			else if (i == (n - 1))
				c = input;
		}
		if (a + b <= c)
			cout << 1 << " "<< 2 << " " << n << endl;
		else 
			cout << -1 << endl;
	}
	return 0;	
} 

B

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e4+10;
int t,n,b[maxn],top;
char a[maxn];
int main()
{
	cin >> t;
	while( t-- )
	{
		cin>>(a+1);
		n=strlen(a+1);
		top=0;
		int last=1;
		char w=a[1];
		for(int i=2;i<=n;i++)
		{
			if( a[i]==w )	last++;
			else
			{
				if( a[i-1]=='1' )	b[++top]=last;
				last=1,w=a[i];
			}
		}
		if( a[n]=='1' )	b[++top]=last;
		sort(b+1,b+1+top);
		int ans=0;
		for(int i=top;i>=1;i-=2)	ans+=b[i];
		cout << ans << endl;
	}
}

C

这个没有想出来!!!写的是 n 2 n^2 n2算法,直接超时了。

#include<bits/stdc++.h>

using namespace std;

map<int, int>mp;

int main(){
	int T;
	cin>>T;
	
	while(T--)
	{
		int n;
		string s;
		cin >> n >> s;
		mp.clear(); 
		mp[0] = 0; 
		long long ans = 0, sum = 0;
		for(int i = 0; i < n; i++)
		{
			sum += s[i] - '1'; //这个是前缀和 
			if (sum == 0)
				ans++;
			ans += mp[sum];
			mp[sum]++;
			
		}
		cout << ans << endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值