暑假-S组-第十场总结

· · 个人记录

T1

  • 从 1 到 n 来模拟一遍。

  • 用前缀和数组统计区间和,最后公式求解 (sum[r]-sum[l-1])*(r-l+1)

    
    #include<bits/stdc++.h>
    using namespace std;
    struct ww{
    int a,b;
    }A[300005];
    long long sum[300005];
    int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=n;i++){
    cin>>A[i].a;
    sum[i]=sum[i-1]+A[i].a;
    }
    for(int i=1;i<=n;i++){
    cin>>A[i].b;
    }
    long long maxx=0;
    int l=1,r=0;
    for(int i=1;i<=n;i++){
    if((A[i].a+A[i].b)==(A[i-1].a+A[i-1].b)){
    r=i;
    }else{
    l=i;
    r=0;
    }
    if(r>l){
    maxx=max(maxx,(sum[r]-sum[l-1])*(r-l+1));
    }
    }
    cout<<maxx<<"\n";
    }
    return 0;
    } 
# [T2](https://www.luogu.com.cn/problem/U512656)
>- 这题可以用第一重循环枚举  $r$ ,又因为答案要对 $100$ 取模,所以再一重循环枚举小于 $100$ 的所有答案
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 100;
const int maxn = 2e5 + 7;
int a[maxn];
int sum[maxn];
int maxx = -2e9;
int min_len = 2e9;
int cnt = 0;
int pre[100];
signed main(){
    int n;
    cin >> n;
    for (int i = 0; i < 100; i++){
        pre[i] = -1;
    }
    pre[0] = 0;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];          
        sum[i] = (sum[i] % mod + mod) % mod;
        for (int j = 0; j < 100; j++){
            if (pre[j] != -1){
                if (maxx < ((sum[i] - j) % mod + mod) % mod){
                    maxx = ((sum[i] - j) % mod + mod) % mod;
                }
            }
        }
        if (pre[sum[i]] == -1){
            pre[sum[i]] = i; 
        }
    }
    cout << maxx << endl;
    return 0;
}

T3

  • 利用栈先进后出的特点,来合并字符,如果是小写字母合并,弹出两次插入大写,大写合并直接弹出两次。
    #include<bits/stdc++.h>
    using namespace std;
    string s;
    bool flag;
    char q[1001];
    stack<char>st;
    void ww(int i){
    int ff=0;
    while(!st.empty()){
    flag=0;
    if(ff==0){
    char c=st.top();
    if(c==s[i]){
    st.pop();
    if(c>='a'&&c<='z'){
    st.push(char(c-32));
    }
    ff=1;
    }else{
    st.push(s[i]);
    return ;
    }
    }else{
    char c=st.top();
    st.pop();
    if(st.empty()==0){
    if(c==st.top()){
    st.pop();
    }else{
    st.push(c);
    return;
    }
    }else{
    st.push(c);
    return ;
    }
    }
    }
    return ;
    }
    int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
    cin>>s;
    int n=s.size();
    for(int i=0;i<n;i++){
    flag=1;
    ww(i);
    if(flag){
    st.push(s[i]);
    }
    }
    int j=0;
    while(!st.empty()){
    q[j]=st.top();
    st.pop();
    j++;
    }
    for(int i=j-1;i>=0;i--){
    cout<<q[i];
    } 
    cout<<'\n';
    }
    return 0;
    }

    T4

  • 这题拿60分和容易,对于n为0或b[i]为1的情况,只要 q>1A 国就能赢,对于a[i]为1的情况,只要 max(b[i])<q 才能赢,这样就有60分。
  • 对于正解使用dp。
  • 确定状态: dp[i][j] 表示前面i次攻击,剩余j点魔法可以打掉的王国A的血量值
  • 确定答案: dp[i]][j] 是否大于 100
  • 确定状态转移方程: dp[i][j-m[k]+t]=dp[i-1][j]+h[k]
  • 确定初始值和边界条件 初始值为 0,因为一开始没有任何攻击,答案就是 0。
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e3+7;
    int dp[maxn][maxn];
    int m[maxn];
    int h[maxn];
    int n,t,q;
    int main(){
    int T;
    cin>>T;
    while(T--){
    cin>>n>>t>>q;
    memset(dp,0,sizeof(dp));
    m[0]=0;
    h[0]=1;
    for(int i=1;i<=n;i++){
    cin>>m[i]>>h[i];
    }
    int limi=100/q;
    if(100%q>0){
    limi++;
    }
    int ans=-1;
    int flag=0;
    for(int i=1;i<=limi;i++){
    for(int k=0;k<=n;k++){
    for(int j=0;j<=100;j++){
    int tmp=j-m[k]+t;
    tmp=min(tmp,100);
    if(j>=m[k]){
    dp[i][tmp]=max(dp[i][tmp],dp[i-1][j]+h[k]);
    }
    if(dp[i][tmp]>=100){
    ans=i;
    flag=1;
    break;
    }
    }
    if(flag){
    break;
    }
    }
    if(flag){
    break;
    }
    }
    if(flag==1){
    cout<<ans<<endl;
    }else{
    cout<<"Loser!"<<endl;
    }
    }
    return 0;
    }