标签:main else can play clr eve back out c++
题意: 给出n个+ - 操作符 求出结束的最小值(当然 过程中不能减到小于0)
求出遍历完成后的总的变化值 和遍历过程中的最小值 这个值就是最小的初始值(显然 题目可以转化为求最小初始值)
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f #define lson l,m,pos<<1 #define rson m+1,r,pos<<1|1 const int N=1000+5; int main() { int n; RI(n); char ch; int ans=0; int minn=0; while(n--) { cin>>ch; if(ch==‘+‘)ans++; else ans--; minn=min(minn,ans); } cout<<-minn+ans; return 0; }
官方题解:
反过来遍历 更加巧妙
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f #define lson l,m,pos<<1 #define rson m+1,r,pos<<1|1 const int N=400000+5; int main() { int n; RI(n); string s; cin>>s; int ans=0; int m=0; repp(i,n-1,0) { if(s[i]==‘+‘) m++; else m--; ans=max(ans,m); } cout<<ans; return 0; }
CF 599 div2 A - A pile of stones
标签:main else can play clr eve back out c++
原文地址:https://www.cnblogs.com/bxd123/p/10856892.html