首页 > 其他分享 >CF1857B Maximum Rounding

CF1857B Maximum Rounding

时间:2023-10-07 21:44:30浏览次数:30  
标签:vis Rounding CF1857B Maximum int printf include 进位 define

题目大意

给定一个自然数 \(n\),可以对任意一位进行四舍五入,可以进行任意次,求能得到的最大数。

\(n\) 的长度不超过 \(2\times 10^5\),没有前导零。

solution

首先,选择四舍五入的数一定 \(\ge 5\),不然对答案没有贡献。

其次,高位的数可能会受到低位的进位,这启发我们从低位向高位考虑。

由于 \(n\) 很大,所以选择用数组存储。从低位向高位扫一遍,如果当前数加上进位 \(\ge 5\),那么就对这一位打上标记,并让进位加一。最后如果有进位,先将进位输出,接着从高位扫向低位,若此位有标记,那么从当前位向后应该全输出 \(0\),否则输出原数接受进位后的结果。

code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<utility>
#include<vector>
#include<queue>
#include<bitset>
#define FOR(i,a,b) for(register int i=a;i<=b;i++)
#define ROF(i,a,b) for(register int i=a;i>=b;i--)
#define mp(a,b) make_pair(a,b)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int INF=0x3f3f3f3f;
int n,m,k;
int A[N];
char s[N];
bitset<N>vis;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--){
		vis.reset();//赋值为0
		scanf("%s",s+1);
		n=strlen(s+1);
		int x=0;
		ROF(i,n,1){
			int t=s[i]-48+x;
			A[i]=t%10;
			x=t/10;
			t=A[i];
			if(t>=5){
				x++;
				vis[i]=1;
			}
		}
		if(x>0) printf("%d",x);
		int f=0;
		FOR(i,1,n) 
		{
			if(vis[i]||f){
				f=1;
				printf("0");
			}
			else printf("%d",A[i]);
		}
		puts("");
	}
	return 0;
}

标签:vis,Rounding,CF1857B,Maximum,int,printf,include,进位,define
From: https://www.cnblogs.com/LHLeisus/p/17747564.html

相关文章

  • 【线段树合并】CF1805E There Should Be a Lot of Maximums 题解
    CF1805E待补:有另解看到维护树上问题,可以想到线段树合并。但直接维护显然不行,要一点技巧。发现\(val\)的出现次数\(cnt_{val}\)如果\(\ge3\),那么一定是一个候选项,若\(cnt_{val}=1\),那么一定不能作为候选项。于是可以用权值线段树维护对于\(val\)有\(cnt_{val}=......
  • [CF762D] Maximum path 题解
    [CF762D]Maximumpath题解想法首先考虑问题的弱化版,如果不能往左走,能取到的最大值是多少。这个问题可以用一个显然的DP解决,\(f_{i,j}\)表示走到第\(i\)列,第\(j\)行,并且不会再访问这一列其它的方格,能取到的最大值。转移可以从三个方向考虑,以\(f_{i,1}\)为例,黑色是当......
  • nginx访问报错“maximum number of descriptors supported by select() is 1024 while
    1、问题背景 项目:一个人力的系统,主要用于考勤打卡环境:windowsservernginx版本:1.22 问题说明:当早上访问人数增加的时候,就会出现nginx的异常nginx的后台报错日志:maximumnumberofdescriptorssupportedbyselect()is1024whileconnectingtoupstream  ......
  • [CF1810G] The Maximum Prefix
    题目描述You'regoingtogenerateanarray$a$withalengthofatmost$n$,whereeach$a_{i}$equalseither$1$or$-1$.Yougeneratethisarrayinthefollowingway.First,youchoosesomeinteger$k$($1\lek\len$),whichdecid......
  • [LeetCode] 1353. Maximum Number of Events That Can Be Attended 最多可以参加的会
    Youaregivenanarrayof events where events[i]=[startDayi,endDayi].Everyevent i startsat startDayi andendsat endDayi.Youcanattendanevent i atanyday d where startTimei<=d<=endTimei.Youcanonlyattendoneeventatanytime ......
  • hadoop中mapred.tasktracker.map.tasks.maximum的设置
    目前,我们邮件的一部分log已经迁移到Hadoop集群上并由Hive来执行相关的查询hadoop中默认的mapred.tasktracker.map.tasks.maximum设置是2也即:每一个tasktracker同时运行的map任务数为2照此默认设置,查询80天某用户的操作日志,耗时5mins,45sec经过测试,发现将mapred.tasktracker.map.ta......
  • 洛谷 AT_maximum_cup_2018_a フィギュアスケート界の貴公子埼大選手 の 题解
    这道题是一道水题,所以你的代码很可能与我相似或相同,如果你的代码出现了问题,你很可能在我的题解里找出答案。这道题大概思路是一旦$10$秒后运动员会接触到毛绒玩具,那么就加上在这个点上毛绒玩具的数量。但是!这道题有一道巨坑的点!由于这道题比较远古,所以说你即使是正解,你也要在......
  • All Pairs Maximum Flow题解
    前置知识:1.P3376【模板】网络最大流2.P4897【模板】最小割树(Gomory-HuTree)Ebola有一句很著名的话如果你乱搞过了我请你抽烟那么这道题肯定不能普通的dinic直接水过去,不然就不是紫题了,那么直接祭出最小割树,复杂度\(O(Tn^3m)\),但是因为dinic跑不满,所以是可以过的。......
  • Caused by: java.sql.SQLSyntaxErrorException: ORA-00923: 未找到要求的 FROM 关键字
    最终是,查询条件,入参为null,所导致。JDBCgetParameterTypecallfailed-usingfallbackmethodinsteadRA-00923:FROMkeywordnotfoundwhereexpected 进一步,这个错误,在job执行的时候,会导致,oracle游标不够ORA-01000maximumopencursorsexceeded   参考: ......
  • Maximum Diameter 题解
    MaximumDiameter题目大意定义长度为\(n\)的序列\(a\)的权值为:所有的\(n\)个点的第\(i\)个点的度数为\(a_i\)的树的直径最大值,如果不存在这样的树,其权值为\(0\)。给定\(n\),求所有长度为\(n\)的序列的权值和。思路分析\(n\)个点的树的边数为\(n-1\),总度数......