目录
B、龍
思路:根据题意,可发现,有两种操作解决:
1.通过操作一将数组翻转,所有的零放到一起,最后经过操作二将所有零转换为1。
2.直接通过操作二将所有区间的零转换为1。
二者取最小值即可。
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner cin= new FastScanner();
PrintWriter out= new PrintWriter(System.out);
int t=cin.nextInt();
while(t-->0) {
int n=cin.nextInt();
long a=cin.nextLong(),b=cin.nextLong();
int x=cin.nextInt();
long ans=0;
String s=cin.nextString();
char c[]=s.toCharArray();
long cnt=0;
for(int i=0;i<c.length;i++) {
if(c[i]=='0') {
int j=i;
while(j<c.length&&c[j]=='0') {
j++;
}
i=j-1;
cnt++;
}
}
ans=Math.min(a*(cnt-1)+b, b*cnt);
if(x>=ans) {
out.println("Yes");
out.println(x-ans);
}else {
out.println("No");
}
out.flush();
}
out.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
}
C、机智的我
import java.io.*;
import java.util.*;
public class Main {
static int N = 200010;
public static void main(String[] args) throws IOException {
FastScanner cin= new FastScanner();
PrintWriter out= new PrintWriter(System.out);
int n=cin.nextInt(),k=cin.nextInt();
if(k==0)out.println("Whatever");
else out.println("Why not");
out.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
}
D、疯狂星期八
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int n=cin.nextInt(),m=cin.nextInt();
int a[]=new int[n+1];
long f[]=new long[110];
for(int i=1;i<=n;i++) {
a[i]=cin.nextInt();
}
Arrays.fill(f, (long) 1e6);
f[0]=0;
for(int i=n;i>0;i--) {
for(int j=100;j>0;j--) {
f[j]=Math.min(f[j],f[j-1]+a[i]+i*(j-1));
}
}
for(int i=100;i>=0;i--) {
if(f[i]<=m) {
out.println(i);
out.flush();
break;
}
}
out.flush();
}
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
G、小明不希望太简单
思路:创建结构体,并结合优先队列,每一次都取当前最多的字母。
import java.io.*;
import java.util.*;
class r{
int id,cnt;
}
public class Main {
public static void main(String[] args) throws IOException {
FastScanner cin= new FastScanner();
PrintWriter out= new PrintWriter(System.out);
String s=cin.nextString();
char c[]=s.toCharArray();
r a[]=new r[26];
for(int i=0;i<26;i++) {
a[i]=new r();
a[i].id=i;
}
for(int i=0;i<c.length;i++) {
int id=c[i]-'a';
a[id].cnt++;
}
PriorityQueue<r> q=new PriorityQueue<>(new Comparator<r>() {
public int compare(r o1,r o2) {
return o2.cnt-o1.cnt;
}
});
for(int i=0;i<26;i++) {
if(a[i].cnt>0)q.add(a[i]);
}
StringBuffer sb=new StringBuffer();
while(!q.isEmpty()) {
r t=q.poll();
if(sb.length()==0||(sb.length()>0&&sb.charAt(sb.length()-1)!='a'+t.id)) {
sb.append((char)('a'+t.id));
t.cnt--;
if(t.cnt>0)q.add(t);
}else if(sb.length()>0&&sb.charAt(sb.length()-1)=='a'+t.id) {
r tt=q.poll();
sb.append((char)('a'+tt.id));
tt.cnt--;
if(tt.cnt>0)q.add(tt);
q.add(t);
}
}
out.println(sb.toString());
out.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
}
H、 防风台
思路:将矩阵中的所有数按大小顺序排列,然后遍历直到满足条件。
import java.io.*;
import java.util.*;
class r{
int x,y,v;
}
public class Main {
public static void main(String[] args) throws IOException {
FastScanner cin= new FastScanner();
PrintWriter out= new PrintWriter(System.out);
int n=cin.nextInt(),m=cin.nextInt();
r a[]=new r[n*m];
boolean st1[]=new boolean[n+1];
boolean st2[]=new boolean[m+1];
for(int i=0;i<n*m;i++) {
a[i]=new r();
}
int x=0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
a[x].x=i;
a[x].y=j;
a[x].v=cin.nextInt();
x++;
}
}
Arrays.sort(a,new Comparator<r>() {
public int compare(r o1,r o2) {
return o1.v-o2.v;
}
});
int ans=0,cnt=0;
for(int i=0;i<x;i++) {
if(cnt==n*m)break;
int xx=a[i].x,yy=a[i].y;
if(!st1[xx]) {
ans=a[i].v;
st1[xx]=true;
cnt++;
}
if(!st2[yy]) {
ans=a[i].v;
st2[yy]=true;
cnt++;
}
}
out.println(ans);
out.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
}
I、计算几何
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int t=cin.nextInt();
while(t-->0) {
double a=cin.nextDouble(),b=cin.nextDouble();
double ans=0;
for(double x=-10;x<=10;x+=0.01) {
for(double y=-10;y<=10;y+=0.01) {
if(x*x+y*y+x*y<=a) {
if(x*x+y*y-x*y<=b) {
if(a*a*x*x+b*b*y*y<=a*a*b*b) {
if(b*b*x*x+a*a*y*y<=a*a*b*b) {
ans+=0.0001;
}
}
}
}
}
}
if(ans%1>=0.5)ans+=1;
// out.println(ans+" "+ans%1);
out.println((int)ans);
out.flush();
}
out.flush();
}
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
J、最短路
思路:由题意可推出,先找出最小树,然后遍历最小树即可得到结果。最小树的查找需要先从小到大排序边,然后遍历边并通过并查集判断该边是否需要。(JAVA超时T-T,换c++才过,卑微JAVA人)
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,ans=0x3f3f3f3f;
int p[N];
bool st[N];
struct r{
int y,v;
};
struct rr{
int x,y,v;
}b[N];
vector<r> a[N];
bool cmp(rr x,rr y)
{
return x.v<y.v;
}
void find(int u, int v,int fx) {
st[u]=true;
for(int i=0;i<a[u].size();i++) {
r t=a[u][i];
if(st[t.y])continue;
st[t.y]=true;
if(t.y==v) {
ans=min(max(fx,t.v),ans);
}
else {
find(t.y,v,max(fx,t.v));
}
}
}
int check(int x) {
if(x==p[x])return x;
return p[x]=check(p[x]);
}
void solve() {
for(int i=0;i<m;i++) {
int x=b[i].x,y=b[i].y;
r t,tt;
t.y=y;tt.y=x;
t.v=tt.v=b[i].v;
int px=check(x),py=check(y);
if(px!=py) {
// cout<<x<<"---"<<y<<endl;
p[px]=py;
a[x].push_back(t);
a[y].push_back(tt);
}
}
}
int main(){
for(int i=0;i<N;i++) {
p[i]=i;
}
scanf("%d%d",&n,&m);
int x,y,v;
for(int i=0;i<m;i++) {
scanf("%d%d%d",&x,&y,&v);
b[i].x=x;b[i].y=y;b[i].v=v;
}
sort(b,b+m,cmp);
solve();
int q,u,vv;
scanf("%d",&q);
// for(int i=1;i<5;i++)cout<<a[i].size()<<" ";
// cout<<endl;
// cout<<"---"<<ans<<endl;
while(q-->0) {
memset(st,false,sizeof st);
scanf("%d%d",&u,&vv);
ans=0x3f3f3f3f;
if(u!=vv)find(u,vv,0);
else ans=0;
printf("%d\n",ans);
}
return 0;
}
L、___O___o______
思路:相切包括内切和外切。
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner cin= new FastScanner();
PrintWriter out= new PrintWriter(System.out);
int t=cin.nextInt();
while(t-->0) {
int a=cin.nextInt(),b=cin.nextInt();
int c=cin.nextInt(),d=cin.nextInt();
if(b==d&&a==c)out.println("NO");
else if(Math.abs(a*d-b*c)==1||a*d-b*c==0)out.println("YES");
else out.println("NO");
out.flush();
}
out.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
}