Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3009 Accepted Submission(s): 441
Problem Description
We define the distance of two strings A and B with same length n is
disA,B=∑i=0n−1|Ai−Bn−1−i|
The difference between the two characters is defined as the difference in ASCII.
You should find the maximum length of two non-overlapping substrings in given string S, and the distance between them are less then or equal to m.
disA,B=∑i=0n−1|Ai−Bn−1−i|
The difference between the two characters is defined as the difference in ASCII.
You should find the maximum length of two non-overlapping substrings in given string S, and the distance between them are less then or equal to m.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integers m : the limit distance of substring.
Then a string S follow.
Limits
T≤100
0≤m≤5000
Each character in the string is lowercase letter, 2≤|S|≤5000
∑|S|≤20000
Each case begins with one line with one integers m : the limit distance of substring.
Then a string S follow.
Limits
T≤100
0≤m≤5000
Each character in the string is lowercase letter, 2≤|S|≤5000
∑|S|≤20000
Output
For each test case output one interge denotes the answer : the maximum length of the substring.
Sample Input
1 5 abcdefedcb
Sample Output
5Hint[0, 4] abcde [5, 9] fedcb The distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5
题解:运用尺取法,对字符串进行缩进。并且记录最大值。
ac code:
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
const int N=2e5+5;
const int M=6666666;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;
const int mod=998244353;
int m;
string s;
int solve()
{
int len=s.length();
int ans=0;
for(int i=len;i>=1;i--)
{
int cnt=i/2-1,t=0,p=0,d=0;
for(int j=0;j<=cnt;j++)
{
d+=abs(s[j]-s[i-j-1]);
if(d>m)
{
d-=abs(s[j]-s[i-j-1]);
d-=abs(s[p]-s[i-p-1]);
t--;
j--;
p++;
}
else
{
t++;
ans=max(ans,t);
}
}
}
return ans;
}
int main()
{
int T;
si1(T);
while(T--)
{
si1(m);
cin>>s;
int ans=0;
ans=max(ans,solve());
reverse(s.begin(),s.end());//需要翻转一次确保答案最大
ans=max(ans,solve());
printf("%d\n",ans);
}
}
Inversion
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1826 Accepted Submission(s): 868
Problem Description
Give an array A, the index starts from 1.
Now we want to know Bi=maxi∤jAj , i≥2 .
Now we want to know Bi=maxi∤jAj , i≥2 .
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer n : the size of array A.
Next one line contains n integers, separated by space, ith number is Ai .
Limits
T≤20
2≤n≤100000
1≤Ai≤1000000000
∑n≤700000
Each case begins with one line with one integer n : the size of array A.
Next one line contains n integers, separated by space, ith number is Ai .
Limits
T≤20
2≤n≤100000
1≤Ai≤1000000000
∑n≤700000
Output
For each test case output one line contains n-1 integers, separated by space, ith number is
Bi+1
.
Sample Input
2 4 1 2 3 4 4 1 4 2 3
Sample Output
3 4 3 2 4 4
ac code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
const int N=1100001;
const int M=6666666;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;
const int mod=1e9+7;
struct node
{
int index,num;
}a[700007];
int b[700007];
bool cmp(node x,node y)
{
return x.num>y.num;
}
int main()
{
int T;
si1(T);
while(T--)
{
int n;
si1(n);
for(int i=1;i<=n;i++)
{
si1(a[i].num);
a[i].index=i;
}
sort(a+1,a+1+n,cmp);
//printf("%d\n",a[1]);
int k=1,cnt=0;
for(int i=2;i<=n;i++)
{
k=1;
while(a[k].index%i==0)
{
k++;
}
b[cnt++]=a[k].num;
}
printf("%d",b[0]);
for(int i=1;i<n-1;i++)
{
printf(" %d",b[i]);
}
printf("\n");
}
}
Classes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2048 Accepted Submission(s): 895
Problem Description
The school set up three elective courses, assuming that these courses are A, B, C. N classes of students enrolled in these courses.
Now the school wants to count the number of students who enrolled in at least one course in each class and records the maximum number of students.
Each class uploaded 7 data, the number of students enrolled in course A in the class, the number of students enrolled in course B, the number of students enrolled in course C, the number of students enrolled in course AB, the number of students enrolled in course BC, the number of students enrolled in course AC, the number of students enrolled in course ABC. The school can calculate the number of students in this class based on these 7 data.
However, due to statistical errors, some data are wrong and these data should be ignored.
Smart you must know how to write a program to find the maximum number of students.
Now the school wants to count the number of students who enrolled in at least one course in each class and records the maximum number of students.
Each class uploaded 7 data, the number of students enrolled in course A in the class, the number of students enrolled in course B, the number of students enrolled in course C, the number of students enrolled in course AB, the number of students enrolled in course BC, the number of students enrolled in course AC, the number of students enrolled in course ABC. The school can calculate the number of students in this class based on these 7 data.
However, due to statistical errors, some data are wrong and these data should be ignored.
Smart you must know how to write a program to find the maximum number of students.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer N, indicates the number of class.
Then N lines follow, each line contains 7 data: a, b, c, d, e, f, g, indicates the number of students enrolled in A, B, C, AB, BC, AC, ABC in this class.
It's guaranteed that at least one data is right in each test case.
Limits
T≤100
1≤N≤100
0≤a,b,c,d,e,f,g≤100
Each case begins with one line with one integer N, indicates the number of class.
Then N lines follow, each line contains 7 data: a, b, c, d, e, f, g, indicates the number of students enrolled in A, B, C, AB, BC, AC, ABC in this class.
It's guaranteed that at least one data is right in each test case.
Limits
T≤100
1≤N≤100
0≤a,b,c,d,e,f,g≤100
Output
For each test case output one line contains one integer denotes the max number of students who enrolled in at least one course among N classes.
Sample Input
2 2 4 5 4 4 3 2 2 5 3 1 2 0 0 0 2 0 4 10 2 3 4 9 6 12 6 3 5 3 2
Sample Output
7 15HintIn the second test case, the data uploaded by Class 1 is wrong. Because we can't find a solution which satisfies the limitation. As for Class 2, we can calculate the number of students who only enrolled in course A is 2, the number of students who only enrolled in course B is 6, and nobody enrolled in course C, the number of students who only enrolled in courses A and B is 1, the number of students who only enrolled in courses B and C is 3, the number of students who only enrolled in courses A and C is 1, the number of students who enrolled in all courses is 2, so the total number in Class 2 is 2 + 6 + 0 + 1 + 3 + 1 + 2 = 15.
题解:有一些坑点注意下就好了,题目说有错误的数据。
ac code:
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<string>
//#include<iostream>
//#include<vector>
//#include<queue>
//#include<stack>
//#include<map>
//#include<set>
#define scand(x) scanf("%d",&x)
#define scandd(x,y) scanf("%d%d",&x,&y)
#define mst(a,zero) memset(a,zero,sizeof(a))
#define PARITY(value) (value&1)/*value>0*/
#define IsPowerOfTwo(n) ((!(n&(n-1)) ) && n)
#define INF 0x3f3f3f3f
#define intinf 1e9
#define llinf 1e18
#define eps 1e-8
#define PI acos(-1.0)
using namespace std;
typedef long long LL;
const int N=1e6+10;
const int M=1e6+10;
const int maxn=1e6+10;
const int mod=1e9+7;
int n;
int main(){
int T,a,b,c,d,e,f,g,sum,ans;
scand(T);
while(T--){
scand(n);
ans=0;
for (int i=0;i<n;i++){
sum=0;
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d%d",&d,&e,&f);
scanf("%d",&g);
if(d>a) continue;
if(d>b) continue;
if(e>b) continue;
if(e>c) continue;
if(f>a) continue;
if(f>c) continue;
if(g>a) continue;
if(g>b) continue;
if(g>c) continue;
if(g>d) continue;
if(g>e) continue;
if(g>f) continue;
if(a-d-f+g<0) continue;
if(b-d-e+g<0) continue;
if(c-e-f+g<0) continue;
sum=a+b+c-d-e-f+g;
if (ans<sum)ans=sum;
}
printf("%d\n",ans);
}
return 0;
}