HZNU Training 21 for Zhejiang Provincial Competition 2020

C - 展开字符串

HDU - 1274

处理括号优先级:栈保存所有的字符,
遇到 数字+字符,数字拿出来,for一遍,字符填到栈里,
遇到 )一直把栈里元素拿出来,直到 ( 为止,然后把这中间的元素保存下来,因为括号前面有系数
,再for一遍,填到栈里,最后把栈清空,输出

题目数据有问题:没有判数字大于10情况。

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
typedef long long ll;
const ll MOD=998244353;
const int N=1e4+5;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){

    string s,ans;stack<char>stk;

    cin>>s;
    int len=s.size();
    for(int i=0;i<len;i++){

    if(s[i]==‘(‘||s[i]>=‘0‘&&s[i]<=‘9‘)stk.push(s[i]);

    else if(s[i]>=‘a‘&&s[i]<=‘z‘){

    if(stk.top()>=‘0‘&stk.top()<=‘9‘){
    int num=stk.top()-‘0‘;stk.pop();
    while(num--){stk.push(s[i]);}

    }

    else stk.push(s[i]);

    }

    else if(s[i]==‘)‘){
    string tmp;
    while(1){
    char ch=stk.top();stk.pop();
    if(ch==‘(‘)break;
    tmp.insert(tmp.begin(),ch);
    }
    int num=0;
    if(stk.top()>=‘0‘&&stk.top()<=‘9‘)num=stk.top()-‘0‘,stk.pop();
    else num=1;
    while(num--){
      for(int j=0;j<tmp.size();j++){
        stk.push(tmp[j]);
      }
    }
    }
    }
    while(!stk.empty()){
      ans.insert(ans.begin(),stk.top());stk.pop();
    }
    cout<<ans<<endl;

    }

    // system("pause");
    return 0;
}

J - Function and Function

ZOJ - 4070

签到,漏了一个case,为0,没有特判,直接返回0了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=6e3+5;
#define pb push_back
int  Std[]={1,0,0,0,1,0,1,0,2,1};
int  f(int  x){
    int sum=0;
    do{
    int tmp=x%10;
    sum+=Std[tmp];
    x/=10;
    }while(x);
    return sum;
}
int  g(int x,int  k){
    // if(k==0)return x;
    int  cur=x;
    while(k--){
    cur=f(cur);
    if(cur==0&&k%2==0)return 0;
    else if(cur==0&&k%2==1)return 1;
    else if(cur==1&&k%2==0)return 1;
    else if(cur==1&&k%2==1)return 0;
    }
    return cur;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
    int  x,k;
    scanf("%d %d",&x,&k);
    int ans=g(x,k);
    printf("%d\n",ans);
    }
    // system("pause");
    return 0;
}

K - Plants vs. Zombies

ZOJ - 4062

蛋疼题,

最小值最大化,一般都是二分,

二分出一个x,去check 一下 最小值为 x 能不能成立,但要注意memset会tle,而且无论当前状态如何,都要跳一步。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e5+50;
ll v[N],h[N];
int n;ll M;
bool check(ll x){

    for(int i=1;i<=n;i++)h[i]=0;
    ll step=0;

    for(int i=1;i<n;i++){

    if(h[i]<x)step++,h[i]+=v[i];

    if(h[i]<x){

    ll cur=(long long )ceil((x-h[i])*1.0/v[i]);
    step+=2*cur;
    h[i+1]+=cur*v[i+1];
    h[i]+=cur*v[i];

    }

    if(step>M)return 0;
    }
    if(h[n]<x){step++;h[n]+=v[n];}

    if(h[n]<x){
    ll cur=(long long )ceil((x-h[n])*1.0/v[n]);
    step+=2*cur;

    }

    if(step>M)return 0;
    else return 1;

}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
    scanf("%d %lld",&n,&M);
    for(int i=1;i<=n;i++)scanf("%lld",&v[i]);
    ll L=0,R=1e12;
    ll ans=0;

    if(M==0){puts("0");continue;}
    while(L<=R){
    ll  mid=(L+R)/2;
    if(check(mid))ans=mid,L=mid+1;
    else R=mid-1;
    }

    printf("%lld\n",ans);
    }
    // system("pause");
    return 0;
}

// 用栈保存所有的字符,
// 遇到 数字+字符,数字拿出来,for一遍,字符填到栈里,
// 遇到 )一直把栈里元素拿出来,直到 ( 为止,然后把这中间的元素保存下来,因为括号前面有系数
// ,再for一遍,填到栈里,最后把栈清空,输出,

原文地址:https://www.cnblogs.com/littlerita/p/12687200.html

时间: 2024-10-29 05:41:26

HZNU Training 21 for Zhejiang Provincial Competition 2020的相关文章

HZNU Training 2 for Zhejiang Provincial Competition 2020

A - A POJ - 3494 先回忆一下单调栈:解决如下问题:一个点可以向右延伸和向左延伸的最大值,维护一个单增的栈,那么对于栈里的元素a来说,右边的元素都能向右延伸的,左边的元素都不能延伸,如果说一个要进来的元素破坏了单调性,那么我就一直pop最后一个pop的元素实际上就是 这个要入栈的元素能向左延伸的最大长度,那么维护一下最后push进去这个元素的向左延伸的最大值,然后队尾push-1,让他们全出栈: 解法:将每个矩形扩充出来,满足    if(x==1)h[i][j]=h[i-1][j

xtu read problem training 2 B - In 7-bit

In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 371364-bit integer IO format: %lld      Java class name: Main Very often, especially in programming contests, we treat a sequence of non-whitespace cha

2019省赛训练组队赛4.9周二 2017浙江省赛

A - Cooking Competition "Miss Kobayashi's Dragon Maid" is a Japanese manga series written and illustrated by Coolkyoushinja. An anime television series produced by Kyoto Animation aired in Japan between January and April 2017. In episode 8, two

ZOJ 3327 Friend Number(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3327 Friend Number Time Limit: 1 Second      Memory Limit: 32768 KB Given a positive integer x, let P(x) denotes the product of all x's digits. Two integers x and y are friend numbers

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

zjuoj 3606 Lazy Salesgirl

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3606 Lazy Salesgirl Time Limit: 5 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the 

zjuoj 3608 Signal Detection

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3608 Signal Detection Time Limit: 2 Seconds      Memory Limit: 65536 KB Parallelepiped Type Prism Faces 6 parallelograms Edges 12 Vertices 8 Dr. Gale is testing his laser system. He uses a

zjuoj 3605 Find the Marble

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605 Find the Marble Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts

zjuoj 3602 Count the Trees

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seconds      Memory Limit: 65536 KB A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left&