杭电多校第三场1004、1005、1007、1009

4.Tokitsukaze and Multiple

题目传送门

Tokitsukaze and Multiple

思路

理解题意:将序列分成若干段,看倍速为p的有几段
记录数组a的前缀和并且对p取模,当同一个数字出现两次时,说明该段的数字之和应该是p的倍速,取模为0的情况直接为p的倍速

代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
// #define TDS_ACM_LOCAL
const int N=1e5 + 9;
int n, p, a[N], sum, ans;
map<int,int>mp;
void solve(){
    cin>>n>>p;
    for(int i=1; i<=n; i++) cin>>a[i];
    mp.clear();
    ans=0, sum=0, mp[0]=1;
    for(int i=1; i<=n; i++){
        sum=(sum+a[i])%p;
        if(mp[sum]){ 
            mp.clear();
            mp[0]=1, ans++, sum=0;
        }
        else    mp[sum]=1;
    }
    cout<<ans<<endl;
    return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}

9.Parentheses Matching

题意传送门

Parentheses Matching

题意

由左右括号和乘法符号组成的字符串 ()* ,其中*可以变成(、)、‘ ’,求生成的最短的平衡括号序列中的字典序最小的,左小于右边。

思路

开一个栈存左括号,当输入右括号的且栈不为空的时候出栈,如果栈为空,则考虑最左边的号,将其转变为(
最后判断栈内是否有剩余的的元素(即为左括号),考虑最右边的
号变为)
同时考虑*的有无,判断有无解

代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
// #define TDS_ACM_LOCAL
const int N=1e5 +9;
int a[N];
char p[N], s[N];
int idxa, idxp, idxi, flag, len;
stack<int>st;
void solve(){
    cin>>s;
    idxa=idxp=flag=0;               //idxa为存*号的下标,idxp为最终答案的下标, flag为是否有解
    len=strlen(s);
    memset(a, 0, sizeof(len+2));    //
    while(!st.empty())  st.pop();   //清空栈
    for(int i=0; i<len; i++){
        if(s[i]=='*')   a[++idxa]=i, p[i]=' ';      //将*号存入a,默认*号显示为' '
        else if(s[i]=='(')  st.push(i), p[i]='(';   //左括号进栈,p记录答案
        else if(s[i]==')'){
            if(!st.empty()) st.pop();               //栈不为空(栈中有'(' ),出栈,即为完成一个括号的匹配
            else {
                if(idxp>=idxa)  {flag=1; break;}    //如果)左边无*号,无解
                else    p[a[++idxp]]='(';           //将使用的*号的位置以(的方式存入p
            }
            p[i]=')';                               //将此次的)存入答案p
        }
    }
    while(!st.empty()){                              //栈不为空(即还有多余的(为匹配完?)
        idxi=st.top(); st.pop();
        if(idxp>=idxa || a[idxa]<idxi)  {flag=1; break;}    //判断最右边是否有*号
        p[a[idxa--]]=')';                                   //相应的*号位置存入)
    }

    if(flag)    {cout<<"No solution!"<<endl;  return ;}
    for(int i=0; i<len; i++) if(p[i]!=' ')   cout<<p[i];
    cout<<endl;
    return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}

5.Little W and Contest(并查集)

题目传送门

Little W and Contest

思路

并查集单独写的:Little W and Contest题解

7.Tokitsukaze and Rescue(最短路、DFS)

题目传送门

Tokitsukaze and Rescue

思路

因为太菜了,又单独写了:Tokitsukaze and Rescue题解

猜你喜欢

转载自blog.csdn.net/xmyrzb/article/details/107675577