洛谷 P2034 选择数字

题目链接

把题目理解成每k个连续的数中必须选择一个,然后求最后所有选择的最小和

设置dp[i]为前i个数里面如若去掉i,最小值为多少

很显然dp[i]等于距离i所有距离小于m的最小的那个

这个就可以用单调队列来表示

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
#define int long long
const int INF = 4e18+5;
#define F(i,l,r) for(int i=l;i<=r;i++)
#define R(i,l,r) for(int i=r;i>=l;i--)
#define vv vector
#define fi first
#define se second
#define pii pair<int,int>
typedef long long ll;
//const int mod = 998244353; 
const int mod = 1e9+7; 
//const int M = 1e7+5;
int a[N],b[N],c,d,vis[N],pri[N],ans1[N],ans2[N],p[N],ans,dp[N]; 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m,k,x,y,n0,n2,m0,m2,op1,op2;
string s;
map<pii,int>mp;
map<int,int>mmap; 
//char mp[1005][1005];
//vector<int>v1[N],v2[N];
int lowbit(int x) {
	return x&-x;
}
char xx[N];
bool temp;
int kuai(ll x,int y){
	ll sum=1;
	x%=mod;
	while(y){
		if(y%2){
			y--;
			sum*=x;
			sum%=mod;
		  }
	  x*=x;
	  y/=2;
	  x%=mod;
	}
	return sum;
}
struct node{
	int shu;
	int id;
}jie[N];
bool cmp(node x,node y){
	return x.shu<y.shu;
}
bool check(int x){
	int op=1,sum=0;
	F(i,1,n){
		sum+=a[i];
		if(sum>x){
			sum=a[i];
			op++;
		}
	}
	return op<=m;
}
void solve()
{
	cin>>n>>m;ans=INF; int sum=0; 
	F(i,1,n) cin>>a[i],sum+=a[i];
	int hh=0,tt=0;//模拟的一个单调队列
	F(i,1,n){
		if(i-b[hh]>m+1) hh++;//如若逾越范围
		a[i]+=a[b[hh]];
		while(hh<=tt&&a[b[tt]]>=a[i]) tt--;//这里整个队伍就递增的了 
		b[++tt]=i; 
	}
	F(i,n-m,n) ans=min(ans,a[i]);
	cout<<sum-ans<<endl;
}
signed main() 
{
	ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0); // cin.tie(nullptr);
    int T = 1;                                                                                                                                                                                                                                                                    
    //cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    return 0;
}
洛谷p1427题目是关于小鱼的数字游戏题目描述如下: 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字按照规定处理。游戏规则是:对于给定的一串数字,小鱼要按照从左到右的顺序进行处理,每处理一个数字后,小鱼的得分就增加这个数字本身,然后把这个数字删除。同时,如果小鱼删除的数字左边有与之相同的数字,则小鱼的得分还要增加这个相同数字本身。小鱼需要你帮助它计算出最终得分。 例如,对于输入的数字序列:321321,小鱼的得分计算过程如下: - 处理第一个数字3,得分为3,删除3后剩下的数字序列为21321。 - 处理第二个数字2,得分为3+2=5,删除2后剩下的数字序列为131。 - 处理第三个数字1,得分为5+1=6,删除1后剩下的数字序列为31。 - 处理第四个数字3,得分为6+3=9,删除3后剩下的数字序列为1。 - 处理第五个数字2,得分为9+2=11,删除2后剩下的数字序列为1。 - 处理最后一个数字1,得分为11+1=12,删除1后剩下的数字序列为空。 所以最终得分为12。 你可以通过编写程序来解决这个问题。具体的实现方式可以根据你使用的编程语言来确定。以下是一个可能的实现方式(使用C++语言): ```cpp #include <iostream> #include <string> using namespace std; int main() { string nums; cin >> nums; int score = 0; for (int i = 0; i < nums.length(); i++) { int num = nums[i] - '0'; score += num; // 删除左边与当前数字相同的数字 for (int j = i - 1; j >= 0; j--) { if (nums[j] == nums[i]) { score += num; nums.erase(j, 1); i--; } else { break; } } } cout << score << endl; return 0; } ``` 以上是一个简单的实现,通过遍历输入的数字序列,计算得分并删除相同的数字。最后输出得分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值