头像

广acl




离线:12天前



广acl
27天前

import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int N=100010;
int n=in.nextInt();
long k=in.nextLong();
long cnt[]=new long [N];
long a[]=new long [N];
for(int i=1;i<=n;i) {
a[i]=in.nextLong();
a[i]+=a[i-1];
}
long res=0;
cnt[0]
;
for(int i=1;i<=n;i) {
res+=cnt[(int)(a[i]%k)];
cnt[(int)(a[i]%k)]
;
}
System.out.println(res);
}
}




广acl
27天前

import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int m=in.nextInt();

    int n=in.nextInt();int f[]=new int [m+1];
    for(int i=1;i<=n;i++) {
        int v=in.nextInt();
        int k=in.nextInt();
        int w=v*k;
        for(int j=m;j>=v;j--) {
            if(j>=v) {
                f[j]=Math.max(f[j],f[j-v]+w);
            }
        }
    }
    System.out.println(f[m]);
}

}




广acl
27天前

import java.util.;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int m=in.nextInt();
int n=in.nextInt();
int f[][]=new int [n+1][m+1];
int max=0;
for(int i=1;i<=n;i) {
int v=in.nextInt();
int k=in.nextInt();
for(int j=0;j<=m;j
) {
f[i][j]=f[i-1][j];
if(j>=v) {
f[i][j]=Math.max(f[i][j],f[i-1][j-v]+k
v);
}
if(max<f[i][j]) {
max=f[i][j];
}
}
}
System.out.println(f[n][m]);
}
}




广acl
27天前

import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int m=in.nextInt();
while(m–!=0) {
int n=in.nextInt();
int k=in.nextInt();
int a[][]=new int [n+1][k+1];
int f[][]=new int [n+1][k+1];
for(int i=1;i<=n;i) {
for(int j=1;j<=k;j
) {
a[i][j]=in.nextInt();
}
}
for(int i=1;i<=n;i) {
for(int j=1;j<=k;j
) {
f[i][j]+=Math.max(f[i][j-1], f[i-1][j])+a[i][j];
}
}
System.out.println(f[n][k]);
}
}
}




广acl
28天前

import java.util.*;
class Main{
static int a[];
static int n;
static int m;
static int b[];
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
b=new int [n];
a=new int [n];
for(int i=0;i<a.length;i) {
b[i]=in.nextInt();
}
f(0);
}
static boolean fg;
static boolean st[]=new boolean [1001000];
static boolean f(int k) {
if(k==n) {
fg=true;
if(m–==0) {
for(int i=0;i<n;i
) {
System.out.print(a[i]+” “);
}
return true;
}
return false;
}
for(int i=1;i<=n;i++) {
if(!st[i]) {
st[i]=true;
a[k]=i;
if(fg||a[k]==b[k]) {
if(f(k+1)) {
return true;
}
}
st[i]=false;
}
}
return false;
}
}




广acl
28天前

愚蠢的全排类
import java.util.*;
class Main{
static int a[];
static int n;
static int m;
static int b[];
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
b=new int [n+1];
a=new int [n+1];
for(int i=1;i<a.length;i) {
a[i]=in.nextInt();
}
f(0);
}
static boolean st[]=new boolean [100];
static void f(int k) {
if(k==n) {
while(m–==0) {
for(int i=1;i<=n;i
) {
System.out.print(a[i]+” “);
}
}
}
for(int i=k;i<a.length;i++) {
int t=a[i];
a[i]=a[k];
a[k]=t;
f(k+1);
t=a[i];
a[i]=a[k];
a[k]=t;
}
}
}

超时了 别看了




广acl
28天前

import java.util.*;

class Main{
static int N=1000100;
static int e[]=new int [N];
static int ne[]=new int [N];
static int idx;
static int head;
static void init() {
head=-1;
idx=0;
}
static void addhead(int x) {
e[idx]=x;
ne[idx]=head;
head=idx;
}
static void re(int k) {
ne[k-1]=ne[ne[k-1]];
}
static void add(int k,int x) {
e[idx]=x;
ne[idx]=ne[k-1];
ne[k-1]=idx
;
}
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int m=in.nextInt();
init();
while(m–!=0) {
String s=in.next();

        if(s.charAt(0)=='D') {
            int k=in.nextInt();
            if(k==0) {
                head=ne[head];
            }else {
                re(k);
            }
        }else if(s.charAt(0)=='I'){
            int k=in.nextInt();
            int x=in.nextInt();
            add(k,x);
        }else {
            int x=in.nextInt();
            addhead(x);
        }
    }
    for(int i=head;i!=-1;i=ne[i]) {
        System.out.print(e[i]+" ");
    }
}

}




广acl
28天前

import java.util.*;
class Main{
static int n;
static int m;
static int dx[]= {0,0,-1,1};
static int dy[]= {1,-1,0,0};
static class node{
int x;
int y;
int cnt;
char p;
public node(int x, int y, int cnt,char p) {
this.x = x;
this.y = y;
this.cnt = cnt;
this.p=p;
}

}
static char a[][];
static int vis[][];
public static void main(String[] args) {
    Scanner in =new Scanner(System.in);
    n=in.nextInt();
    a=new char [n][n];
    vis=new int [n][n];
    int ax=0;
    int ay=0;
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) {
            String s=in.next();
            a[i][j]=s.charAt(0);
            if(a[i][j]=='A') {
                ax=i;
                ay=j;
            }
            if(a[i][j]=='B') {
                bx=i;
                by=j;
            }
        }
    }
    bfs(ax,ay);
    if(flag) {
        System.out.println(-1);
    }
}
static int bx;
static int by;
static boolean flag=true;
static void bfs(int  ax,int ay) {
    Queue<node> q=new ArrayDeque<Main.node>();
    q.add(new node(ax,ay,0,'A'));
    vis[ax][ay]=1;
    while(!q.isEmpty()) {
        node u=q.peek();
        q.poll();
        vis[u.x][u.y]=1;
        if(u.x==bx&&u.y==by) {
            System.out.println(u.cnt);
            flag=false;
            break;
        }
        for(int i=0;i<dx.length;i++) {
            int x=u.x+dx[i];
            int y=u.y+dy[i];
            if(x>=0&&y>=0&&x<n&&y<n&&vis[x][y]==0&&a[u.x][u.y]!=a[x][y]) {
                vis[x][y]=1;
                q.add(new node(x,y,u.cnt+1,a[x][y]));
            }
        }
    }
}

}




广acl
28天前

import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int a[][]=new int [n+1][n+1];
int f[][]=new int [n+1][n+1];
for(int i=1;i[HTML_REMOVED]=1;i–) {
for(int j=1;j<n;j++) {
f[i][j]+=Math.max(f[i+1][j], f[i+1][j+1])+a[i][j];
}
}
System.out.println(f[1][1]);
}
}




广acl
28天前

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
BigInteger a=in.nextBigInteger();
System.out.print(a+” “);
while(a.compareTo(BigInteger.valueOf(1))==1) {
a=a.divide(BigInteger.valueOf(2));
System.out.print(a+” “);
}

}

}