A - LIS O(n^2) 模板
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
题目意思是求最长上升子序列,dp[i]表示以a[i]结尾的上升子序列的长度。外层循环i从1到n遍历原序列,内层循环j从1到i,当a[j]<a[i]时,有转移方程:dp[i]=max(dp[i],dp[j]+1)。最后要遍历dp数组找最大值。最长上升子序列可不一定以最后一个元素结尾。
#include <iostream>
using namespace std;
const int N = 1000;
int a[N], dp[N];
int lis(int n)
{
int res = 0;
for(int i=0; i<n; i++) {
dp[i] = 1;
for(int j=0; j<i; j++)
if(a[j] < a[i])
dp[i] = max(dp[i], dp[j] + 1);
res = max(res, dp[i]);
}
return res;
}
int main()
{
int n;
while(cin >> n) {
for(int i=0; i<n; i++)
cin >> a[i];
cout << lis(n) << endl;
}
return 0;
}
C - Frog Jumping
A frog is currently at the point 00 on a coordinate axis OxOx. It jumps by the following algorithm: the first jump is aa units to the right, the second jump is bb units to the left, the third jump is aa units to the right, the fourth jump is bb units to the left, and so on.
Formally:
- if the frog has jumped an even number of times (before the current jump), it jumps from its current position xx to position x+ax+a;
- otherwise it jumps from its current position xx to position x−bx−b.
Your task is to calculate the position of the frog after kk jumps.
But... One more thing. You are watching tt different frogs so you have to answer tt independent queries.
Input
The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of queries.
Each of the next tt lines contain queries (one query per line).
The query is described as three space-separated integers a,b,ka,b,k (1≤a,b,k≤1091≤a,b,k≤109) — the lengths of two types of jumps and the number of jumps, respectively.
Output
Print tt integers. The ii-th integer should be the answer for the ii-th query.
Example
Input
6
5 2 3
100 1 4
1 10 5
1000000000 1 6
1 1 1000000000
1 1 999999999
Output
8
198
-17
2999999997
0
1
Note
In the first query frog jumps 55 to the right, 22 to the left and 55 to the right so the answer is 5−2+5=85−2+5=8.
In the second query frog jumps 100100 to the right, 11 to the left, 100100 to the right and 11 to the left so the answer is 100−1+100−1=198100−1+100−1=198.
In the third query the answer is 1−10+1−10+1=−171−10+1−10+1=−17.
In the fourth query the answer is 109−1+109−1+109−1=2999999997109−1+109−1+109−1=2999999997.
In the fifth query all frog's jumps are neutralized by each other so the answer is 00.
The sixth query is the same as the fifth but without the last jump so the answer is 11.
设置数据范围long long,判断k的奇偶性,代入式子即可得出答案。
#include<stdio.h>
int main()
{
int t;
long long a,b,k;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&a,&b,&k);
if(k%2==0)
printf("%lld\n",(a-b)*k/2);
else
printf("%lld\n",(a-b)*(k/2)+a);
}
}
D - Disturbed People
There is a house with nn flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of nn integer numbers a1,a2,…,ana1,a2,…,an, where ai=1ai=1 if in the ii-th flat the light is on and ai=0ai=0 otherwise.
Vova thinks that people in the ii-th flats are disturbed and cannot sleep if and only if 1<i<n1<i<n and ai−1=ai+1=1ai−1=ai+1=1 and ai=0ai=0.
Vova is concerned by the following question: what is the minimum number kk such that if people from exactly kk pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number kk.
Input
The first line of the input contains one integer nn (3≤n≤1003≤n≤100) — the number of flats in the house.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (ai∈{0,1}ai∈{0,1}), where aiai is the state of light in the ii-th flat.
Output
Print only one integer — the minimum number kk such that if people from exactly kk pairwise distinct flats will turn off the light then nobody will be disturbed.
Examples
Input
10
1 1 0 1 1 0 1 0 1 0
Output
2
Input
5
1 1 0 0 0
Output
0
Input
4
1 1 1 1
Output
0
Note
In the first example people from flats 22 and 77 or 44 and 77 can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.
There are no disturbed people in second and third examples.
暴力ac
#include<stdio.h>
int num[155];
int main()
{
int n;
int cnt=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
for(int i=1;i<n-1;i++)
{
if(num[i-1]==1&&num[i]==0&&num[i+1]==1)
{
cnt++;
num[i+1]=0;
}
}
printf("%d\n",cnt);
}
E - Good Array
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1,3,3,7]a=[1,3,3,7] is good because there is the element a4=7a4=7 which equals to the sum 1+3+31+3+3.
You are given an array aa consisting of nn integers. Your task is to print all indices jj of this array such that after removing the jj-th element from the array it will be good (let's call such indices nice).
For example, if a=[8,3,5,2]a=[8,3,5,2], the nice indices are 11 and 44:
- if you remove a1a1, the array will look like [3,5,2][3,5,2] and it is good;
- if you remove a4a4, the array will look like [8,3,5][8,3,5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in the array aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — elements of the array aa.
Output
In the first line print one integer kk — the number of indices jj of the array aa such that after removing the jj-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print kk distinct integers j1,j2,…,jkj1,j2,…,jk in any order — nice indices of the array aa.
If there are no such indices in the array aa, just print 00 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 22 so the array will look like [5,1,2,2][5,1,2,2]. The sum of this array is 1010 and there is an element equals to the sum of remaining elements (5=1+2+25=1+2+2).
In the second example you can remove 88 so the array will look like [3,5,2][3,5,2]. The sum of this array is 1010 and there is an element equals to the sum of remaining elements (5=3+25=3+2). You can also remove 22 so the array will look like [8,3,5][8,3,5]. The sum of this array is 1616 and there is an element equals to the sum of remaining elements (8=3+58=3+5).
In the third example you cannot make the given array good by removing exactly one element.
#include<stdio.h>
#include<string.h>
typedef long long LL;
const int maxn=2e5+10;
int t,cnt,a[maxn],flag,ans[1000000+10],b[maxn];
LL sum;
int main()
{
memset(ans,0,sizeof(ans));
memset(b,0,sizeof(b));
sum=cnt=flag=0;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
ans[a[i]]++;
}
for(int i=1;i<=t;i++)
{
LL q=sum-a[i];
LL j=q/2;
if(q%2)continue;
if(j<=1000000)
{
if(j==a[i]&&ans[j]>1||ans[j]==1&&a[i]!=j||t==3&&ans[j]==2&&a[i]!=j)
b[++cnt]=i;flag=1;
}
}
if(flag)
{
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
{
printf("%d ",b[i]);
}
printf("\n");
}
else
printf("0\n");
return 0;
}