目录
A
第一个遇到连续两个荆棘的地方就不能再赢金币了。
所以统计连续两个荆棘之前的所有金币
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve() {
int n;
cin>>n;
string str;
cin>>str;
int pos=n;
rep(i,0,n-2){
if(str[i]=='*'&&str[i+1]=='*'){
pos=i;
break;
}
}
int ans=0;
if(pos!=n){
rep(i,0,pos-1){
if(str[i]=='@'){
ans++;
}
}
}else{
rep(i,0,n-1){
if(str[i]=='@'){
ans++;
}
}
}
cout<<ans<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
B
最后需要满足\(a[i]>a[i-1]\)
\(a[i]\)只能增加自身的倍数
只需要计算\(a[i]\)最终会变为自己的多少倍会严格大于\(a[i-1]\),当\(a[i-1]\)是\(a[i]\)的倍数的时候,必须再\(+1\)才能保证严格大于。
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve() {
int n;
cin>>n;
vector<int>a(n+1);
rep(i,1,n){
cin>>a[i];
}
rep(i,1,n){
if(a[i]<=a[i-1]){
int cnt=a[i-1]/a[i]+1;
a[i]*=cnt;
}
}
cout<<a[n]<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
C
正着算会溢出
考虑倒着算,也就是先算最后一个留下的,然后边乘边模
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve() {
int n,m;
cin>>n>>m;
struct node{
int val,pos;
bool operator<(const node&t)const{
return pos>t.pos;
}
};
vector<node>a(n+1);
rep(i,1,n){
cin>>a[i].val;
}
string str;
cin>>str;
int cnt=1;
int l=1,r=n;
rep(i,0,str.size()-1){
if(str[i]=='L'){
a[l].pos=cnt++;
l++;
}else{
a[r].pos=cnt++;
r--;
}
}
sort(a.begin()+1,a.end());
// rep(i,1,n){
// cout<<a[i].val<<' '<<a[i].pos<<endl;
// }
vector<int>ans;
int mul=1;
rep(i,1,n){
int k=a[i].val*mul%m;
ans.pb(k);
mul=k;
}
fep(i,ans.size()-1,0){
cout<<ans[i]<<' ';
}
cout<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
D
模拟题.
同花色的两两配对,是奇数的话,再用一张王去配对
判断有没有解是通过奇数牌的张数和王的张数判断。
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve() {
int n;
cin>>n;
//0:C,1:D,2:H,3:S
set<int>s[4];
char c;
cin>>c;
map<char,int>mp;
mp['C']=0;
mp['D']=1;
mp['H']=2;
mp['S']=3;
map<int,char>pm;
pm[0]='C';
pm[1]='D';
pm[2]='H';
pm[3]='S';
rep(i,1,2*n){
string str;
cin>>str;
char k=str[1];
int num=str[0]-'0';
if(k=='C'){
s[0].insert(num);
}else if(k=='D'){
s[1].insert(num);
}else if(k=='H'){
s[2].insert(num);
}else{
s[3].insert(num);
}
}
int kk=0;
rep(i,0,3){
if(mp[c]==i){
continue;
}
kk+=s[i].size()%2;
}
if(kk>s[mp[c]].size()){
cout<<"IMPOSSIBLE"<<endl;
return;
}
rep(i,0,3){
if(mp[c]==i){
continue;
}
int ji=s[i].size()%2;
if(ji==1){
cout<<*s[i].begin()<<pm[i];
s[i].erase(s[i].begin());
cout<<' ';
cout<<*s[mp[c]].begin()<<pm[mp[c]];
s[mp[c]].erase(s[mp[c]].begin());
cout<<endl;
while(s[i].size()>=2){
cout<<*s[i].begin()<<pm[i];
s[i].erase(s[i].begin());
cout<<' ';
cout<<*s[i].begin()<<pm[i];
s[i].erase(s[i].begin());
cout<<endl;
}
}else{
while(s[i].size()>=2){
cout<<*s[i].begin()<<pm[i];
s[i].erase(s[i].begin());
cout<<' ';
cout<<*s[i].begin()<<pm[i];
s[i].erase(s[i].begin());
cout<<endl;
}
}
}
while(s[mp[c]].size()>=2){
cout<<*s[mp[c]].begin()<<pm[mp[c]]<<' ';
s[mp[c]].erase(s[mp[c]].begin());
cout<<*s[mp[c]].begin()<<pm[mp[c]]<<' ';
s[mp[c]].erase(s[mp[c]].begin());
cout<<endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
E
数学题。
考虑每一位对答案的贡献
\(12345\)
个位会对答案贡献\(12345\)
十位会对答案贡献\(1234\)
百位会对答案贡献\(123\)
千位会对答案贡献\(12\)
万位会对答案贡献\(1\)
然后会发现规律,将上面列竖式相加
每一位的结果就是当前位的数和前面所有数的和,然后倒着处理一下进位。
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve() {
int n;
cin>>n;
string str;
cin>>str;
vector<int>s(n+1,0);
rep(i,1,n){
s[i]=s[i-1]+str[i-1]-'0';
}
fep(i,n,1){
s[i-1]+=s[i]/10;
s[i]%=10;
}
bool flag=false;
rep(i,0,n){
if(s[i]||flag){
cout<<s[i];
flag=true;
}
}
cout<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("1.in", "r", stdin);
cout.tie(0);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
F
\(dp[i][0|1]\):表示在第i个点不喂或喂猫,合法的能为猫的最大值
转移
\(nxt[i]\):在i处喂猫的话能喂猫的最有边的位置
\(cf[i]\):在i处喂猫,所能喂猫的所有数量
\(f[i][0]=max(f[i-1][0],f[i-1][1]);\)
\(f[i][1]=max(f[nxt[i]-1][1],f[nxt[i]-1][0])+cf[i];\)
\(cf[i]\)可以用差分处理一下
\(nxt[i]\)可以倒序枚举用\(nxt[i-1]\)去更新\(nxt[i]\)
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
void solve() {
int n,m;
cin>>n>>m;
vector<int>nxt(n+2),cf(n+2);
rep(i,1,n){
nxt[i]=i;
}
rep(i,1,m){
int l,r;
cin>>l>>r;
cf[l]++;
cf[r+1]--;
nxt[r]=min(nxt[r],l);
}
//处理在每个点喂能为多少猫
rep(i,1,n){
cf[i]+=cf[i-1];
}
//处理转移的位置
fep(i,n-1,1){
nxt[i]=min(nxt[i],nxt[i+1]);
}
//dp
vector<vector<int>>f(n+1,vector<int>(2));
rep(i,1,n){
f[i][0]=max(f[i-1][0],f[i-1][1]);
f[i][1]=max(f[nxt[i]-1][1],f[nxt[i]-1][0])+cf[i];
}
cout<<max({f[n][0],f[n][1]})<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}
标签:int,rep,Codeforces,long,cin,str,Div,927,define
From: https://www.cnblogs.com/cxy8/p/18021840