Arrays of Length Zero

博客介绍了GNU C中零长度数组的相关内容。作为扩展,零长度数组可作为变长对象头部结构体的最后元素,虽其大小为零,但可能因尾部填充增加封闭类型大小。在其他上下文声明零长度数组不被鼓励,访问此类数组元素未定义。此外还提及ISO C90中类似情况的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Arrays of Length Zero

https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html#Zero-Length

Declaring zero-length arrays is allowed in GNU C as an extension. A zero-length array can be useful as the last element of a structure that is really a header for a variable-length object:

struct line {
  int length;
  char contents[0];
};

struct line *thisline = (struct line *)
  malloc (sizeof (struct line) + this_length);
thisline->length = this_length;

Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type. The alignment of a zero-length array is the same as the alignment of its elements.

Declaring zero-length arrays in other contexts, including as interior members of structure objects or as non-member objects, is discouraged. Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed.

In the absence of the zero-length array extension, in ISO C90 the contents array in the example above would typically be declared to have a single element. Unlike a zero-length array which only contributes to the size of the enclosing structure for the purposes of alignment, a one-element array always occupies at least as much space as a single object of the type. Although using one-element arrays this way is discouraged, GCC handles accesses to trailing one-element array members analogously to zero-length arrays.

C. Need More Arrays time limit per test2 seconds memory limit per test256 megabytes Given an array a and n integers. It is sorted in non-decreasing order, that is, ai≤ai+1 for all 1≤i<n . You can remove any number of elements from the array (including the option of not removing any at all) without changing the order of the remaining elements. After the removals, the following will occur: a1 is written to a new array; if a1+1<a2 , then a2 is written to a new array; otherwise, a2 is written to the same array as a1 ; if a2+1<a3 , then a3 is written to a new array; otherwise, a3 is written to the same array as a2 ; ⋯ For example, if a=[1,2,4,6] , then: a1=1 is written to the new array, resulting in arrays: [1] ; a1+1=2 , so a2=2 is added to the existing array, resulting in arrays: [1,2] ; a2+1=3 , so a3=4 is written to a new array, resulting in arrays: [1,2] and [4] ; a3+1=5 , so a4=6 is written to a new array, resulting in arrays: [1,2] , [4] , and [6] . Your task is to remove elements in such a way that the described algorithm creates as many arrays as possible. If you remove all elements from the array, no new arrays will be created. Input The first line of input contains one integer t (1≤t≤104 ) — the number of test cases. The first line of each test case contains one integer n (1≤n≤2⋅105 ) — the length of the array. The second line of each test case contains n integers a1,a2,…,an (1≤ai≤106 , ai≤ai+1 ) — the elements of the array. It is guaranteed that the sum of n across all test cases does not exceed 2⋅105 . Output For each test case, output one integer — the maximum number of arrays that can be obtained by removing any (possibly zero) number of elements. Example InputCopy 6 6 1 2 3 4 5 6 3 1 2 3 4 1 2 2 4 1 2 3 1 4 8 2 1 1 OutputCopy 3 2 2 1 3 1 Note In the first example, you can remove a3 and a5 , then a=[1,2,4,6] , the process of forming arrays for it is shown in the statement. In the second example, you need to remove a2 , after which a=[1,3] , and the arrays [1] and [3] will be written. In the third example, no removals are needed; for a=[1,2,2,4] , the arrays [1,2,2] and [4] will be written.用c++
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值