ADV-282 Island Hopping
cpp:
#include
#define int long long
using namespace std;
const int INF = (1LL << 62);
vector<pair<pair<int, int>, int> > vec;
vector<vector<int> > G;
vector<vector<int> > adj;
vector<int> dist;
vector<int> pre;
vector<int> max_edge;
vector<int> mark;
int Dist(pair<int, int> a, pair<int, int> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
void dfs(int u, double& s1, double& s2) {
for (int i = 0; i < (int)adj[u].size(); i++) {
int v = adj[u][i];
s1 += ((double)sqrt(max_edge[v]) * (double)vec[v].second);
s2 += (double)vec[v].second;
dfs(v, s1, s2);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
int tt = 1;
while (cin >> n) {
if (n == 0) {
break;
}
vec.clear();
dist.resize(n);
pre.resize(n);
max_edge.resize(n);
mark.resize(n, 0);
G.resize(n);
for (int i = 0; i < n; i++) {
G[i].resize(n);
}
adj.resize(n);
for (int i = 0; i < n; i++) {
int x, y, m;
cin >> x >> y >> m;
vec.push_back(make_pair(make_pair(x, y), m));
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
G[i][j] = Dist(vec[i].first, vec[j].first);
}
}
int pos = 0;
mark[pos] = 1;
for (int i = 0; i < n; i++) {
dist[i] = G[pos][i];
max_edge[i] = G[pos][i];
pre[i] = pos;
}
double s1 = 0.0;
double s2 = vec[pos].second;
for (int i = 1; i < n; i++) {
int Min_dist = INF;
int Max_Peo = 0;
for (int j = 0; j < n; j++) {
if (mark[j] == 0 && Min_dist > dist[j]) {
Min_dist = dist[j];
Max_Peo = vec[j].second;
pos = j;
} else if (mark[j] == 0 && Min_dist == dist[j] &&
Max_Peo < vec[j].second) {
Max_Peo = vec[j].second;
pos = j;
}
}
max_edge[pos] = max(max_edge[pre[pos]], dist[pos]);
mark[pos] = 1;
adj[pre[pos]].push_back(pos);
for (int j = 0; j < n; j++) {
if (mark[j] == 0 && G[pos][j] < dist[j]) {
dist[j] = G[pos][j];
pre[j] = pos;
} else if (mark[j] == 0 && G[pos][j] == dist[j] &&
max_edge[pos] < max_edge[pre[j]]) {
pre[j] = pos;
}
}
}
dfs(0, s1, s2);
printf("Island Group: %d Average %.2f\n\n", tt, s1 / s2);
tt++;
dist.clear();
pre.clear();
max_edge.clear();
mark.clear();
G.clear();
adj.clear();
}
return 0;
}
java:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int tt = 1;
while(true) {
int num = in.nextInt();
if(num==0) {
break;
}
int arr1[][] = new int[num][3];
for(int i = 0 ; i < num ; i++) {
for(int j = 0 ; j < 3 ;j++) {
arr1[i][j]=in.nextInt();
}
}
double arr2[][] = new double[num][num];
for(int i = 0 ; i < num ; i++) {
for(int j = 0 ; j < num ;j++) {
arr2[i][j]=(Math.sqrt((arr1[i][0]-arr1[j][0])*(arr1[i][0]-arr1[j][0])+(arr1[i][1]-arr1[j][1])*(arr1[i][1]-arr1[j][1])));
}
}
int arris[] = new int[num];
int tempi = num;
double templ = Math.pow(2,31);
int parent=0;
int me = 0;
double arr3[][]=new double[num][3];
arris[0]=-1;
while(tempi!=1){
templ = Math.pow(2,31);
for(int i = 0;i<num;i++ ) {
if(arris[i]==0) {
continue;
}
for(int j = 0 ; j < num ;j++) {
if(arris[j]!=0) {
continue;
}
if(templ>arr2[i][j]) {
templ=arr2[i][j];
me = j;
parent =i;
}
}
}
arr3[me][0]=me;
arr3[me][1]=templ;
arr3[me][2]=parent;
arris[me]=-1;
tempi--;
}
double sum =0;
int people=0;
for(int i = 0 ; i < num ; i++) {
sum +=(fun1(arr3,i)*arr1[i][2]);
people +=arr1[i][2];
}
double answer = Math.round(sum/people * 100) * 0.01d;
System.out.printf("Island Group: %d Average %.2f\n\n", tt++, answer);
}
}
public static double fun1(double arr3[][],int i) {
double max = arr3[i][1];
int parent = (int)arr3[i][2];
while(parent!=0) {
if(max<arr3[parent][1]) {
max=arr3[parent][1];
}
parent = (int)arr3[parent][2];
}
return max;
}
}
ADV-283 矩形靶
cpp:
#include
#include
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
int N, M, L, R;
char target[605][605] = {0}, state[605][605] = {0};
scanf("%d %d %d %d", &N, &M, &L, &R);
for (int i = 0; i < N; ++i) {
scanf("%s", target[i]);
strcpy(state[i], target[i]);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (target[i][j] == '1') {
for (int s = max(0, i - L); s <= min(N - 1, i + L); ++s)
for (int r = max(0, j - R); r <= min(M - 1, j + R); ++r)
state[s][r] = '1';
}
}
}
for (int i = 0; i < N; ++i) printf("%s\n", state[i]);
return 0;
}
java:
import java.util.Scanner;
public
class Main {
public
static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
char[][] str = new char[n][m];
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.nextLine();
if (s.equals("")) {
s = sc.nextLine();
}
str[i] = s.toCharArray();
a[i] = s.toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (str[i][j] == '1') {
for (int i1 = i - l < 0 ? 0 : i - l; i1 <= i + l && i1 < n;
i1++) {
for (int j1 = j - r < 0 ? 0 : j - r;
j1 <= j + r && j1 < m; j1++) {
a[i1][j1] = '1';
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
ADV-284 GPA
cpp:
#include
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int maxc = 26;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 5e-3;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return f * x;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll poww(ll x, ll y) {
ll ans = 1;
while (y) {
if (y & 1) ans = ans * x;
x = x * x;
y >>= 1;
}
return ans;
}
int n, s;
char c[10];
int main() {
cin >> n;
int fz = 0, fm = 0;
while (n--) {
cin >> s;
scanf("%s", c);
if (c[0] == 'N' || c[0] == 'P')
continue;
else {
fz += s * atoi(c);
fm += s;
}
}
double g1 = fz * 1.0 / fm;
cin >> n;
fz = fm = 0;
while (n--) {
cin >> s;
scanf("%s", c);
if (c[0] == 'N' || c[0] == 'P')
continue;
else {
fz += s * atoi(c);
fm += s;
}
}
double g2 = fz * 1.0 / fm;
if (abs(g1 - g2) < eps)
cout << "0.00" << endl;
else
printf("%.2lf\n", g1 - g2);
return 0;
}
java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int Si = 0;
int SC = 0;
for(int i=0;i<n;i++) {
int si = sc.nextInt();
int ci = 0;
String s = sc.next();
if(s.equals("N") || s.equals("P")) continue;
ci = Integer.parseInt(s);
Si += si;
SC += si*ci;
}
double GPA1 = 1.0*SC/Si;
n = sc.nextInt();
Si = 0;
SC = 0;
for(int i=0;i<n;i++) {
int si = sc.nextInt();
int ci = 0;
String s = sc.next();
if(s.equals("N") | s.equals("P")) continue;
ci = Integer.parseInt(s);
Si += si;
SC += si*ci;
}
double GPA2 = 1.0*SC/Si;
double ans = GPA1-GPA2;
String s = String.format("%.2f",ans);
if(s.equals("-0.00")) s="0.00";
System.out.println(s);
}
}
ADV-285 套正方形
cpp:
#include
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int maxc = 26;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 5e-3;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return f * x;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll poww(ll x, ll y) {
ll ans = 1;
while (y) {
if (y & 1) ans = ans * x;
x = x * x;
y >>= 1;
}
return ans;
}
int n;
char s[55][55];
int main() {
cin >> n;
int l = 1, wid = n;
while (wid >= 2) {
for (int i = l; i <= l + wid - 1; i++) {
for (int j = l; j <= l + wid - 1; j++) {
if (i == l || i == l + wid - 1 || j == l || j == l + wid - 1)
s[i][j] = '*';
}
}
l += 2;
wid -= 4;
}
for (int i = 1; i <= n; i++) s[i][n + 1] = '\0';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%c", s[i][j]);
if (j == n) printf("%c", '\n');
}
}
return 0;
}
java:
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
ArrayList<String> list = new ArrayList<>();
StringBuffer s = new StringBuffer("");
for (int i = 0; i < n; i++)
s = s.append("*");
int l = 1, r = n - 2;
String current = " ";
list.add(s.toString());
for (int i = 1; i < n / 2; i++) {
for (int j = l; j < r + 1; j++) {
s.replace(j, j + 1, current);
}
l++;
r--;
if (current.equals(" ")) {
current = "*";
} else {
current = " ";
}
list.add(s.toString());
}
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
ADV-286 Channel
cpp:
#include
#include
using namespace std;
void hang(int N, int x, int y)
{
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
if (i == x) cout << "(" << i << "," << j << ")";
cout << endl;
}
void lie(int N, int x, int y)
{
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
if (j == y) cout << "(" << i << "," << j << ")";
cout << endl;
}
void zuoxiayoushang(
int N, int x,
int y)
{
for (int i = N; i >= 1; i--)
{
for (int j = N; j >= 1; j--) {
for (int k = -N; k <= N; k++)
{
if (i - x == k && j - y == -k) {
cout << "(" << i << "," << j << ")";
}
}
}
}
cout << endl;
}
void zuoshangyouxia(
int N, int x,
int y)
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++) {
for (int k = -N; k <= N; k++)
{
if (i - x == k && j - y == k) {
cout << "(" << i << "," << j << ")";
}
}
}
}
cout << endl;
}
int main() {
int N, x, y;
while (cin >> N >> x >> y) {
hang(N, x, y);
lie(N, x, y);
zuoshangyouxia(N, x, y);
zuoxiayoushang(N, x, y);
}
return 0;
}
ADV-287 Monday-Saturday质因子
cpp:
#include
using namespace std;
bool ms(int x) {
if (x % 7 == 1 || x % 7 == 6)
return 1;
else
return 0;
}
bool msy(int a, int b) {
if (b % a)
return 0;
else {
int x = b / a;
if (ms(x))
return 1;
else
return 0;
}
}
bool msz(int x) {
for (int i = 2; i < x; i++)
if (msy(i, x)) return 0;
return 1;
}
int main() {
int n;
while (cin >> n) {
if (n == 1) break;
cout << n << ":";
for (int i = 6; i <= n; i++) {
if (ms(i)) {
if (msy(i, n) && msz(i)) cout << " " << i;
}
}
cout << endl;
}
return 0;
}
java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num[]=new int[100];
int p=0;
while(true) {
int nn=sc.nextInt();
if(nn!=1) {
num[p]=nn;
p++;
}else {
break;
}
}
for(int i=0;i<p;i++) {
int number=num[i];
ArrayList<Integer> al=new ArrayList<Integer>();
for(int j=7;j-1<=Math.sqrt(number);j=j+7) {
int a=j-1;
if(number%a==0) {
if((number/a)%7==1||(number/a)%7==6) {
if(isZhi(a)) {
al.add(a);
}
if(isZhi(number/a)&&(number/a)!=a) {
al.add(number/a);
}
}
}
int b=j+1;
if(b<=Math.sqrt(number)) {
if(number%b==0) {
if((number/b)%7==1||(number/b)%7==6) {
if(isZhi(b)) {
al.add(b);
}
if(isZhi(number/b)&&(number/b)!=b) {
al.add(number/b);
}
}
}
}
}
if(isZhi(number)) {
al.add(number);
}
Collections.sort(al);
System.out.print(number+": ");
for(int k=0;k<al.size();k++) {
System.out.print(al.get(k)+" ");
}
System.out.println("");
}
}
static boolean isZhi(int num) {
boolean flag=true;
for(int i=7;i<num;i=i+7) {
int a=i-1;
int b=i+1;
if(num%a==0&&num!=a) {
if((num/a)%7==1||(num/a)%7==6) {
flag=false;
break;
}
}
if(num%b==0&&num!=b) {
if((num/b)%7==1||(num/b)%7==6) {
flag=false;
break;
}
}
}
return flag;
}
}
ADV-288 成绩排名
cpp:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace lgs {
long long pow(long long a, long long n) {
long long sum = 1;
while (n) {
if (n & 1) sum = a * sum;
a = a * a;
n >> 1;
}
return sum;
}
}
using namespace std;
using lgs::pow;
int prime(int x) {
int i, k = 1;
for (i = 2; i <= sqrt(x); i++) {
if (x % i == 0) {
k = 0;
break;
}
}
return k;
}
long long gcd(long long x, long long y) {
if (y == 0) return x;
return gcd(y, x % y);
}
const double pi = 3.1415926536;
string a[100] = {"ling", "yi", "er", "san", "si", "wu",
"liu", "qi", "ba", "jiu", "shi"};
string b[10] = {"", "", "bai", "qian", "wan",
"shi wan", "bai wan", "qian wan", "yi"};
struct student {
string name;
int grade;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
struct student s[105];
int i, n, j;
cin >> n;
for (i = 0; i < n; i++) {
cin >> s[i].name >> s[i].grade;
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++) {
if ((s[j].grade < s[j + 1].grade) ||
(s[j].grade == s[j + 1].grade && (s[j].name > s[j + 1].name)))
swap(s[j], s[j + 1]);
}
for (i = 0; i < n; i++) {
cout << s[i].name << endl;
}
return 0;
}
java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Object o[]=new Object[n];
for(int i=0;i<n;i++) {
String s=sc.next();
int na=sc.nextInt();
student stu=new student(s,na);
o[i]=stu;
}
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
student stu1=(student) o[i];
student stu2=(student) o[j];
if(stu1.score<stu2.score) {
Object ob=o[i];
o[i]=o[j];
o[j]=ob;
}else if(stu1.score==stu2.score) {
if(stu1.name.charAt(0)>stu2.name.charAt(0)) {
Object ob=o[i];
o[i]=o[j];
o[j]=ob;
}
}
}
}
for(int i=0;i<n;i++) {
student stu=(student) o[i];
System.out.println(stu.name);
}
}
static class student{
String name;
int score;
public student(String n,int s) {
name=n;
score=s;
}
}
}
ADV-289 双十一抢购
cpp:
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
int dis[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
struct Node {
int val;
int c;
int id;
int profit;
} node[maxn];
bool cmp(Node a, Node b) {
if (a.profit != b.profit) {
return a.profit > b.profit;
}
if (a.profit == b.profit && a.val != b.val)
return a.val < b.val;
else
return a.id < b.id;
}
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
double sum;
int n;
cin >> sum >> n;
for (int i = 1; i <= n; i++) {
cin >> node[i].val >> node[i].c;
if (node[i].c == 1)
node[i].profit = node[i].val;
else
node[i].profit = 0;
node[i].id = i;
}
sort(node + 1, node + n + 1, cmp);
set<int> s;
for (int i = 1; i <= n; i++) {
if (node[i].c == 1 && sum >= (double)(node[i].val) * 0.5) {
sum -= double(node[i].val) * 0.5;
s.insert(node[i].id);
}
if (node[i].c == 0 && sum >= (double)(node[i].val) * 1.0) {
sum -= (double)(node[i].val) * 1.0;
s.insert(node[i].id);
}
}
set<int>::iterator it;
if (s.empty())
cout << "0";
else {
for (it = s.begin(); it != s.end(); it++) cout << *it << " ";
}
return 0;
}
java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public
class Main {
public
static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int s = reader.nextInt();
int n = reader.nextInt();
ArrayList<Wupin> list = new ArrayList<Wupin>();
ArrayList<Integer> xvhao = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
Wupin w = new Wupin(reader.nextInt(), reader.nextInt(), i + 1);
list.add(w);
}
Collections.sort(list);
double zongjia = 0;
for (int i = n - 1; i >= 0; i--) {
zongjia += list.get(i).xianjia;
if (zongjia <= s) {
xvhao.add(list.get(i).number);
} else {
zongjia -= list.get(i).xianjia;
}
}
if (xvhao.size() == 0) {
System.out.println(0);
} else {
Collections.sort(xvhao);
for (int i : xvhao) {
System.out.print(i + " ");
}
}
}
}
class Wupin implements Comparable<Wupin> {
int a;
int b;
int number;
double xianjia;
public
Wupin(int a, int b, int number) {
super();
this.a = a;
this.b = b;
this.number = number;
setxianjia();
}
public
Wupin() { super(); }
public
void setxianjia() {
if (b == 0) {
xianjia = a;
} else {
xianjia = a / 2.0;
}
}
public
int compareTo(Wupin o) {
if (b > o.b) {
return 1;
} else if (b < o.b) {
return -1;
} else {
if (b == 1) {
if (a > o.a) {
return 1;
} else if (a < o.a) {
return -1;
} else {
if (number < o.number) {
return 1;
} else {
return -1;
}
}
} else {
if (a < o.a) {
return 1;
} else if (a > o.a) {
return -1;
} else {
if (number < o.number) {
return 1;
} else {
return -1;
}
}
}
}
}
}
ADV-290 成绩排序
cpp:
#include
#define max 100
typedef struct {
int math;
int Engith;
int child;
int num;
} linklist;
void Insert(linklist a[], int n) {
int i, j;
int temp;
int path[max] = {0};
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j].math < a[j + 1].math) {
temp = a[j].math;
a[j].math = a[j + 1].math;
a[j + 1].math = temp;
temp = a[j].Engith;
a[j].Engith = a[j + 1].Engith;
a[j + 1].Engith = temp;
temp = a[j].child;
a[j].child = a[j + 1].child;
a[j + 1].child = temp;
temp = a[j].num;
a[j].num = a[j + 1].num;
a[j + 1].num = temp;
} else if (a[j].math == a[j + 1].math &&
a[j].Engith < a[j + 1].Engith) {
temp = a[j].math;
a[j].math = a[j + 1].math;
a[j + 1].math = temp;
temp = a[j].Engith;
a[j].Engith = a[j + 1].Engith;
a[j + 1].Engith = temp;
temp = a[j].child;
a[j].child = a[j + 1].child;
a[j + 1].child = temp;
temp = a[j].num;
a[j].num = a[j + 1].num;
a[j + 1].num = temp;
} else if (a[j].math == a[j + 1].math &&
a[j].Engith == a[j + 1].Engith &&
a[j].child < a[j + 1].child) {
temp = a[j].math;
a[j].math = a[j + 1].math;
a[j + 1].math = temp;
temp = a[j].Engith;
a[j].Engith = a[j + 1].Engith;
a[j + 1].Engith = temp;
temp = a[j].child;
a[j].child = a[j + 1].child;
a[j + 1].child = temp;
temp = a[j].num;
a[j].num = a[j + 1].num;
a[j + 1].num = temp;
}
}
}
}
int main() {
linklist a[max];
int n;
int i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d%d", &a[i].math, &a[i].Engith, &a[i].child);
a[i].num = i + 1;
}
Insert(a, n);
for (i = 0; i < n; i++)
printf("%d %d %d %d\n", a[i].math, a[i].Engith, a[i].child, a[i].num);
return 0;
}
java:
import java.util.*;
import java.io.*;
public class Main {
static class Student implements Comparable<Student>{
int math;
int english;
int chinese;
int id;
public Student(int math,int english,int chinese,int id) {
this.math=math;
this.english=english;
this.chinese=chinese;
this.id=id;
}
@Override
public int compareTo(Student o) {
if(this.math!=o.math)
return this.math>o.math?-1:1;
else if(this.english!=o.english)
return this.english>o.english?-1:1;
else if(this.chinese!=o.chinese)
return this.chinese>o.chinese?-1:1;
else
return this.id>o.id?-1:1;
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Student> al = new ArrayList<Student>();
int n = Integer.parseInt(br.readLine());
for(int i=0;i<n;i++) {
String[] each_line = br.readLine().split(" ");
al.add(new Student(Integer.parseInt(each_line[0]),Integer.parseInt(each_line[1]),Integer.parseInt(each_line[2]),i+1));
}
Collections.sort(al);
for(int i=0;i<al.size();i++) {
System.out.println(al.get(i).math+" "+al.get(i).english+" "+al.get(i).chinese+" "+al.get(i).id);
}
}
}
ADV-291 成绩排序2
cpp:
#include
#include
using namespace std;
int n;
struct node {
int m, e, c;
int num, sum;
};
node a[1010];
bool cmp(node a, node b) {
if (a.sum > b.sum)
return true;
else if (a.sum == b.sum) {
if (a.m > b.m)
return true;
else if (a.m == b.m) {
if (a.e > b.e)
return true;
else if (a.e == b.e) {
if (a.c > b.c)
return true;
else if (a.c == b.c) {
if (a.num < b.num) return true;
}
}
}
}
return false;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
a[i].num = i + 1;
cin >> a[i].m >> a[i].e >> a[i].c;
a[i].sum = a[i].m + a[i].e + a[i].c;
}
sort(a, a + n, cmp);
for (int i = 0; i < n; i++)
cout << a[i].m << " " << a[i].e << " " << a[i].c << " " << a[i].num
<< endl;
return 0;
}
java:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static class student{
int Math;
int English;
int Chinese;
int sum;
int num;
public student(int m,int e,int c,int s,int n) {
Math=m;
English=e;
Chinese=c;
sum=s;
num=n;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Object o[]=new Object[n];
for(int i=0;i<n;i++) {
int m=sc.nextInt();
int e=sc.nextInt();
int c=sc.nextInt();
student s=new student(m,e,c,m+e+c,i+1);
o[i]=s;
}
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
student student1=(student) o[i];
student student2=(student) o[j];
if(student1.sum<student2.sum) {
Object ob=o[i];
o[i]=o[j];
o[j]=ob;
}else if(student1.sum==student2.sum) {
if(student1.Math<student2.Math) {
Object ob=o[i];
o[i]=o[j];
o[j]=ob;
}else if(student1.Math==student2.Math) {
if(student1.English<student2.English) {
Object ob=o[i];
o[i]=o[j];
o[j]=ob;
}else if(student1.English==student2.English) {
if(student1.num>student2.num) {
Object ob=o[i];
o[i]=o[j];
o[j]=ob;
}
}
}
}
}
}
for(int i=0;i<n;i++) {
student st=(student) o[i];
System.out.println(st.Math+" "+st.English+" "+st.Chinese+" "+st.num);
}
}
}
ADV-292 计算行列式
cpp:
#include
#include
using namespace std;
double det(int n, double *aa) {
if (n == 1) return aa[0];
double *bb = new double[(n - 1) * (n - 1)];
int mov = 0;
double sum = 0.0;
for (int arow = 0; arow < n; arow++)
{
for (int brow = 0; brow < n - 1;
brow++)
{
mov =
arow > brow
? 0
: 1;
for (int j = 0; j < n - 1; j++)
{
bb[brow * (n - 1) + j] = aa[(brow + mov) * n + j + 1];
}
}
int flag =
(arow % 2 == 0
? 1
: -1);
sum += flag * aa[arow * n] *
det(n - 1, bb);
}
delete[] bb;
return sum;
}
int main() {
int n = 0;
scanf("%d", &n);
double *aa = new double[n * n];
for (int i = 0; i < n * n; i++) cin >> aa[i];
printf("%.f\n", det(n, aa));
delete[] aa;
return 0;
}
java:
import java.io.*;
public class Main{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
public static int Int(String s){return Integer.parseInt(s);}
public static void copy(int[][]A, int[][] A1, int i, int len) throws IOException{
for(int x = 1; x < len; x++)
for(int y = 0, j = 0; j < len; j++)
if(j != i) {
A1[x-1][y++] = A[x][j];
}
}
public static int F(int[][] A, int len)throws Exception{
int res = 0;
if(len == 1)return A[0][0];
if(len == 2){
return A[0][0]*A[1][1] - A[0][1]*A[1][0];
}
else{
int A1[][] = new int[10][10];
for(int i = 0; i < len; i++){
copy(A, A1, i, len);
res += Math.pow(-1, i) * A[0][i] * F(A1, len-1);
}
}
return res;
}
public static void main(String[] args) throws Exception{
int n;
n = Integer.parseInt(in.readLine());
int arr[][] = new int[10][10];
for(int i = 0; i < n; i++){
String[] s = in.readLine().split(" ");
for(int j = 0; j < n; j++){
arr[i][j] = Int(s[j]);
}
}
out.write(F(arr, n) + "\n");
out.flush();
}
}
ADV-293 最大值路径
cpp:
#include
using namespace std;
int n;
int *p;
int maxx = 0;
int countb = 1;
void dps(int x, int y, int sum) {
if (x < 0 || y < 0 || x >= n || y >= n) return;
sum += p[x * n + y];
if (x == 0 && y == n - 1) {
if (sum > maxx) {
maxx = sum;
countb = 1;
} else if (sum == maxx)
countb++;
}
dps(x - 1, y, sum);
dps(x, y + 1, sum);
}
int main() {
cin >> n;
p = new int[n * n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> p[i * n + j];
dps(n - 1, 0, 0);
cout << countb << " " << maxx;
return 0;
}
java:
import java.util.Scanner;
public class Main {
private static int n;
private static int[][] dp;
private static int result=1;
private static void total(int x, int y) {
if(x+1>n-1||y-1<0){
return;
}
if(x==n-1&&y==0){
return;
}
if (dp[x][y - 1] > dp[x + 1][y]) {
total(x, y - 1);
} else if (dp[x][y - 1] < dp[x + 1][y]) {
total(x + 1, y);
}else{
result++;
total(x, y - 1);
total(x + 1, y);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = scanner.nextInt();
}
}
dp = new int[n][n];
dp[n - 1][0] = arr[n - 1][0];
for (int i = n - 2; i >= 0; i--) {
dp[i][0] = arr[i][0] + dp[i + 1][0];
}
for (int i = 1; i < n; i++) {
dp[n - 1][i] = arr[n - 1][i] + dp[n - 1][i - 1];
}
for (int i = n - 2; i >= 0; i--) {
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]) + arr[i][j];
}
}
total(0,n-1);
System.out.println(result + " " + dp[0][n - 1]);
}
}
ADV-294 最长滑雪道
cpp:
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 105;
int a[maxn][maxn];
int dp[maxn][maxn];
struct node {
int x, y, h;
} A[maxn * maxn];
bool cmp(node a, node b) { return a.h < b.h; }
int main() {
int n, c;
while (scanf("%d%d", &n, &c) == 2) {
memset(dp, 0, sizeof(dp));
memset(a, 0, sizeof(a));
int num = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= c; j++) {
scanf("%d", &a[i][j]);
A[num].x = i;
A[num].y = j;
A[num++].h = a[i][j];
dp[i][j] = 1;
}
}
sort(A, A + num, cmp);
for (int k = 0; k < num; k++) {
int i = A[k].x;
int j = A[k].y;
if (a[i][j] > a[i - 1][j])
dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1);
if (a[i][j] > a[i][j - 1])
dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1);
if (a[i][j] > a[i + 1][j])
dp[i][j] = max(dp[i][j], dp[i + 1][j] + 1);
if (a[i][j] > a[i][j + 1])
dp[i][j] = max(dp[i][j], dp[i][j + 1] + 1);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= c; j++) {
java:
import java.util.Scanner;
public class Main {
static int p[][] = { {1, 0}, {0, 1},{-1, 0}, {0, -1}};
static int max = Integer.MIN_VALUE, r, c;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
r = sc.nextInt();
c = sc.nextInt();
int[][] a = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
dfs(a, i, j, 1);
}
}
System.out.println(max);
}
public static void dfs(int[][] a, int i, int j, int sum) {
int t, x, y;
if (sum > max)
max = sum;
for (t = 0; t < 4; t++)
{
x = i + p[t][0];
y = j + p[t][1];
if (x >= 0 && x < r && y >= 0 && y < c && a[x][y] < a[i][j])
{
sum++;
dfs(a, x, y, sum);
sum--;
}
}
}
}
ADV-295 fx
cpp:
#include
#include
#include
using namespace std;
#define MOD 1000000007
int f[210][66], A[210], B[210];
char s1[210], s2[210];
int read() {
int s = 0, fh = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + (ch - '0');
ch = getchar();
}
return s * fh;
}
int Dfs(
int len, int tot,
int flag)
{
if (len == 0) return tot >= 0;
if (tot < -30) return 0;
if (tot > 30) tot = 30;
if (flag == 0 && f[len][tot + 30] != -1) return f[len][tot + 30];
int end = flag ? B[len] : 9, i, ret = 0;
for (i = 0; i <= end; i++)
ret = (ret + Dfs(len - 1, (tot + A[len] - i) * 2, flag && (i == end))) %
MOD;
if (flag == 0) f[len][tot + 30] = ret;
return ret;
}
int main() {
int case1 = 0, T, la, lb, i;
T = read();
while (T--) {
scanf("\n%s %s", s1 + 1, s2 + 1);
la = strlen(s1 + 1);
lb = strlen(s2 + 1);
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(f, -1, sizeof(f));
for (i = 1; i <= la; i++)
A[i] = s1[la - i + 1] - '0';
for (i = 1; i <= lb; i++) B[i] = s2[lb - i + 1] - '0';
if (la > lb) swap(la, lb);
printf("Case #%d: %d\n", ++case1, Dfs(lb, 0, 1));
}
return 0;
}
ADV-296 奥运会开幕式
cpp:
#include
int main() {
int a[10010], m, n, x, count, i;
scanf("%d %D", &m, &n);
for (i = 1; i <= m; i++) {
a[i] = i, count = m, x = 0;
}
for (i = 0; count > 1; i++) {
if (a[i % m + 1] != -1) x++;
if (x == n && a[i % m + 1] != -1) {
a[i % m + 1] = -1, count--, x = 0;
}
}
for (i = 1; i <= m; i++) {
if (a[i] != -1) printf("%d\n", i);
}
return 0;
}
java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int p=0;
for (int i = 2; i <= n; i++) {
p=(p+m)%i;
}
System.out.println(p+1);
}
}
ADV-297 快速排序
cpp:
#include
using namespace std;
void quicksort(int arr[], int low, int high) {
int i = low, j = high;
int temp;
if (low < high) {
temp = arr[low];
while (i != j) {
while (j > i && arr[j] >= temp) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= temp) {
i++;
}
arr[j] = arr[i];
}
arr[i] = temp;
quicksort(arr, low, i - 1);
quicksort(arr, i + 1, high);
}
}
int main() {
int arr[10] = {5, 2, 6, 1, 7, 3, 4};
int i = 0, n;
for (i = 0; i < 10; i++) {
cin >> arr[i];
if (arr[i] == 0) {
break;
}
}
n = i;
quicksort(arr, 0, n - 1);
for (i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str=br.readLine().split(" ");
int [] numbers=new int[str.length-1];
for(int i=0;i<str.length-1;i++)
numbers[i]=Integer.parseInt(str[i]);
quicksort(numbers,0,numbers.length-1);
for(int i=0;i<numbers.length;i++)
System.out.print(numbers[i]+" ");
}
public static void quicksort(int[] numbers,int low,int high)
{
if(low>high)
return;
int x=numbers[low];
int i=low;
int j=high;
while (i<j)
{
while(x<=numbers[j]&&j>i)
j--;
while (x>=numbers[i]&&j>i)
i++;
if(i<j)
{
int tmep=numbers[i];
numbers[i]=numbers[j];
numbers[j]=tmep;
}
}
numbers[low]=numbers[i];
numbers[i]=x;
quicksort(numbers,low,j-1);
quicksort(numbers, j+1,high);
}
}
ADV-298 和谐宿舍2
cpp:
#include
#define INF 0x3F3F3F3F
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int main() {
int n, m;
int heights[105] = {0};
int max_height[105][105] = {0};
int f[105][105] = {0};
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &heights[i]);
for (int i = 1; i <= n; ++i) {
max_height[i][i] = heights[i];
for (int j = i + 1; j <= n; ++j)
max_height[i][j] = max(max_height[i][j - 1], heights[j]);
}
for (int i = 1; i <= n; ++i) {
f[i][1] = i * max_height[1][i];
for (int j = 2; j <= i && j <= m; ++j) {
f[i][j] = INF;
for (int k = 1; k <= i - j + 1; ++k)
f[i][j] = min(f[i][j],
f[i - k][j - 1] + k * max_height[i - k + 1][i]);
}
}
printf("%d", f[n][m]);
return 0;
}
java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int [] heights = new int[n+1];
for (int i=1;i<=n;i++){
heights[i]=sc.nextInt();
}
sc.close();
int [][] max_height = new int[n+1][n+1];
int [] [] f = new int[n+1][m+1];
for (int i=1;i<=n;i++){
max_height[i][i]=heights[i];
for (int j=i+1;j<=n;j++){
max_height[i][j]=Math.max(max_height[i][j-1],heights[j]);
}
}
for (int i=1;i<=n;i++){
f[i][1]=i*max_height[1][i];
for (int j=2;j<=i && j<=m;j++){
f[i][j]=Integer.MAX_VALUE;
for (int k=1;k<=i-j+1;k++){
f[i][j]=Math.min(f[i][j], f[i-k][j-1]+k * max_height[i-k+1][i]);
}
}
}
System.out.println(f[n][m]);
}
}
ADV-299 宰羊
cpp:
#include
using namespace std;
const int N = 105;
int T;
int n, m;
int a[N];
int b[N][N];
void one() {
memset(b, 0, sizeof(b));
memset(a, 0, sizeof(a));
int i, j, l, r;
cin >> n >> m;
for (i = 1; i <= m; i++) {
cin >> a[i];
}
sort(a + 1, a + m);
a[m + 1] = n + 1;
for (i = 1; i <= m; i++) {
for (l = 1; l + i - 1 <= m; l++) {
r = l + i - 1;
if (i == 1) {
b[l][r] = a[r + 1] - a[l - 1] - 2;
} else {
b[l][r] = 0x3f3f3f3f;
for (j = l; j <= r; j++) {
b[l][r] = min(b[l][r], a[r + 1] - a[l - 1] - 2 +
b[j + 1][r] + b[l][j - 1]);
}
}
}
}
cout << b[1][m] << endl;
}
int main(int argc, char *argv[]) {
cin >> T;
while (T--) {
one();
}
return 0;
}
java:
import java.io.*;
public class Main{
public static void main(String[]args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-->0){
String[] arr = in.readLine().split(" ");
int n = Integer.parseInt(arr[0]);
int m = Integer.parseInt(arr[1]);
int[][] dp = new int[m+2][m+2];
String[] cur = in.readLine().split(" ");
int[] a = new int[m+2];
for(int i=1; i<=m; i++) a[i] = Integer.parseInt(cur[i-1]);
a[0] = 0;
a[m+1] = n+1;
for(int len = 1; len<=m; len++){
for(int l = 1; l+len-1<=m; l++){
int r = l+len-1;
if(len ==1){
dp[l][r] = a[r+1]-a[l-1]-2;
}else{
dp[l][r] = Integer.MAX_VALUE;
for(int i=l; i<=r; i++){
dp[l][r] = Math.min(dp[l][r], dp[l][i-1]+dp[i+1][r]+a[r+1]-a[l-1]-2);
}
}
}
}
if(t>0)
System.out.println(dp[1][m]);
else System.out.print(dp[1][m]);
}
}
}
ADV-300 字符串生成器
cpp:
#include
#include
#include
#include
#include
#include
#include
#define maxn 55
#define eps 1e-7
using namespace std;
int T, n, l, c, tr[maxn][10], tot, fail[maxn];
int id[maxn];
double p[10], a[maxn][maxn], f[maxn][1 << 10], ans[maxn];
char ch[maxn];
void Q() {
for (int i = 0; i <= tot; i++) {
fail[i] = 0;
id[i] = 0;
for (int j = 0; j < c; j++) tr[i][j] = 0;
for (int j = 0; j < (1 << n); j++) f[i][j] = 0;
}
tot = 0;
}
bool ins(int x) {
int k = 0;
for (int i = 0; i < l; i++) {
if (!tr[k][ch[i] - 'a']) tr[k][ch[i] - 'a'] = ++tot;
k = tr[k][ch[i] - 'a'];
}
if (id[k]) return 0;
id[k] = 1 << (x - 1);
return 1;
}
queue<int> q;
void build() {
for (int i = 0; i < c; i++)
if (tr[0][i]) q.push(tr[0][i]);
while (!q.empty()) {
int k = q.front();
q.pop();
for (int i = 0; i < c; i++) {
if (tr[k][i])
fail[tr[k][i]] = tr[fail[k]][i], q.push(tr[k][i]);
else
tr[k][i] = tr[fail[k]][i];
}
}
}
void Guess() {
int N = tot;
for (int i = 0; i <= N; i++) {
int Max = i;
for (int j = i; j <= N + 1; j++) swap(a[i][j], a[Max][j]);
if (fabs(a[i][i]) < eps) continue;
for (int j = i + 1; j <= N; j++) {
double tmp = a[j][i] / a[i][i];
for (int k = i; k <= N + 1; k++) a[j][k] -= a[i][k] * tmp;
}
}
for (int i = N; i >= 0; i--) {
ans[i] = -a[i][N + 1] / a[i][i];
for (int j = i - 1; j >= 0; j--) {
a[j][N + 1] += a[j][i] * ans[i];
}
}
}
void work() {
Q();
scanf("%d%d%d", &n, &l, &c);
for (int i = 1; i <= n; i++) {
scanf(" %s", ch);
if (!ins(i)) n--, i--;
}
build();
for (int i = 0, t; i < c; i++) {
scanf("%d", &t);
p[i] = 1.0 * t / 10000;
}
int N = (1 << n) - 1;
for (int S = N - 1; S >= 0; S--) {
for (int i = 0; i <= tot; i++)
for (int j = 0; j <= tot + 1; j++) a[i][j] = 0;
for (int i = 0; i <= tot; i++) {
for (int j = 0; j < c; j++) {
int v = tr[i][j];
a[i][v] += p[j];
if (id[v] && (!(S & id[v]))) {
a[i][v] -= p[j];
a[i][tot + 1] += (f[v][S | id[v]]) * p[j];
}
}
a[i][i]--;
a[i][tot + 1]++;
}
Guess();
for (int i = 0; i <= tot; i++) f[i][S] = ans[i];
}
printf("%.5lf\n", f[0][0]);
}
int main() {
for (scanf("%d", &T); T--; work())
;
return 0;
}