t组测试,字符串s的长度 2 * n - 1
输出长度为n的字符串w,满足s[1,n] s[2,n + 1],s[3,n + 2],s[n,2 * n - 1]相似
从第一个字母开始去,隔着一个再取下一个
思维
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; int t,n; string s; int main() { //freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> t; while(t--){ cin >> n >> s; for(int i = 0; i < s.size(); i+=2) cout << s[i]; cout << endl; } return 0; }
t组测试样例。你有p,你的追随者有f,sword和axe数量分别是cnts,cntw;
每个分别消耗s,w;求获得的weapon数量最大值
设
p | f | |
s | s1 | s2 |
w | w1 | w2 |
由题意满足条件
s1 + s2 <= cnts, w1 + w2<= cntw;
s1 * s + w1 * w<= p; s2 * s + w2 * w<= f;
先假设 s < w; s1开始枚举
枚举
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; int t,p,f,cnts,cntw,s,w; int ans,s1,w1,s2,w2; int main() { // freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> t; while(t--){ cin >> p >> f >> cnts >> cntw >> s >> w; ans = 0; if(s > w){swap(s,w);swap(cnts,cntw);} for(s1 = 0; s1 <= cnts && s1 * s <= p; s1 ++){ w1 = min(cntw,(p - s1 * s) / w ); s2 = min(cnts - s1,f / s); w2 = min(cntw - w1,(f - s * s2 ) / w); ans = max(ans,s1 + w1 + s2 + w2); } cout << ans << endl; } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int t,x,flag,len; string S; int w[maxn],s[maxn]; int main() { // freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> t; while(t--) { cin >> S >> x; int i; len = S.size(); for (i = 0; i < len; i++) { s[i + 1] = S[i] - '0';w[i + 1] = 1; } for (i = 1; i <= len; i++) { if(!s[i]){ if(i - x > 0) w[i - x] = 0; if(i + x <= len) w[i + x] = 0; } } flag = 1; for (i = 1; i <= len; i++) { if(s[i]){ if(((i - x > 0 && !w[i - x]) || i - x <= 0) && ((i + x <= len && !w[i + x])|| i + x > len)){ flag = 0; break; } } } if (flag) for (i = 1; i <= len; i++) cout << w[i]; else cout << -1; cout << endl; } return 0; }