文章目录
题目链接
知识一览
01-数论(F题)
02-动态规划(I题)
03-子串倒置/字典序/前缀
题目列表
快输
static class FastReader{
BufferedReader br;
StringTokenizer st;
String tmp;
public FastReader() {
br=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st==null||!st.hasMoreElements()) {
try {
st=new StringTokenizer(br.readLine());
}catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong(){return Long.parseLong(next());}
String nextLine() {
String str="";
try {
str=br.readLine();
}catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext(){
if(st!=null&&st.hasMoreTokens())return true;
try {
tmp=br.readLine();
st=new StringTokenizer(tmp);
}catch(IOException e) {
return false;
}
return true;
}
}
static PrintWriter out=new PrintWriter(
new BufferedWriter(new OutputStreamWriter(System.out)));
static FastReader sc=new FastReader();
F Sum of Numerators(分子之和)
public static void main(String[] args) {
int t=sc.nextInt();
while(t-->0) {
long n=sc.nextLong(),k=sc.nextLong();
long sum=n*(n+1)/2;
while(k>0){
k--;
n/=2;
if(n==0)break;
sum-=n*(n+1)/2;
}
out.println(sum);
out.flush();
}
}
I Array Division(数组划分)
题意:对两个数组进行相同的划分,划分得到的每一个子数组中,a数组中的元素之和大于等于b数组中的元素之和。若可以进行这样的划分,则输出最大组数;否则输出-1。
public static void main(String[] args) {
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
//a & b 数组前缀和
long a[]=new long[n+1],b[]=new long[n+1];
for(int i=1;i<=n;i++){
a[i]=sc.nextLong();a[i]+=a[i-1];
}
for(int i=1;i<=n;i++){
b[i]=sc.nextLong();b[i]+=b[i-1];
}
if(a[n]<b[n]){
out.println(-1);out.flush();
continue;
}
long dp[]=new long[n+1];
for(int i=1;i<=n;i++){
//前i个数字还没有进行划分,但可以被分为一组(a[i]>=b[i])
if(dp[i]==0&&a[i]>=b[i])
dp[i]=1;
for (int j=i-1;j>=0;j--){
//若dp[j]=0,则前j个数字不能进行划分
if((a[i]-a[j])>=(b[i]-b[j])&&dp[j]!=0){
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
}
out.println(dp[n]);
out.flush();
}
}
J - Substring Inversion (Easy Version)(子字符串反转(简易版))
找符合以下条件的元组(a,b,c,d) 个数
①1≤a≤b≤n
②1≤c≤d≤n
③a < c
④s[a:b] is lexicographically greater(字典序靠前) than s[c:d]
static int mod=(int)1e9+7;
static int n;
public static void main(String[] args) {
int T=sc.nextInt();
//int T=1;
while(T-->0){
solve();
}
}
private static void solve(){
n=sc.nextInt();
char s[]=(sc.next()).toCharArray();
long ans=0;
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
int a=i,b=j;
while (b<n&&s[a]==s[b]) {
ans=(ans+n-a-1)%mod;
a++;b++;
}
if(b>=n){
a--;b--;
}
if (s[a]>s[b]) {
ans+=(n-a)*(n-b)%mod;
ans%=mod;
}
}
}
out.println(ans);
out.flush();
}