AIM Tech Round (Div. 2)——ABCD
http://codeforces.com/contest/624
A.python是用来写div2的AB题的...
1 a, b, x, y = map(float, raw_input().split()) 2 print ((b - a) / (x + y))
B.思路很简单,但你的实现一样简短吗
1 n = input() 2 a = sorted(map(int, raw_input().split())) 3 s = 0 4 x = 10 ** 9 5 while a and x: 6 x = min(x, a.pop()) 7 s += x 8 x -= 1 9 print s
C.注意到只有abc三种字符
b是和abc都相邻的!就很ok!
跟n-1个都相邻的肯定涂b
然后随便选个连b的涂成a再dfs
剩下的涂c最后检验就行
1 import java.util.Scanner; 2 3 public class C { 4 static int[][] g = new int[505][505]; 5 static char[] s = new char[505]; 6 static int n; 7 static void dfs(int x) { 8 s[x] = 'a'; 9 for(int i = 1;i <= n;i ++) 10 if(g[x][i] == 1 && s[i] == 0) 11 dfs(i); 12 } 13 public static void main(String []args) { 14 Scanner cin = new Scanner(System.in); 15 n = cin.nextInt(); 16 int m = cin.nextInt(); 17 int[] u = new int[130005]; 18 int[] v = new int[130005]; 19 int[] c = new int[505]; 20 for(int i = 1;i <= m;i ++) { 21 u[i] = cin.nextInt(); 22 v[i] = cin.nextInt(); 23 g[u[i]][v[i]] = 1; 24 g[v[i]][u[i]] = 1; 25 c[u[i]] ++; 26 c[v[i]] ++; 27 } 28 boolean ok = false; 29 for(int i = 1;i <= n;i ++) 30 if(c[i] == n - 1) { 31 s[i] = 'b'; 32 ok = true; 33 } 34 if(!ok) dfs(1); 35 else { 36 for(int i = 1;i <= n;i ++) { 37 if(s[u[i]] == s[v[i]] && s[u[i]] == 'b') continue; 38 if(s[u[i]] == 'b' || s[v[i]] == 'b') { 39 if(s[u[i]] == 'b') dfs(v[i]); 40 else dfs(u[i]); 41 break; 42 } 43 } 44 } 45 for(int i = 1;i <= n;i ++) 46 if(s[i] == 0) 47 s[i] = 'c'; 48 for(int i = 1;i <= n;i ++) 49 for(int j = i + 1;j <= n;j ++) { 50 boolean x = (g[i][j] != 0); 51 boolean y = (s[i] == s[j] || s[i] - s[j] == 1 || s[j] - s[i] == 1); 52 if(x ^ y) { 53 System.out.println("No"); 54 System.exit(0); 55 } 56 } 57 System.out.println("Yes"); 58 for(int i = 1;i <= n;i ++) 59 System.out.print(s[i]); 60 } 61 }
注意也可能没有b,就选第一个点涂a,dfs下去就行了
D.显然a1,a1 + 1, a1 - 1, an, an + 1, an - 1
这六个数在最后的数列里至少出现了一个
最后数列的gcd肯定在这个数里...然后枚举这个gcd
开始DP,f[0/1/2][i]代表到第i个位置还没开始remove/正在remove/已经结束remove的最小代价
状态说清了,转移方程想一想咯
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 1000010; 6 7 typedef long long ll; 8 9 int n, a, b, c[maxn], v[maxn]; 10 11 ll f[3][maxn], ans = 1e18; 12 13 void dp(int x) { 14 memset(f, 0x3f3f3f3f, sizeof f); 15 f[0][0] = f[1][0] = f[2][0] = 0; 16 for(int i = 1;i <= n;i ++) { 17 if(c[i] % x == 0 || c[i] % x == 1 || c[i] % x == x - 1) { 18 f[0][i] = f[0][i - 1] + b * (c[i] % x != 0); 19 f[1][i] = f[1][i - 1] + b * (c[i] % x != 0); 20 } 21 f[2][i] = min(f[2][i - 1], f[0][i - 1]) + a; 22 f[1][i] = min(f[1][i], f[2][i]); 23 if(f[0][i] >= ans && f[1][i] >= ans) return; 24 } 25 ans = min(min(f[0][n], f[1][n]), ans); 26 } 27 28 void solve(int x) { 29 for(int i = 2;i * i <= x;i ++) 30 if(x % i == 0) { 31 if(!v[i]) dp(i); 32 v[i] = 1; 33 while(x % i == 0) x /= i; 34 } 35 if(x != 1) dp(x); 36 } 37 38 int main() { 39 scanf("%d %d %d", &n, &a, &b); 40 for(int i = 1;i <= n;i ++) 41 scanf("%d", &c[i]); 42 solve(c[1]), solve(c[1] - 1), solve(c[1] + 1); 43 solve(c[n]), solve(c[n] - 1), solve(c[n] + 1); 44 printf("%lld\n", ans); 45 return 0; 46 }