第 285 场周赛
2210. 统计数组中峰和谷的数量
int countHillValley(vector<int>& nums) {
vector<int> v; int n = 0;
for(int i=0;i<nums.size();i++){
if(n==0) v.push_back(nums[i]),n++;
else if(v[n-1]!=nums[i]) v.push_back(nums[i]),n++;
}
int ans = 0;
for(int i=1;i<n-1;i++) {
if(v[i]>v[i-1]&&v[i]>v[i+1]) ans++;
if(v[i]<v[i-1]&&v[i]<v[i+1]) ans++;
}
return ans;
}
2211. 统计道路上的碰撞次数
int countCollisions(string directions) {
int n = directions.size(),i=0,ans=0;
while(i<n&&directions[i]=='L') i++;
stack<char> s;
while(i<n){
if(s.size()==0) s.push(directions[i]);
else{
char u = s.top();
if(u=='R'&&directions[i]=='L') {
ans+=2;s.pop();
while(!s.empty()&&s.top()=='R'){
ans+=1;
s.pop();
}
s.push('S');
}
else if(u=='S'&&directions[i]=='L') {
ans+=1;s.pop();
s.push('S');
}
else if(u=='R'&&directions[i]=='S'){
ans+=1;s.pop();
while(!s.empty()&&s.top()=='R'){
ans+=1;
s.pop();
}
s.push('S');
}
else s.push(directions[i]);//R or S
// cout<<i<<" "<<ans<<endl;
}
i++;
}
return ans;
}
2212. 射箭比赛中的最大得分
<int> ans,tmp;
int val;
void dfs(int ix,int numArrows,int count,vector<int>& aliceArrows){
if(ix==aliceArrows.size()){
if(count>val) {
val = count;
tmp[11]+=numArrows;
ans = tmp;
}
return;
}
if(numArrows>aliceArrows[ix]) {
tmp[ix] = aliceArrows[ix]+1;
dfs(ix+1,numArrows-aliceArrows[ix]-1,count+ix,aliceArrows);
tmp[ix] = 0;
}
dfs(ix+1,numArrows,count,aliceArrows);
}
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
ans.resize(aliceArrows.size());
tmp.resize(aliceArrows.size());
dfs(0,numArrows,0,aliceArrows);
return ans;
}
2213. 由单个字符重复的最长子字符串
当时没想到
第 288 场周赛
6037. 按奇偶性交换后的最大数字
int largestInteger(int num) {
vector<int> a;
vector<int> b;
string s = to_string(num);
for(int i=0;i<s.size();i++) {
int t = s[i]-'0';
if(t&1) a.push_back(t);
else b.push_back(t);
}
string ans = "";
sort(a.begin(),a.end(),greater<int>());
sort(b.begin(),b.end(),greater<int>());
int l = 0, r=0;
for(int i=0;i<s.size();i++) {
int t = s[i]-'0';
if(t&1) {
ans+=to_string(a[l]);
l++;
}
else {
ans+=to_string(b[r]);
r++;
}
}
return std::stoi(ans);
}
6038. 向表达式添加括号后的最小结果
string minimizeResult(string expression) {
string a="",b="";
int flag = 0;
for(int i=0;i<expression.size();i++) {
if(expression[i]=='+') flag=1;
else if(!flag) a+=expression[i];
else b+=expression[i];
}
// cout<<a<<" "<<b<<endl;
int ans = INT_MAX;
string res = "";
for(int i=0;i<a.size();i++) {
for(int j=0; j<b.size(); j++) {
int al,ar,bl,br;
if(i==0){
al = 1;ar = std::stoi(a);
}
else {
al = std::stoi(a.substr(0,i));
ar = std::stoi(a.substr(i,a.size()-i));
}
if(j==b.size()-1) {
br = 1;
bl = std::stoi(b);
}
else {
bl = std::stoi(b.substr(0,j+1));
br = std::stoi(b.substr(j+1,b.size()-j+1));
}
// cout<<al<<" "<<ar<<" "<<bl<<" "<<br<<endl;
int tmp = al*(ar+bl)*br;
if(tmp<ans){
ans = tmp;
res = "";
if(i!=0) res+=to_string(al);
res+="("+to_string(ar);
res+="+"+to_string(bl)+")";
if(j!=b.size()-1) res+=to_string(br);
}
}
}
return res;
}
6039. K 次增加后的最大乘积
int maximumProduct(vector<int>& nums, int k) {
int n = nums.size();
sort(nums.begin(),nums.end());
int cost = 0;
for(int i=0; i<n; i++ ){
if(i==0) continue;
else if(cost + i*(nums[i]-nums[i-1]) < k){
cost += i*(nums[i]-nums[i-1]);
if(i==n-1){
for(int j=0;j<i;j++) nums[j] = nums[i];
}
}
else {
int x = (k - cost)/i;
int m = (k-cost)%i;
int val = nums[i-1];
for(int j=0;j<i;j++){
if(m>0) nums[j] = val + x + 1,m--;
else nums[j] = val + x;
}
cost = k;
break;
}
}
if(cost<k) {
int x = (k-cost)/n;
int m = (k-cost)%n;
for(int i=0;i<n;i++){
if(m>0) nums[i] = nums[i] + x + 1,m--;
else nums[i] = nums[i] + x;
}
cost = k;
}
long long ans = 1;
int mod = 1e9+7;
for(int i=0;i<n;i++){
ans=(ans*(long long)nums[i])%mod;
}
return (int)ans;
}
6040. 花园的最大总美丽值
没想到 = =