A - Necklace
题意:
给出总粘土体积和消耗体积,问最长的项链长度是多少?
思路:
本质是求导数为0,取极值时的自变量。由于本题的点是离散的,所以要考虑极值点离哪一个更近,即考虑 0.5 * vt / v0 - (int)(0.5 * vt / v0) 与 0.5的大小关系。
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
A - Necklace
UVA 11001
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1e5+7;
int n;
double v0,vt;
int main(){
while(scanf("%lf %lf",&vt,&v0) && vt + v0){
if(vt <= v0){
printf("0\n");
}else if(vt <= 2*v0){
printf("1\n");
}else if(0.5 * vt / v0 - (int)(0.5 * vt / v0) == 0.5){
printf("0\n");
}else if(0.5 * vt / v0 - (int)(0.5 * vt / v0) < 0.5){
printf("%d\n",(int)(0.5 * vt / v0) );
}else if(0.5 * vt / v0 - (int)(0.5 * vt / v0) > 0.5){
printf("%d\n",(int)(0.5 * vt / v0 )+1);
}
}
return 0;
}
B - Bode Plot
题意:
物理背景,确定不同的W的值对应的VR值。
思路:
完全看不懂,只能抄公式
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
B- Bode Plot
POJ 1045
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
const int N = 1e5+7;
int n;
double a,b,c,w,v;
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
scanf("%lf %lf %lf %d",&a,&b,&c,&n);
while(n--){
scanf("%lf",&w);
v=a*b*c*w/sqrt(1+pow(b*c*w,2));
printf("%.3f\n",v);
}
return 0;
}
C - Symmetric Matrix
题意:
问矩阵是否中心对称,注意元素非负,这里未注意到。
思路:
模拟即可。
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
C - Symmetric Matrix
UVA 11349
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1007;
int n,cas;
ll matrix[N][N];
int main(){
cin>>cas;
for(int icas = 1; icas <= cas; ++icas){
getchar();
int flag = 0;
scanf("N = %d",&n);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
scanf("%lld",&matrix[i][j]);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(matrix[i][j] < 0 || matrix[i][j] != matrix[n+1-i][n+1-j]){
flag = 1;
goto here;
}
}
}
here:
printf("Test #%d: ",icas);
if(flag == 0) puts("Symmetric.");
else puts("Non-symmetric.");
}
return 0;
}
D - Homogeneous Squares
题意:
问独立位置的数的和是否相同,相同输出“homogeneous“,否则输出“not homogeneous”.
思路:
观察特征,所有情况都可以变为2*2的情况
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
D - Homogeneous Squares
POJ_2941
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1007;
int n;
int a[N][N];
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
while(scanf("%d",&n) && n){
int flag = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
scanf("%d",&a[i][j]);
for(int i = 1; i < n; ++i){
for(int j = 1; j < n; ++j){
if(a[i][j] + a[i+1][j+1] != a[i+1][j] + a[i][j+1]){
flag = 1;
goto there;
}
}
}
there:
if(flag == 0){
puts("homogeneous");
}else{
puts("not homogeneous");
}
}
return 0;
}
E - To the Max
题意:
求最大子矩阵和,DP思想,采用多行压成一行的思路,化为一维求最大连续和问题。
两层循环依次枚举压缩的行。
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
E - To the Max
POJ 1050
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 107;
int n,a[N][N];
ll s[N][N],dp,maxx = -1;
void init(){
for(int j = 1; j <= n; ++j){
for(int i = 1; i <= n; ++i)
s[i][j] = s[i-1][j] + a[i][j];
}
}
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
scanf("%d",&a[i][j]);
init();
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j){
dp = 0;
for(int k = 1; k <= n; ++k){
dp = max(s[j][k]-s[i-1][k],dp + s[j][k]-s[i-1][k]);
maxx = max(maxx,dp);
}
}
printf("%lld\n",maxx);
return 0;
}
F - Who’s in the Middle
题意:
排序找出中间值。
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
F - Who's in the Middle
USACO_04 Nov POJ2388
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1e5+7;
int n,a[N];
int main(){
cin>>n;
for(int i = 1; i <= n; ++i)
cin>>a[i];
sort(a + 1,a+1+n);
cout<<a[n/2+1];
return 0;
}
G - Train Swapping
题意:
经典例题,问最优交换次数,和冒泡排序一致。
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4s
G - Train Swapping
UVA 299
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1e5+7;
int a[N];
int n,cas;
int main(){
cin>>cas;
while(cas--){
int cnt = 0;
cin>>n;
for(int i = 1; i <= n; ++i) cin>>a[i];
for(int i = n-1; i >= 1; i--){
for(int j = 1; j <= i; ++j){
if(a[j] > a[j+1])
swap(a[j],a[j+1]),cnt++;
}
}
printf("Optimal train swapping takes %d swaps.\n",cnt);
}
return 0;
}
H - DNA Sorting
题意:
求出每个序列的逆序对个数,然后从小到大排列。可利用冒泡排序求出
/********************
*author:
*topic:
*source:灵动ICPC冬令营基础-4
H - DNA Sorting
POJ 1007
*******************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1007;
int n,m;
struct Node{
int id,val;
}node[N];
char str[N][57],tmp[57];
bool cmp(const Node &a,const Node &b){
return a.val < b.val;
}
int solve(int x){
int ret = 0;
for(int i = 1; i <= n; ++i)
tmp[i] = str[x][i];
for(int i = n-1; i >= 1; --i)
for(int j = 1; j <= i; ++j)
if(tmp[j] > tmp[j+1])
swap(tmp[j],tmp[j+1]),ret++;
return ret;
}
int main(){
cin>>n>>m;
for(int i = 1; i <= m; ++i){
scanf("%s",str[i]+1);
node[i].val = solve(i);
node[i].id = i;
}
sort(node+1,node+1+m,cmp);
for(int i = 1; i <= m; ++i){
printf("%s\n",str[node[i].id]+1);
}
return 0;
}