pta地址
A CCPC
判断给定的字符串中是否包含 ccpc 输出给定字符串
利用indexOf判断字符串中是否包含 ccpc 然后输出给定字符串即可
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String s = sc.next();
if (s.indexOf("CCPC") != -1)
out.println("Hello CCPC!");
else
out.println("This is not my CCPC");
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
B 道路
给定每个城市造路的费用和道路相通的城市 求最多修改k条路之后的修建道路的最小费用
怎么说呢,其实题目是讲了一开始给定的路是全部都是通的,也就是每个城市否能到达另外任意一个城市。
也就是这句↓
要是不能理解这句的话 就会很难想之后如何去完成了
这是题目的一个小细节 能明白的话 这题就很简单了
然后就很简单了
因为一开始每个城市都能到达另外任意一个城市 所以我们只需要修改价格最大的路换成最小的即可
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
int n = ini(), k = ini();
int shu[] = new int[n + 10], min[] = new int[n + 10];
for(int i = 1; i <= n; i ++)
shu[i] = ini();
for(int i = 1; i < n; i ++)
{
int a = ini(), b = ini();
min[i] = Math.min(shu[a], shu[b]);
}
Arrays.sort(min, 1, n);
for(int i = n - 1; i > n - 1 - k; i --)
min[i] = min[1];
long ans = 0;
for(int i = 1; i < n; i ++)
ans += min[i];
out.println(ans);
out.flush();
out.close();
}
static StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int ini() throws IOException
{
sc.nextToken();
return (int) sc.nval;
}
}
D 函数
给定一个二元一次方程 与 一个区间
求这个区间内的最大最小值
判断 a
a = 0 时,这是一个一次方程
判断 b
b > 0 递增的一次函数
b = 0 一条直线
b < 0 递减的一次函数
最大最小值 直接范围两边的点带入求最大最小即可
a != 0 时,这是一个二次函数
a > 0 向下凹的直线
最小值: 最靠近对称轴
−
b
2
a
-\frac{b}{2a}
−2ab且在范围的点
最大值: 范围两边的点带入 求最大值即可
a < 0 向上凸的直线
最大值: 最靠近对称轴
−
b
2
a
-\frac{b}{2a}
−2ab且在范围的点
最小值: 范围两边的点带入 求最小值即可
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static long a, b, c;
static long Fx(long x)
{
return a * x * x + b * x + c;
}
public static void main(String[] args) throws IOException
{
int t = ini();
while(t -- > 0)
{
a = ini(); b = ini(); c = ini();
long l = ini(), r = ini();
long L = Fx(l), R = Fx(r);
if(a == 0)
out.println(Math.min(L, R) + " " + Math.max(L, R));
else
{
double mid = b / (-2.0 * a);
long midl = (long) Math.ceil(mid), midr = (long) Math.floor(mid);
if(a > 0)
{
if(l <= mid && mid <= r)
out.println(Math.min(Fx(midl), Fx(midr)) + " " + Math.max(L, R));
else
out.println(Math.min(L, R) + " " + Math.max(L, R));
}
else
{
if(l <= mid && mid <= r)
out.println(Math.min(L, R) + " " + Math.max(Fx(midl), Fx(midr)));
else
out.println(Math.min(L, R)+ " " + Math.max(L, R));
}
}
}
out.flush();
out.close();
}
static StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int ini() throws IOException
{
sc.nextToken();
return (int) sc.nval;
}
}
E 二进制
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static int count(int x)
{
int ans = 0;
for(int i = x; i > 0; i &= i - 1)
ans ++;
return ans;
}
public static void main(String[] args) throws IOException
{
int t = sc.nextInt();
while(t -- > 0)
{
int x = sc.nextInt();
int cnt = count(x);
if(cnt % 2 == 1)
{
cnt = count(x + 1);
x ++;
if(cnt % 2 == 1)
x ++;
}
out.println(x);
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
H 修复括号序列
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static boolean check()
{
int n = sc.nextInt();
char s[] = (" " + sc.next()).toCharArray();
if((n & 1) == 1)
return false;
int a = 0;
boolean vis = false;
for(int i = 1; i <= n; i ++)
{
a += s[i] == '(' ? 1 : -1;
if(a < 0)
{
if(!vis)
{
vis = true;
a += 2;
}
else
return false;
}
}
if(!vis && (a == 0 || a == 2))
return true;
return a == 0;
}
public static void main(String[] args) throws IOException
{
out.println(check() ? "Yes" : "No");
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
I 石子游戏
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
int t = sc.nextInt();
while(t -- > 0)
{
long n = sc.nextLong();
long x = (long) Math.sqrt(n);
if(x <= n && n < x * x + x)
out.println("Alice");
else
out.println("Bob");
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}