关于一些初级ACM竞赛题目的分析和题解(九)
一些逻辑题目,见下题:
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.
Help Kefa cope with this task!
The first line contains integer n (1 ≤ n ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.
6 2 2 1 3 4 1
3
3 2 2 9
3
In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.
In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.
Kefa决定在互联网上做一些钱,正好n天。 他知道在第i天(1≤i≤n),他赚了钱。 Kefa喜欢进步,这就是为什么他想知道序列ai中最大非递减子段的长度。 让我们提醒你,序列的子段是它的连续片段。 如果数字中的所有数字都是非递减的,则数字的一个子段称为非递减。
在第一个测试中,最大的非递减子段是从第三个到第五个的数字。
在第二个测试中,最大的非递减子段是从第一个到第三个的数字。
using namespace std;
typedef long long ll;
int a,b,c,n,s[100001];
int main()
{
cin>>n;
for (int i=0;i<n;i++)
{
cin>>s[i];
}
for (int i=0;i<n;i++)
{if(s[i]>=s[i-1])a++; //找到非递减序列
else (b=max(a,b),a=1);}// 不断寻找最大非递减序列的长度
b=max(a,b);
printf("%d",b);
}
For a positive integer n let's define a function f:
f(n) = - 1 + 2 - 3 + .. + ( - 1)nn
Your task is to calculate f(n) for a given integer n.
The single line contains the positive integer n (1 ≤ n ≤ 1015).
Print f(n) in a single line.
4
2
5
-3
f(4) = - 1 + 2 - 3 + 4 = 2
f(5) = - 1 + 2 - 3 + 4 - 5 = - 3
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,c,d,e,n;
int main()
{
scanf("%lld",&n);
if(n%2==0)
printf("%lld",n/2);
if(n%2!=0)
printf("%lld",(-1-n)/2);
}
是一个分奇偶的等差数列,分清题意就好了,