You are given a permutation p=[p1,p2,…,pn] of integers from 1 to n. Let’s call the number m (1≤m≤n) beautiful, if there exists two indices l,r (1≤l≤r≤n), such that the numbers [pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m.
For example, let p=[4,5,1,3,2,6]. In this case, the numbers 1,3,5,6 are beautiful and 2,4 are not. It is because:
if l=3 and r=3 we will have a permutation [1] for m=1;
if l=3 and r=5 we will have a permutation [1,3,2] for m=3;
if l=1 and r=5 we will have a permutation [4,5,1,3,2] for m=5;
if l=1 and r=6 we will have a permutation [4,5,1,3,2,6] for m=6;
it is impossible to take some l and r, such that [pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m for m=2 and for m=4.
You are given a permutation p=[p1,p2,…,pn]. For all m (1≤m≤n) determine if it is a beautiful number or not.
Input
The first line contains the only integer t (1≤t≤1000) — the number of test cases in the input. The next lines contain the description of test cases.
The first line of a test case contains a number n (1≤n≤2⋅105) — the length of the given permutation p. The next line contains n integers p1,p2,…,pn (1≤pi≤n, all pi are different) — the given permutation p.
It is guaranteed, that the sum of n from all test cases in the input doesn’t exceed 2⋅105.
Output
Print t lines — the answers to test cases in the order they are given in the input.
The answer to a test case is the string of length n, there the i-th character is equal to 1 if i is a beautiful number and is equal to 0 if i is not a beautiful number.
Example
Input
3
6
4 5 1 3 2 6
5
5 3 1 2 4
4
1 4 3 2
Output
101011
11111
1001
Note
The first test case is described in the problem statement.
In the second test case all numbers from 1 to 5 are beautiful:
if l=3 and r=3 we will have a permutation [1] for m=1;
if l=3 and r=4 we will have a permutation [1,2] for m=2;
if l=2 and r=4 we will have a permutation [3,1,2] for m=3;
if l=2 and r=5 we will have a permutation [3,1,2,4] for m=4;
if l=1 and r=5 we will have a permutation [5,3,1,2,4] for m=5.
给你n个数,它是从1到n的排列,问你从1到n中,给定的序列能组成哪些长度的从1-n的序列。
如题目中的 451326 4 5 1 3 2 6 451326
长度为1的时候1满足条件
长度为2的时候由于1和2之间有个3,所以组不成一个1-2的排列
长度为3的时候1 3 2 正好是一个1-3的序列,所以3位置是1
长度为4的时候4到1中间有一个5,所以不行
长度为5的时候 45132 4 5 1 3 2 45132是一个排列 所以可以
长度为6的时候一定可以,因为给定的n个数就是从1到n的一个排列。
先输入每一个的值,用id把每一个数的初始位置记下来
对数组按照值从小到大排序,排成1-n的形式
这时候从1到n进行遍历,每次都更新l到r,l是当前的id和之前的l小的那个,而r是大的那个。如果当前的区间长度(r-l+1)正好等于i,就说明从1-i中所有的数都恰巧在这个序列中因为是从小到大找的,所以就满足题意是1-i的一个排列,反之是0。
#include
#include
using namespace std;
const int maxn = 200000+100;
class node
{
public:
int val,id;
}a[maxn];
int ans[maxn];
bool cmp(node a,node b)
{
return a.val<b.val;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i].val),a[i].id=i,ans[i]=0;
sort(a+1,a+1+n,cmp);
int l = a[1].id;
int r = a[1].id;
for(int i=1;i<=n;i++)
{
l=min(l,a[i].id);
r=max(r,a[i].id);
if(r-l+1==i)
ans[i]=1;
}
for(int i=1;i<=n;i++)
printf("%d",ans[i]);
printf("\n");
}
}
You have three piles of candies: red, green and blue candies:
the first pile contains only red candies and there are r candies in it,
the second pile contains only green candies and there are g candies in it,
the third pile contains only blue candies and there are b candies in it.
Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can’t eat two candies of the same color in a day.
Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.
Input
The first line contains integer t (1≤t≤1000) — the number of test cases in the input. Then t test cases follow.
Each test case is given as a separate line of the input. It contains three integers r, g and b (1≤r,g,b≤108) — the number of red, green and blue candies, respectively.
Output
Print t integers: the i-th printed integer is the answer on the i-th test case in the input.
Example
Input
6
1 1 1
1 2 1
4 1 1
7 4 10
8 1 4
8 2 8
Output
1
2
2
10
5
9
Note
In the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.
In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.
In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten.
给你三堆糖果,每次可以同时吃两个不同颜色的,问最后最多吃多少次
1️⃣ 如果a+b
如果a+b>c 那么可以先用c把max(a,b)先吃到满足abs(a-b)<=1 因为这时候a和b相差最多为1最少为0
所以消耗完c以后可能a或b剩一个,也可能都不剩
#include
#include
using namespace std;
// const int maxn = 3;
const int maxn = 100+10;
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&a[0],&a[1],&a[2]);
sort(a,a+3);
if(a[0]+a[1]<=a[2])
{
printf("%d\n",a[0]+a[1]);
}
else
{
printf("%d\n",(a[0]+a[1]+a[2])/2);
}
}
}
Alex has a sequence S which contains n elements, let’s define f(i, j) denoting the xor sum of Si, Si+1, …, Sj. Now Alex wants to know the value of f(1, 1) xor f(1, 2) xor … xor f(1, n) xor f(2, 2) xor f(2, 3) xor … xor f(2, n) xor … xor f(n, n).
Input
Input starts with an integer T denoting the number of test case.
For each test case, first line contains an integer n (1 <= n <= 100000).
Next line contains n integers Si(1 <= Si <= 100000).
Output
For each test case. print the value of
f(1, 1) xor f(1, 2) xor … xor f(1, n) xor f(2, 2) xor f(2, 3) xor … xor f(2, n) xor … xor f(n, n).
Sample Input
1
3
1 3 2
Sample Output
3
Hint
无
定义 ( f ( i , j ) ) (f(i,j)) (f(i,j))是从 s i si si异或到 s j sj sj的结果,现在要求从 f ( 1 , 1 ) f(1,1) f(1,1)异或到 f ( n , n ) f(n,n) f(n,n)的结果
1️⃣需要明白一个数异或自己奇数次是它本身 ,异或偶数次是0
2️⃣首先预处理一下从 f ( 1 , 1 ) 到 f ( 1 , n ) f(1,1)到f(1,n) f(1,1)到f(1,n)的值,然后如果要算异或 f ( 2 , 2 ) 到 f ( 2 , n ) f(2,2)到f(2,n) f(2,2)到f(2,n)的结果时,如果上一次的第一列本身是奇数次的话,就需要再异或一下那一列的数,如果是偶数次的话就不需要操作因为偶数次那一列就相当于不存在
#include
using namespace std;
typedef long long ll;
const int maxn = 100000+100;
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,now;
scanf("%d%d%d",&n,&a[1],&a[2]);
int m = n;
if(m&1)
now = a[1];
else
now = a[2];
m-=2;
for(int i=3;i<=n;i++)
{
scanf("%d",&a[i]);
if(m&1)
now^=a[i];
m--;
}
m = n;
int ans = now;//从11到1n的亦或和
for(int i=1;i<=n;i++)
{
if(m&1)
{
now^=a[i];//把前面一列去掉
}
ans^=now;
m--;
}
printf("%d\n",ans);
}
}
//每次要异或的东西都先处理好 然后再异或
Alex likes to play with one and zero!One day he gets an empty string.So our cute boy wants to add one and zero in it. Every time he will add ‘01’in the string at any position and then get a new string.For example:if the string is “01” now ,he can get “0101” or “0011,Now give you a string that is Alex has get,you need to answer whether the string is legal?
Input
First is a integer n(n<=100)
Next contains n lines .Every line is a string whose legth is no more than 1000.
Output
For each case output “YES” in a single line if it’s legal.
Or you need to output “NO”;
Sample Input
3
0101
0110
0011
Sample Output
YES
NO
YES
Hint
无
每一次只能往字符串里面加入01(按顺序)问现在给的字符串是否合法
其实这题好像就是括号匹配) 因为每次都只能按顺序加入01,只需要判断0前面有没有1就行了。设置一个now来记录当前的情况,遇到0就+1 遇到1就-1 必须保持now时时刻刻都>=0 因为如果<0就说明有1在0的前面,不符合条件应该输出NO。
#include
#include
#include
#include
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int flag=1;
string s;
cin>>s;
int len = s.size(),now=0;
for(int i=0;i<len;i++)
{
if(s[i]=='0')
now++;
else
now--;
if(now<0)
{
flag=0;
break;
}
}
if(now!=0 || flag==0)
printf("NO\n");
else
printf("YES\n");
}
}
A string is called beautiful if no two consecutive characters are equal. For example, “ababcb”, “a” and “abab” are beautiful strings, while “aaaaaa”, “abaa” and “bb” are not.
Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’. Ahcl needs to replace each character ‘?’ with one of the three characters ‘a’, ‘b’ or ‘c’, such that the resulting string is beautiful. Please help him!
More formally, after replacing all characters ‘?’, the condition si≠si+1 should be satisfied for all 1≤i≤|s|−1, where |s| is the length of the string s.
Input
The first line contains positive integer t (1≤t≤1000) — the number of test cases. Next t lines contain the descriptions of test cases.
Each line contains a non-empty string s consisting of only characters ‘a’, ‘b’, ‘c’ and ‘?’.
It is guaranteed that in each test case a string s has at least one character ‘?’. The sum of lengths of strings s in all test cases does not exceed 105.
Output
For each test case given in the input print the answer in the following format:
If it is impossible to create a beautiful string, print “-1” (without quotes);
Otherwise, print the resulting beautiful string after replacing all ‘?’ characters. If there are multiple answers, you can print any of them.
Example
Input
3
a???cb
a??bbc
a?b?c
Output
ababcb
-1
acbac
Note
In the first test case, all possible correct answers are “ababcb”, “abcacb”, “abcbcb”, “acabcb” and “acbacb”. The two answers “abcbab” and “abaabc” are incorrect, because you can replace only ‘?’ characters and the resulting string must be beautiful.
In the second test case, it is impossible to create a beautiful string, because the 4-th and 5-th characters will be always equal.
In the third test case, the only answer is “acbac”.
字符串中有许多? 要在保持字符串中没有两个相同字符挨着的情况下把?改成abc其中一个。
先判断给你的字符串中有没有两个相同的字符挨着,如果有的话就输出-1.如果没有相同的话就一定能填充,因为前面的对后面没有影响,而且有三种字符可以选所以只需要判断当前?两边的字符是不是和你想要填入的字符相同就行了。就算ab都被占用了,还有个c等着呢不是 2333.
#include
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int flag=1;
string s;
cin>>s;
int len = s.size();
for(int i=0;i<len-1;i++)
{
if(s[i]!='?' && s[i]==s[i+1])
{
flag=0;
break;
}
}
if(!flag)
{
printf("-1\n");
continue;
}
else
{
for(int i=0;i<=len-1;i++)
{
if(s[i]=='?')
{
if('a'!=s[i-1]&&'a'!=s[i+1])
s[i]='a';
else if('b'!=s[i-1]&&'b'!=s[i+1])
s[i]='b';
else if('c'!=s[i-1]&&'c'!=s[i+1])
s[i]='c';
}
}
}
cout<<s<<"\n";
}
}
hkhv学长最近对二进制数很感兴趣,喜欢一切0和1组成的数。现在有一个十进制整数n,问你1到n之间有多少个数是只有0和1组成的类似二进制的数,输出他们的个数。
Input
输入数据包含一个数n (1 <= n <=10^9).
Output
输出1到n中类似二进制的数的个数.
Sample Input
10
Sample Output
2
Hint
对于n = 10,1 和 10是类似二进制的数.
题意很明确了。
从1到1e9类似二进制的十进制数最多也就1000多个,直接枚举十进制转换成类似二进制的十进制的最大值,一直和输入的n比较就行了。转换的时候写个dfs,目的是把十进制的数转换成类似二进制的十进制,过程模拟一下就行了。每次要转换的++ 因为一共没多少,所以可行。
#include
using namespace std;
typedef long long ll;
ll dfs(ll x)
{
if(x==0) return 0;
if(x==1) return 1;
return dfs(x>>1)*10+(x&1);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
ll ans=1,cnt=0;
while(ans<=n)
{
cnt++;
ans=dfs(cnt);
// printf("%lld\n",ans);
}
printf("%lld\n",cnt-1);
}
}
Frog Wa has many frogs, so he can hear GuaGuaGua ev在这里插入代码片
ery day. But one day a flappy bird ate his frogs, so Frog Wa decided to build a fence to protect his precious frogs. Frog Wa told you four points (x, 0), (x, y), (z, 0), (z, t) which represent for the vertex of the fence. he want to know the area of the fence, can you tell him?
Input
The input consist of four integers x, y, z and t. 0 <= x, y, z, t <= 10000, x < z and y < t.
Output
For each test case, print the area, numbers should accurate to one decimal places.
Sample Input
1 1 2 2
0 2 6 6
0 0 2 10
Sample Output
1.5
24.0
10.0
Hint
无
求梯形面积
#include
#include
using namespace std;
int main()
{
double x,y,z,t;
while(~scanf("%lf%lf%lf%lf",&x,&y,&z,&t))
printf("%.1lf\n",(fabs(y)+fabs(t))*fabs(x-z)/2);
}