time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1≤n≤109) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
inputCopy
6
18
1
9
100500
33
1000000000
outputCopy
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
#include
using namespace std;
int t,n;
void solve(int x)
{
if(x<=9){cout<<x<<endl;return;}
int len=log10(x)+1;
int ans=(len-1)*9;
int a=x;
while(a/10!=0)
a/=10;
long long sum=0;
for(int i=1;i<=len;i++)
{
sum=sum*10+a;
}
if(x>=sum)ans+=a;else ans+=a-1;
cout<<ans<<endl;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
solve(n);
}
return 0;
}
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n positive integers a1,a2,…,an. For the one move you can choose any even value c and divide by two all elements that equal c.
For example, if a=[6,8,12,6,3,12] and you choose c=6, and a is transformed into a=[3,8,12,3,3,12] after the move.
You need to find the minimal number of moves for transforming a to an array of only odd integers (each element shouldn’t be divisible by 2).
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains n (1≤n≤2⋅105) — the number of integers in the sequence a. The second line contains positive integers a1,a2,…,an (1≤ai≤109).
The sum of n for all test cases in the input doesn’t exceed 2⋅105.
Output
For t test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by 2).
Example
inputCopy
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
outputCopy
4
10
4
0
Note
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves a=[40,6,40,3,20,1];
choose c=6;
now a=[40,3,40,3,20,1];
choose c=40;
now a=[20,3,20,3,20,1];
choose c=20;
now a=[10,3,10,3,10,1];
choose c=10;
now a=[5,3,5,3,5,1] — all numbers are odd.
Thus, all numbers became odd after 4 moves. In 3 or fewer moves, you cannot make them all odd.
#include
using namespace std;
int t,n;
int a;
set<int>v;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++){cin>>a;if(a%2==0)v.insert(a);}
int ans=0;
while(!v.empty())
{
auto i=--v.end();
int k=*i;
v.erase(i);
ans++;
k=k/2;
if(k&1)continue;
else v.insert(k);
}
v.clear();
cout<<ans<<endl;
}
return 0;
}
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a non-empty string s=s1s2…sn, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string “one” or at least one string “two” (or both at the same time) as a substring. In other words, Polycarp does not like the string s if there is an integer j (1≤j≤n−2), that sjsj+1sj+2=“one” or sjsj+1sj+2=“two”.
For example:
Polycarp does not like strings “oneee”, “ontwow”, “twone” and “oneonetwo” (they all have at least one substring “one” or “two”),
Polycarp likes strings “oonnee”, “twwwo” and “twnoe” (they have no substrings “one” and “two”).
Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time.
For example, if the string looks like s=“onetwone”, then if Polycarp selects two indices 3 and 6, then “onetwone” will be selected and the result is “ontwne”.
What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be?
Input
The first line of the input contains an integer t (1≤t≤104) — the number of test cases in the input. Next, the test cases are given.
Each test case consists of one non-empty string s. Its length does not exceed 1.5⋅105. The string s consists only of lowercase Latin letters.
It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed 1.5⋅106.
Output
Print an answer for each test case in the input in order of their appearance.
The first line of each answer should contain r (0≤r≤|s|) — the required minimum number of positions to be removed, where |s| is the length of the given line. The second line of each answer should contain r different integers — the indices themselves for removal in any order. Indices are numbered from left to right from 1 to the length of the string. If r=0, then the second line can be skipped (or you can print empty). If there are several answers, print any of them.
Examples
inputCopy
4
onetwone
testme
oneoneone
twotwo
outputCopy
2
6 3
0
3
4 1 7
2
1 4
inputCopy
10
onetwonetwooneooonetwooo
two
one
twooooo
ttttwo
ttwwoo
ooone
onnne
oneeeee
oneeeeeeetwooooo
outputCopy
6
18 11 12 1 6 21
1
1
1
3
1
2
1
6
0
1
4
0
1
1
2
1 11
Note
In the first example, answers are:
“onetwone”,
“testme” — Polycarp likes it, there is nothing to remove,
“oneoneone”,
“twotwo”.
In the second example, answers are:
“onetwonetwooneooonetwooo”,
“two”,
“one”,
“twooooo”,
“ttttwo”,
“ttwwoo” — Polycarp likes it, there is nothing to remove,
“ooone”,
“onnne” — Polycarp likes it, there is nothing to remove,
“oneeeee”,
“oneeeeeeetwooooo”.
#include
using namespace std;
int t,n;
string s;
vector<int>ans;
int main()
{
cin>>t;
while(t--)
{
cin>>s;
int len=s.length();
//cout<
ans.clear();
int i=0;
while(i<len-2)
{
if(s[i]!='t'&&s[i]!='o'){i++;continue;}
if(i<len-5&&s.substr(i,6)=="twoone")
{
ans.push_back(i+2);
ans.push_back(i+5);
i=i+6;
}
else if(i<len-4&&s.substr(i,5)=="twone")
{
ans.push_back(i+3);
i=i+5;
}
else if(s.substr(i,3)=="two")
{
ans.push_back(i+2);
i+=3;
}
else if(s.substr(i,3)=="one")
{
ans.push_back(i+2);
i+=3;
}
else i++;
}
int v=ans.size();
cout<<v<<endl;
if(v==0)cout<<endl;
else
{
for(auto i:ans)
{
cout<<i<<" ";
}cout<<'\n';
}
}
return 0;
}
D题到底是干什么,看不懂
第一个样例的第一个不能删第一个串吗???哪位大佬给俺说一下
已看懂 反转后的串若出现过 该串就不能反转
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp has n different binary words. A word called binary if it contains only characters ‘0’ and ‘1’. For example, these words are binary: “0001”, “11”, “0” and “0011100”.
Polycarp wants to offer his set of n binary words to play a game “words”. In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: “0101”, “1”, “10”, “00”, “00001”.
Word reversal is the operation of reversing the order of the characters. For example, the word “0111” after the reversal becomes “1110”, the word “11010” after the reversal becomes “01011”.
Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that:
the final set of n words still contains different words (i.e. all words are unique);
there is a way to put all words of the final set of words in the order so that the final sequence of n words is consistent with the game rules.
Polycarp wants to reverse minimal number of words. Please, help him.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains one integer n (1≤n≤2⋅105) — the number of words in the Polycarp’s set. Next n lines contain these words. All of n words aren’t empty and contains only characters ‘0’ and ‘1’. The sum of word lengths doesn’t exceed 4⋅106. All words are different.
Guaranteed, that the sum of n for all test cases in the input doesn’t exceed 2⋅105. Also, guaranteed that the sum of word lengths for all test cases in the input doesn’t exceed 4⋅106.
Output
Print answer for all of t test cases in the order they appear.
If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain k (0≤k≤n) — the minimal number of words in the set which should be reversed. The second line of the output should contain k distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from 1 to n in the order they appear. If k=0 you can skip this line (or you can print an empty line). If there are many answers you can print any of them.
Example
inputCopy
4
4
0001
1000
0011
0111
3
010
101
0
2
00000
00001
4
01
001
0001
00001
outputCopy
1
3
-1
0
2
1 2
#include
using namespace std;
int t,n;
string s;
vector<int> v[5];
vector<string>z;
int a[5],b[5];
map<string,bool>mp;
int main()
{
cin>>t;
while(t--)
{
mp.clear();z.clear();
memset(a,0,sizeof(a));memset(b,0,sizeof(b));
for(int i=1;i<=4;i++)v[i].clear();
cin>>n;
int f=0,c,d;
for(int i=1;i<=n;i++)
{
cin>>s;
z.push_back(s);
c=*s.begin()-'0';
d=*--s.end()-'0';
if(c==1&&d==0)
{
mp[s]=true;a[1]++;
}
else if(c==0&&d==1)
{
mp[s]=true;a[2]++;
}
else if(c==1&&d==1)
{
a[3]++;
}
else {a[4]++;}
}
for(int i=0;i<n;i++)
{
s=z[i];
c=*s.begin()-'0';
d=*--s.end()-'0';
reverse(s.begin(),s.end());
if(mp[s]==false)
{
if(c==1&&d==0)
{
v[1].push_back(i+1);
}
else if(c==0&&d==1)
{
v[2].push_back(i+1);
}
}
}
if((a[3]>0||a[4]>0)&&a[1]==0&&a[2]==0){
if(a[3]>0&&a[4]>0)
{cout<<-1<<endl;}
else cout<<0<<endl;
continue;
}
if(a[1]>0||a[2]>0)
{
int g=abs(a[1]-a[2])/2;
if(g==0)
{
cout<<0<<endl<<endl;
continue;
}
else if(a[1]>a[2])
{
int y=0;
if(v[1].size()<g){cout<<-1<<endl;continue;}
cout<<g<<endl;
for(auto i:v[1])
{
cout<<i<<" ";
++y;
if(y==g)break;
}cout<<endl;continue;
}
else
{
int y=0;
if(v[2].size()<g){cout<<-1<<endl;continue;}
cout<<g<<endl;
for(auto i:v[2])
{
cout<<i<<" ";
++y;
if(y==g)break;
}cout<<endl;continue;
}
}
}
return 0;
}