#include #include #include #include #include #include #include #include #include #include #include #include #include #define mid ((l &#43; r) >> 1) #define Lson rt <<1, l , mid #define Rson rt <<1|1, mid &#43; 1, r #define ms(a,al) memset(a,al,sizeof(a)) #define _for(i,a,b) for( int i &#61; (a); i <(b); &#43;&#43;i) #define _rep(i,a,b) for( int i &#61; (a); i <&#61; (b); &#43;&#43;i) #define for_(i,a,b) for( int i &#61; (a); i >&#61; (b); -- i) #define rep_(i,a,b) for( int i &#61; (a); i > (b); -- i) #define lowbit(x) ((-x) & x) #define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define INF 0x3f3f3f3f #define hash Hash #define next Next #define f first #define s second usingnamespace std; constint N &#61;3e5&#43;10; constdouble eps &#61;1e-9; typedeflonglong LL; typedefunsignedlonglong ULL; typedef pair<int,int> PII; int n, len; LL a[N]; inline LL read(){LL s&#61;0,w&#61;1;char ch&#61;getchar();while(ch<&#39;0&#39;||ch>&#39;9&#39;){if(ch&#61;&#61;&#39;-&#39;)w&#61;-1;ch&#61;getchar();}while(ch>&#61;&#39;0&#39;&&ch<&#61;&#39;9&#39;) s&#61;s*10&#43;ch-&#39;0&#39;,ch&#61;getchar();return s*w; } LL last, ans; intmain() {IOS;n &#61;read();_for(i,0,n){LL a;a &#61;read();if(a /2>&#61; last){ans &#43;&#61; last; a -&#61; last *2; last &#61;0;}else ans &#43;&#61; a /2, last -&#61; a /2, a %&#61;2;ans &#43;&#61; a /3;a %&#61;3;last &#43;&#61; a;}cout << ans << endl;return0; }
C. Okabe and Boxes 简单的栈模拟 就是给你一个栈每次都加入一个数或者移除栈顶的元素移除的数的顺序必须按照1~n的顺序出栈&#xff0c;如果不满足你可以重排栈顶元素&#xff0c;问你最小需要重排的次数 我们可以假想成两个栈。如果不满足我们就将栈内的数重排放到假想的栈中&#xff0c;并把实际的栈清空&#xff08;细节要处理好就是每次remove的时候last &#43;&#43;
#include #include #include #include #include #include #include #include #include #include #include #include #include #define mid ((l &#43; r) >> 1) #define Lson rt <<1, l , mid #define Rson rt <<1|1, mid &#43; 1, r #define ms(a,al) memset(a,al,sizeof(a)) #define _for(i,a,b) for( int i &#61; (a); i <(b); &#43;&#43;i) #define _rep(i,a,b) for( int i &#61; (a); i <&#61; (b); &#43;&#43;i) #define for_(i,a,b) for( int i &#61; (a); i >&#61; (b); -- i) #define rep_(i,a,b) for( int i &#61; (a); i > (b); -- i) #define lowbit(x) ((-x) & x) #define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define INF 0x3f3f3f3f #define hash Hash #define next Next #define f first #define s second usingnamespace std; constint N &#61;3e5&#43;10; constdouble eps &#61;1e-9; typedeflonglong LL; typedefunsignedlonglong ULL; typedef pair<int,int> PII; int n, len; LL str[N], top; inline LL read(){LL s&#61;0,w&#61;1;char ch&#61;getchar();while(ch<&#39;0&#39;||ch>&#39;9&#39;){if(ch&#61;&#61;&#39;-&#39;)w&#61;-1;ch&#61;getchar();}while(ch>&#61;&#39;0&#39;&&ch<&#61;&#39;9&#39;) s&#61;s*10&#43;ch-&#39;0&#39;,ch&#61;getchar();return s*w; } LL last &#61;1, ans; intmain() {IOS;cin >> n;_for(i,0,2*n){char op[10];cin >> op;if(*op &#61;&#61;&#39;r&#39;&& top &#61;&#61;0){last &#43;&#43;;continue;}if(*op &#61;&#61;&#39;r&#39;){if(str[top -1]&#61;&#61; last){last &#43;&#43;;top --;}else ans &#43;&#43;, top &#61;0, last &#43;&#43;;}else cin >> str[top], top &#43;&#43;;}cout << ans << endl;return0; }