Problem - 1400C - Codeforces
思维不清晰的时候一定不能做这种考验细节考验码力的题目,不然会死的很惨;其实这题目只需要根据题意来模拟就行了,但细节没处理好,代码写的很麻烦,wa到崩
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll inf=1e18;
ll t,x;
string s;
int main(){
//freopen("in.txt","r",stdin);
scanf("%lld",&t);
while(t--){
cin>>s;cin>>x;
ll n=s.size();
s=" "+s;
string w=s;
bool flag=0;
for(int i=0;i<=n;i++) w[i]='#';
for(int i=1;i<=n;i++){
flag=0;
if(s[i]=='1'){
if(i-x>0){
if(w[i-x]=='1'||w[i-x]=='#'){flag=1;w[i-x]='1';}
}
if(i+x<=n&&flag==0){
if(w[i+x]=='1'||w[i+x]=='#'){flag=1;w[i+x]='1';}
}
}
else{
if(i-x>0){
if(w[i-x]=='0'||w[i-x]=='#')flag=1,w[i-x]='0';
else flag=0;
if(!flag) break;
//cout<<flag<<" "<<i<<" cc "<<w[i+x]<<" "<<i+x<<endl;
}
if(i+x<=n){
if(w[i+x]=='0'||w[i+x]=='#') flag=1,w[i+x]='0';
else flag=0;
//cout<<flag<<" "<<i<<" cc "<<w[i+x]<<" "<<i+x<<endl;
}
if(i-x<1&&i+x>n) flag=1;
}
//cout<<flag<<" "<<i<<" "<<w[i+x]<<endl;
if(!flag) break;
}
if(!flag){
printf("-1\n");continue;
}
for(int i=1;i<=n;i++){
if(w[i]=='#') w[i]='1';
cout<<w[i];
}
printf("\n");
}
return 0;
}
1526C1 - Potions (Easy Version) 反悔贪心||dp
用dp做就是dp[i][j],前i瓶用了j瓶,状态和方程都搞出来了但是有很多细节都没处理好导致一错再错,具体细节看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll inf=1e18;
ll n,a[2005],dp[2005][2005];
int main(){
//freopen("in.txt","r",stdin);
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++) dp[i][j]=-inf;
for(int i=0;i<=n;i++) dp[i][0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
dp[i][j]=max(dp[i-1][j],dp[i][j]);
if(dp[i-1][j-1]!=-inf&&dp[i-1][j-1]+a[i]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-1]+a[i]);
}
}
for(int i=n;i>=0;i--){
if(dp[n][i]>=0){
printf("%d\n",i);
// cout<<dp[n][i]<<" "<<i<<endl;
break;
}
}
return 0;
}
还有就是用优先队列做,而且更快;如果a[i]本来就是>=0,那么就sum就加上他,ans++;如果a[i]<0,但sum+a[i]>=0,那就先加上,但是把a[i]的绝对值放进队列,如果sum+a[i]<0而且队列不为空,那就看看队列的第一个元素是否大于a[i]的绝对值,若大于那就弹出队首元素,放进a[i],也就是用a[i]来替换掉队首元素,小的换大的总是最优的;
(7条消息) C1. Potions (Easy Version)(cf) 优先队列、后悔贪心_璇玑你没有心的博客-CSDN博客
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll inf=1e18;
ll n,a[2005];
priority_queue<ll>q;
int main(){
//freopen("in.txt","r",stdin);
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
ll sum=0,ans=0;
for(int i=1;i<=n;i++){
if(a[i]>=0) sum+=a[i],ans++;
else{
if(sum+a[i]>=0) sum+=a[i],q.push(abs(a[i])),ans++;
else if(sum+a[i]<0){
if(!q.empty()&&q.top()>abs(a[i])){
sum=sum+q.top()+a[i];
q.pop();q.push(abs(a[i]));
}
}
}
}
printf("%lld\n",ans);
return 0;
}
优美!最长上升子序列 - 题目 - Daimayuan Online Judge
直接dp的话会T到飞起,自己写的埃氏筛也T的飞起,知道看到大佬的博客如沐春风,把比较的顺序到一下就可以直接枚举了,我这个废物;
(倍数遍历) 代码源每日一题 Div2 优美!最长上升子序列 - 知乎 (zhihu.com)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
const ll inf=1e18;
ll t,n,a[1000006],dp[1000006];
int main(){
//freopen("in.txt","r",stdin);
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]),dp[i]=1;
ll maxx=1;
for(int i=1;i<=n;i++)
for(int j=2*i;j<=n;j+=i){//和一般写的顺序到一下就可以正着枚举了
if(a[j]>a[i]) dp[j]=max(dp[j],dp[i]+1);
maxx=max(maxx,dp[j]);
}
printf("%lld\n",maxx);
}
return 0;
}