首页 > 其他分享 >Codeforces Round 829 (Div. 1)A1. Make Nonzero Sum (easy version)(思维找规律)

Codeforces Round 829 (Div. 1)A1. Make Nonzero Sum (easy version)(思维找规律)

时间:2023-11-29 20:13:58浏览次数:43  
标签:Nonzero int Sum Codeforces long Make define

先考虑无解的情况:当n为奇数时无解
相邻的两个元素一定可以变成0

\[a[i] != a[i + 1]时, 分成[i, i], 和[i + 1, i + 1] \]

\[a[i] = a[i + 1]时, 分成[i, i + 1] \]

这两种情况对答案的贡献都是0,当n为奇数时我们总会有一个没办法凑成0.

#include <bits/stdc++.h> 
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define db double
#define ull unsigned long long
#define endl '\n'

using namespace std;
const int N = 1e6 + 10;
int t, a[N];

void solve()
{
	int res = 0, n;
	cin >> n;
	vector<PII>ans;
	for(int i = 1; i <= n; ++ i)	cin >> a[i];
	if(n & 1 == 1)
	{
		cout << "-1" << endl;
		return;
	}
	else
	{
		for(int i = 1; i <= n - 1; i += 2)
		{
			if(a[i] == a[i + 1])	ans.push_back({i, i + 1}), ++ res;
			else
			{
				res += 2;
				ans.push_back({i, i});
				ans.push_back({i + 1, i + 1});
			}
		}
		cout << res << endl;
		for(auto x : ans)	cout << x.first << ' ' << x.second << endl;
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//	freopen("1.in", "r", stdin);
	cin >> t;
	while(t --)	
	solve(); 
	return 0;
}

标签:Nonzero,int,Sum,Codeforces,long,Make,define
From: https://www.cnblogs.com/cxy8/p/17865735.html

相关文章

  • Android踩坑小记-在onResume中申请权限
    Android踩坑小记-在onResume中申请权限最近遇见一个问题,在onResume中申请权限,比如申请定位权限,如下所示:@OverrideprotectedvoidonResume(){super.onResume();requestPermission();}@TargetApi(Build.VERSION_CODES.M)privatevoidr......
  • Problem: A. Tricky Sum
    A:做法:数据比较小,用求和公式(n+1)*n/2,减去所有2的幂即可点击查看代码//Problem:A.TrickySum//Contest:Codeforces-EducationalCodeforcesRound1//URL:https://codeforces.com/contest/598/problem/A//MemoryLimit:256MB//TimeLimit:1000ms//Aut......
  • C++完美开发环境vscode+clangd+lldb+xmake(已亲测有效,使用体验秒杀vscode官方C++插件)
    vscode下载并安装1.下载vscode官网下载网速不好的可以在这里自取:vscode蓝奏云下载密码:hnp42.安装选择我同意可以选择不创建开始菜单这里勾选了最后一个选择(添加到系统环境变量中,如果没有勾选这个选项,则需要手动添加),其他的按自己情况勾选,建议全部勾选方便使用安装......
  • cmake Ninja 集成试用
    核心比较简单就是添加Ninja的依赖工具,然后再构建的时候指定生成器为Ninja使用安装ninja我使用的mac系统 brewinstallninja生成前提是已经有了一个CMakeLists.tx同时还没有初始化 cmake-GNinja..效果 构建比较快 cmake--build.效果├──CMakeCache.txt├──......
  • cmake 进行rpm包构建
    cmake实际上包含了构建,测试,以及打包的能力,以下是一个简单的rpm打包测试(cpack模块)项目结构├──CMakeLists.txt├──README.md├──add.c├──add.h└──main.c├──README.md代码说明main.c为一个入口(可执行文件)CMakeLists.txt是cmake的定义包含了基于cpack......
  • Uboot顶层Makefile解析-1. defconfig过程分析
    1版本号TopMakefile的开头会有版本描述,VERSION是主版本号,PATCHLEVEL是补丁版本号,SUBLEVEL是次版本号,这三个一起构成了uboot的版本号,比如当前的uboot版本号就是“2016.03”。EXTRAVERSION是附加版本信息,NAME是和名字有关的,一般不使用这两个。2MAKEFLAGS有两个特......
  • Make Lexicographically Smallest Array by Swapping Elements
    MakeLexicographicallySmallestArraybySwappingElementsYouaregivena 0-indexed arrayof positive integers nums anda positive integer limit.Inoneoperation,youcanchooseanytwoindices i and j andswap nums[i] and nums[j] if |nums......
  • Makefile - Error: Makefile:2: *** missing separator. Stop.
    Gotbelowerror:Makefile:2:***missingseparator. Stop. ChecktheMakefileusingcat-e-t-v:zzh@ZZHPC:/zdata/Github/zimplebank$cat-e-t-vMakefilecreatedb:$dockerexec-itpostgres16createdb--username=root--owner=rootzimple_bank$$......
  • Makefile中空格与tab
    空格与tabmakefile实际上是在一个文件中用两种完全不同的“语言”编写的。recipe(运行编译器,echo等的命令)是用shell脚本语法编写的。不在recipe中的其余makefile是用makefile语法编写的。为了使make能够区分recipe和不是recipe的东西,它使用了TAB字符。因此,以TAB开头的行......
  • [ARC168E] Subsegments with Large Sums
    题目链接看到严格选\(k\)个,不难想到WQS二分。定义\(f(x)\)为分成\(x\)段,最多有多少个超过\(S\)的。然后你会发现他不是凸的。因为他有很多平段,比如把两个很小的合并不改变答案。换个方向?考虑定义\(f(x)\)为有\(x\)个超过\(S\)的段,最多有多少个段。然后发现这个......