Solution to the 42nd issue of CSDN competition

Solution to the 42nd issue of CSDN competition

1. Topic name: Zongmen Grand Competition of Ghost Painting Talisman Gate

Given a sequence A of integers. Find the weight of the subsequence with the largest continuous weight in the integer sequence A.

int main(){
    
    
int n;cin>>n;
int ans=-1e9;
int s =0;
for(int i=1;i<=n;i++){
    
    
int x;cin>>x;
s+=x;
if(s>ans){
    
    
ans=s;
}
if(s<0){
    
    
s=0;
}
}
cout<<ans;
return 0;
}

2. Topic name: Emperor K picks up girls

There are n nodes, and the target node is m. Each node has its own weight a. Select a node with non-zero weight within weight k (including weight K) and the
closest distance to the target node. The distance between node i and node j is abs(ij).

int main(){
    
    
int n,m,k;cin>>n>>m>>k;
rep(i,1,n) cin>>a[i];
int ans =1e9;
for(int i=n;i>=1;i--){
    
    
if(i==m) continue;
if(a[i] && a[i]<=k){
    
    
ans=min(ans,abs(m-i));
}
}
cout<<ans;
return 0;
}

3. Topic name: Shadow Clone

Known string str. The string str contains the characters 'x', 'y'. If the two adjacent characters are different, eliminate the two characters, and eliminate them from the left first. xyyx ->
yx ->

int main(){
    
    
string s;cin>>s;
int n=s.size();
//stack<char>t;
for(int i=0;i<n;i++){
    
    
b[++cnt] = s[i];
if(cnt>=2){
    
    
while(cnt>=2 && b[cnt-1]!=b[cnt]){
    
    
cnt-=2;
}
}
}
for(int i=1;i<=cnt;i++) cout<<b[i];
return 0;
}

4. Topic name: Happy Jin Ming

Jin Ming is very happy today. The new house he bought is about to receive the keys. There is a very spacious room in the new house for his own use. What makes him even more happy is that his mother
said to him yesterday: "You have the final say on what items you need to buy and how to arrange your room, as long as it does not exceed N yuan." Jin Ming started to make a budget early this morning, but
he wanted to buy too many things, which would definitely exceed the N yuan limit set by his mother. Therefore, he set an importance for each item, which is divided into 5 grades:
represented by integers 1-5, and the 5th grade is the most important. He also found the price of each item (all integers) from the Internet.
He hopes to maximize the sum of the products of the price and importance of each item without exceeding N yuan (which can be equal to N yuan) . Suppose the price of the jth item is # #v_[j] # #, the importance is # #w_[j]
#
#, a total of k items are selected, and the numbers are # # j_1,j_2,…,j_k# # , the sum sought is: # #v_[j_1] imes
w_[j_1]+v_[j_2] imes w_[j_2]+ …+v_[j_k] imes w_[j_k] # #
. Please help Jinming design a shopping list that meets the requirements

scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
    
    
scanf("%d%d",&w[i],&p[i]);
}
for(int i=0;i<m;i++)
for(int j=n;j>=w[i];j--)
{
    
    
dp[j]=max(dp[j],dp[j-w[i]]+w[i]*p[i]);
}
cout<<dp[n]<<endl;

Guess you like

Origin blog.csdn.net/weixin_45750972/article/details/130010113