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;
}
}

被折叠的 条评论
为什么被折叠?



