列表

详情


NC14650. 开心的cc

描述

CC is a smart girl and she is curious about everything. One day she starts to analyze her lifestyle and finds out that she either feels happy or unhappy in any day, then she collects her mood data and makes numerical representation. She writes down 1 if she is happy, otherwise she writes down 0. 
For example, if CC is happy, happy, unhappy, happy, happy, unhappy in the next 6 days, a numerical sequence which stands for it is 1,1,0,1,1,0

Meanwhile, smart CC finds her mood sequence is always periodic. She also finds the repeated segment like 1,1,0 in the aforementioned example and calls it “feeling repetend”. As a curious girl, CC wants to make another deep analysis. After she gets the length of “feeling repetend” n, she defines a rule: she tries to find a day whose index is d and considers the next d+n-1 days. For each day d' in [d, d+n-1], CC guarantee that the number of happy days is more than unhappy days in [1, d'] days.

Now, CC wonder that how many days which satisfy the rule in n “feeling repetend” days.

输入描述

Input contains multiple test cases.
The first line contains an integer T (1<=T<=20), which is the number of test cases.
Then the first line of each test case contains an integer n (1<=n<=100000), which is the length of “feeling repetend”.
The second line of each test case contains n integers, each integer is either 0 or 1, standing for unhappy or happy.

输出描述

output a integer represents the answer.

示例1

输入:

2
5
1 0 1 1 0
5 
1 1 1 1 1

输出:

1
5

说明:

For the sample,the days from the third day as a starting point are as follows:
Day1: 1 day happy : 0 days unhappy
Day2: 2 days happy : 0 days unhappy
Day3: 2 days happy : 1 day unhappy
Day4: 3 days happy : 1 day unhappy
Day5: 3 days happy : 2 days unhappy

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 188ms, 内存消耗: 2316K, 提交时间: 2020-04-02 14:31:24

#include<iostream>
using namespace std;
int main()
{
	int n, t;
	cin >> t;
	while (t--)
	{
		int a, x = 0, y = 0;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> a;
			if (a == 1)x++;
			else y++;
		}
		if (x - y > 0)cout << x - y << endl;
		else cout << "0" << endl;
	}
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 108ms, 内存消耗: 268K, 提交时间: 2017-12-19 20:36:02

#include<cstdio>
int main(){
	int t,n;
	scanf("%d",&t);
	while(t--){
		int a,x=0,y=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&a);
			if(a==1)  x++;
			else  y++;
		}
		if(x-y>0)  printf("%d\n",x-y);
		else  printf("0\n");
	}
	return 0;
} 

C 解法, 执行用时: 77ms, 内存消耗: 320K, 提交时间: 2022-04-11 20:14:00

int c,t,n;
main(){
    scanf("%d",&t);
    while(t--){
        int a=0,b=0;
        scanf("%d",&n);
        while(n--){scanf("%d",&c);if(c)++a;else++b;}
        printf("%d\n",a-b>0?a-b:0);
    }
}

上一题