3 条题解
-
0
分奇偶
奇数项平均值存在且为整数,与偶数项相等
使用s1,s2记录奇数项与偶数项的和,使用n1,n2记录奇数项和偶数项的项数。
核心代码
有注释
tt=read_(); //s应为longlong,否则溢出(被坑了) s1=0;n1=0;s2=0;n2=0; while(tt--){ s2+=read_();n2++; //合并偶数/奇数操作,使用if排除奇数项与偶数项不相等的情况 if(tt--){s1+=read_();n1++;}else{break;} } //首先保证整除 if(s2%n2){write_str("NO\n");continue;} if(s1%n1){write_str("NO\n");continue;} //其次判断相等 if(s1/n1==s2/n2)write_str("YES\n");else write_str("NO\n");
AC代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <sys/stat.h> #include <sys/mman.h> #include <unistd.h> #define N1 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1 #define N2 N1,N1,N1,N1,N1,N1,N1,N1,N1,N1 #define N3 N2,N2,N2,N2,N2,N2,N2,N2,N2,N2 #define N4 N3,N3,N3,N3,N3,N3,N3,N3,N3,N3 #define D(a) a,1##a,2##a,3##a,4##a,5##a,6##a,7##a,8##a,9##a #define S246 N2,N2,N1,N1,N1,N1,-1,-1,-1,-1,-1,-1 const int _mp[0x10000] = { N4,N3,N3,N2,N2,N2,N1,N1,N1,-1,-1,-1,-1,-1,-1, D(0),S246,D(1),S246,D(2),S246,D(3),S246,D(4),S246, D(5),S246,D(6),S246,D(7),S246,D(8),S246,D(9), N4,N4,N4,N4,N4,N2,N2,N2,N2,N2,N2,N2,N2,N1,N1,N1,N1,N1,N1,N1,N1, -1,-1,-1,-1,-1,-1 }; #undef N1 #undef N2 #undef N3 #undef N4 #undef D #undef S246 char *buf_ptr; char *buf_end; char out_buf[1 << 20]; char *pp = out_buf; void io_init(){ struct stat sb; fstat(0,&sb); buf_ptr=(char*)mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,0,0); buf_end=buf_ptr+sb.st_size; } int read_(){ int x=0,f=0; while(buf_ptr<buf_end && *buf_ptr<=' ') buf_ptr++; if(buf_ptr>=buf_end) return 0; if(*buf_ptr=='-'){ f=1; buf_ptr++; } while(buf_ptr+1<buf_end){ int v=_mp[*(uint16_t*)buf_ptr]; if(v==-1) break; x = x*100 + v; buf_ptr += 2; } if(buf_ptr<buf_end && *buf_ptr>='0' && *buf_ptr<='9'){ x = x*10 + (*buf_ptr - '0'); buf_ptr++; } return f ? -x : x; } void push(char c){ if(pp - out_buf == (1<<20)){ fwrite(out_buf,1,1<<20,stdout); pp = out_buf; } *pp++ = c; } void write_(int x){ if(x<0){ push('-'); x = -x; } static int sta[40]; int top=0; do{ sta[top++] = x%10; x/=10; } while(x); while(top) push(sta[--top] + '0'); } void write_str(const char* s){ while(*s) push(*s++); } void io_flush(){ fwrite(out_buf,1,pp - out_buf,stdout); } int gc(){return(unsigned char)*buf_ptr++;} static inline void mvp(int n){buf_ptr+=n;} int main() { io_init(); int t,tt,n1,n2; long long s1,s2; t=read_(); while(t--){ tt=read_(); s1=0;n1=0;s2=0;n2=0; while(tt--){ s2+=read_();n2++; if(tt--){s1+=read_();n1++;}else{break;} } if(s2%n2){write_str("NO\n");continue;} if(s1%n1){write_str("NO\n");continue;} if(s1/n1==s2/n2)write_str("YES\n");else write_str("NO\n"); } io_flush(); return 0; } -
0
saki酱●█▀█▄saki酱●█▀█▄saki酱●█▀█▄
题目分析
根据题意不难得出,对于任意被操作的两个数的下标的奇偶性是相同的;同时,无论怎么操作,所有奇数项或偶数项的总和是不变的。所以可以满足题目要求的必要条件是奇数项的平均数和偶数项的平均数都是整数且相等。所以将数组的奇数项和偶数项求和再判断平均数是否为整数以及相等即可。
代码如下(cpp):
#include <bits/stdc++.h> using namespace std; int main(){ int t; scanf("%d", &t); while(t--){ int n; scanf("%d", &n); long long num1 = 0, num2 = 0; for(int i = 1; i <= n; i++){ int x; scanf("%d", &x); if(i & 1) num1 += x; else num2 += x; } if(num1 % ((n + 1) / 2) == 0 && num2 % (n / 2) == 0 && (num1 / ((n + 1) / 2)) == (num2 / (n / 2))) printf("YES\n"); else printf("NO\n"); } return 0; } -
0
saki酱●█▀█▄saki酱●█▀█▄saki酱●█▀█▄
首先,整个数组的总和不随操作与否而变化,而如果最后所有元素可能可以相等的一个前提是:数组所有元素和是数组大小的某个正整数倍(x)。其次,一个元素操作后可能影响的其他元素的序号的奇偶性必定与它本身的序号奇偶性一致,所以将数组分为奇数序号部分和偶数序号部分分别取和判断是否为元素数量的x倍即可
参考代码(python)
from sys import stdin,setrecursionlimit from math import inf,ceil,sqrt from collections import Counter,deque def solve(): s1,s2=0,0 for i in range(n): if i&1: s1+=a[i] else: s2+=a[i] if s1==g*(n//2) and s2==g*ceil(n/2): print('YES') else: print('NO') for _ in range(int(stdin.readline())): n=int(stdin.readline()) a=[int(_) for _ in stdin.readline().split()] if sum(a)%n!=0: print('NO') continue g=sum(a)//n solve()
- 1
信息
- ID
- 1097
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 156
- 已通过
- 25
- 上传者