longyitongxue's blog

By longyitongxue, history, 7 hours ago, In English

A

Process the characters in order: replace A with Alpha, B with Bravo, and C with Charlie. Remember to read $$$N$$$ (I got a time penalty here).

Code

#include<bits/stdc++.h>
using namespace std;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr),cout.tie(nullptr);
  int n;
  cin>>n;
  string s;
  cin>>s;
  for(char c:s){
    if(c=='A'){
      cout<<"Alpha ";
    }else if(c=='B'){
      cout<<"Bravo ";
    }else cout<<"Charlie ";
  }
  return 0;
}

B

We can use a boolean array $$$\texttt{vis}$$$. For each input $$$A_i$$$, set $$$\texttt{vis}_{A_i}=1$$$. For each subsequent $$$X_i$$$ and $$$Y_i$$$, set $$$\texttt{vis}_{X_i}=0$$$ and $$$\texttt{vis}_{Y_i}=1$$$. Finally, output $$$\sum\limits_{i=1}^{100} \texttt{vis}_i$$$.

Code

#include<bits/stdc++.h>
using namespace std;
int a[int(5e5+5)];
bitset<int(5e5+5)> vis;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr),cout.tie(nullptr);
  int n,m;
  cin>>n>>m;
  for(int i=1;i<=n;i++){
    cin>>a[i];
    vis[a[i]]=1;
  }
  for(int i=1,x,y;i<=m;i++){
    cin>>x>>y;
    vis[x]=0,vis[y]=1;
  }
  for(int i=1;i<=100;i++){
    if(vis[i])cout<<i<<' ';
  }
  return 0;
}

C

If we encounter an o, then we add $$$1$$$ to the answer, and then change this cell and every cell after it at intervals of $$$k$$$ to ..

Code

#include<bits/stdc++.h>
using namespace std;
int a[int(5e5+5)];
bitset<int(5e5+5)> vis;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr),cout.tie(nullptr);
  int n,k,ans=0;
  string s;
  cin>>n>>k>>s;
  for(int i=0;i<n;i++){
    if(s[i]=='.')continue;
    for(int j=i;j<n&&s[j]=='o';j+=k){
      s[j]='.';
    }
    ans++;
  }
  cout<<ans;
  return 0;
}

D

D $$$ \lt $$$ C.

Obviously, this is a 0-1 knapsack problem. For each monster that can be defeated, handle it as in 0-1 knapsack: if defeating it is better, defeat it; otherwise, do not. Finally, enumerate the level after defeating the first $$$N-1$$$ monsters. If that level is sufficient to defeat the $$$N$$$-th monster, update the answer with the current stamina consumption plus the consumption of the $$$N$$$-th monster, and take the minimum.

Code

#include<bits/stdc++.h>
#define int int_fast64_t
using namespace std;
int dp[6005];
int l[6005],p[6005];
int32_t main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
      cin>>l[i]>>p[i];
    }
    memset(dp,0x3f,sizeof dp);
    dp[1]=0;
    for(int i=1;i<=n;i++){
      for(int j=i;j;j--){
        if(j>=l[i]&&dp[j]!=0x3f3f3f3f3f3f3f3f){
          dp[j+1]=min(dp[j+1],dp[j]+p[i]);
        }
      }
    }
    int ans=0x3f3f3f3f3f3f3f3f;
    for(int i=1;i<=n;i++){
      if(i>=l[n]&&dp[i]!=0x3f3f3f3f3f3f3f3f){
        ans=min(ans,dp[i]+p[n]);
      }
    }
    cout<<((ans==0x3f3f3f3f3f3f3f3f)?-1:ans);
    return 0;
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By longyitongxue, history, 5 weeks ago, In English

A

Convert the value of $$$A+B$$$ to a string, and output the length of the string.

Code

B

Determine whether $$$k$$$ belongs to $$$(\min(a_i, a_{i+1}), \max(a_i, a_{i+1}))$$$ or $$$a_{i+1}$$$.

Code

C

Just simulate the process of assigning IDs as required by the problem.

Code

D

After the transformation, we can see that it is Dijkstra. We only need to sort the vertices connected to the current vertex by their indices, then assign the edge connecting the smallest-indexed vertex to $$$0$$$ and the remaining edges to $$$1$$$, and finally run the template.

Code

E

We can solve this efficiently by finding the maximum number of points contained within a half-plane. To completely avoid precision issues and TLE, we use strictly integer operations, specifically long long cross products, instead of atan2 or floating-point math. We first sort the points by their polar angle using a custom comparator based on quadrants and cross products. To handle circular wraparound smoothly, we duplicate the sorted array. Finally, we use a two-pointer sliding window to find the maximum valid window. We move the right pointer forward as long as the cross product between the left point $$$A$$$ and the candidate right point $$$B$$$, calculated as $$$A_x \times B_y - A_y \times B_x$$$, is positive, which guarantees the angular span is strictly less than $$$180^\circ$$$.

Code

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it

By longyitongxue, history, 6 weeks ago, In English

2274B - Prefix

Problem statement: Let $$$s=\sum\limits_{j=1}^{i-1} a_j$$$, and update $$$a_i$$$ to $$$a_i-s$$$, $$$a_i$$$, or $$$a_i+s$$$. Find the largest possible value of $$$\max\limits_{i=1}^n a_i$$$ at the end.

We observe that when $$$s$$$ is positive, it is obvious that we choose to set $$$a_i\gets a_i+s$$$, and when $$$s$$$ is negative, we can choose to set $$$a_i\gets a_i-s$$$ ($$$a_i\gets a_i+(-s)$$$) so that the contribution is positive. Therefore $$$a_i$$$ will ultimately be assigned the value $$$a_i+|s|$$$. To make the contribution larger, we need $$$|s|$$$ to be larger, which means we need $$$s$$$ to be very large or very small (i.e., a large absolute value). At this point we can use two variables $$$l,h$$$ (corresponding to minn and maxn in the code) to denote the smallest and largest prefix sums achievable so far, respectively. Putting this together, we update our answer as $$$\text{ans}\gets \max{\text{ans},|l|,|h|}$$$, and finally output $$$\text{ans}$$$.

So how do we maintain this maximum and minimum? Consider the current position $$$i$$$; the newly updated prefix sum $$$s'$$$ has three possibilities: $$$s\in{s+(a_i-s),s+a_i,s+(a_i+s)}$$$, i.e. $$$s\in{a_i,a_i+s,a_i+2s}$$$. Therefore we update $$$l'\gets \min{a_i,a_i+l,a_i+2l}$$$ and $$$h'\gets \max{a_i,a_i+h,a_i+2h}$$$. Since $$$l$$$, whether positive or negative, can never be smaller than $$$a_i$$$ or $$$a_i+2l$$$, and similarly for $$$h$$$, the two expressions can be rewritten as $$$l'\gets a_i+\min(0,2l)$$$ and $$$h'\gets a_i+\max(0,2h)$$$.

And with that, we are done with this problem.

[AC Code](https://codeforces.me/contest/2274/submission/394678152)

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it