007
题目:
题解:
该复合词中有空格,因此相当于以空格为分割依次输入多个字符串,字符串第一个字符为要大写并提出的字符。
#include <iostream>
using namespace std;
int main()
{
string s;
while(cin>>s) //自动跳过空格
{
if(s[0]>='a' && s[0]<='z') cout<<(char)(s[0]-32);
else cout<<s[0];
}
return 0;
}
008
题目:
题解:
1.暴力解法:两层for循环,先固定左端点i,依次向后找符合条件的右端点j。
2.滑动窗口(同向双指针):
1)进窗口:(区间和)sum+=arr[right]
2)判断:sum>=x
3)更新结果:right-left+1>retlen
4)出窗口:sum-=arr[left]
#include <iostream>
using namespace std;
const int N=1e7+9;
int arr[N];
int n,x;
int main()
{
cin>>n>>x;
int arr[n];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
int left=1,right=1,sum=0;
int retlen=N,retleft=-1,retright=-1;
while(right<=n)
{
sum+=arr[right]; //进窗口
while(sum>=x)
{
if(right-left+1<retlen) //判断相同时,不会更新
{
retlen=right-left+1; //更新区间长度
retleft=left; //记录符合条件的端点
retright=right;
}
sum-=arr[left++]; //出窗口
}
right++;
}
cout<<retleft<<" "<<retright<<endl;
return 0;
}
009
题目:
题解:
模拟+堆+贪心:创建一个只存偶数的堆,每次拿堆顶数出来,除2后,若还是偶数则放回堆中,同时数组中所有数之和sum减去减少的。
#include<iostream>
#include<queue>
using namespace std;
typedef long long LL;
LL n,k;
priority_queue<LL> heap;
int main()
{
cin>>n>>k;
LL sum=0,x;
while(n--)
{
cin>>x;
sum+=x;
if(x%2==0) heap.push(x);
}
while(heap.size() && k--)
{
LL t=heap.top()/2;
sum-=t;
heap.pop();
if(t%2==0)
heap.push(t);
}
cout<<sum<<endl;
return 0;
}