题目B:简单贪心+完全背包!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=210002;
int cas=1,t;
ll ans,mx,v,dp[maxn];
struct node
{
ll s,pri;
double pi;
}p[4];
int cmp(node a,node b)
{
return a.pi>=b.pi?1:0;
}
int main()
{
//freopen("D://input.txt","r",stdin);
cin>>t;
while(t--)
{
int i;
ll j;
for(mx=ans=i=0;i<3;i++)
{
cin>>p[i].s>>p[i].pri;
p[i].pi=1.0*p[i].pri/p[i].s;
}
memset(dp,0,sizeof(dp));
sort(p,p+3,cmp);
cin>>v;
cout<<"Case "<<cas++<<": ";
if(v>100000)
{
int tmp=v-100000;
v-=tmp;
int cnt=tmp/p[0].s;
ans+=cnt*p[0].pri;
v+=tmp-cnt*p[0].s;
}
for(i=0;i<3;i++)
{
for(j=p[i].s;j<=v;j++)
{
if(dp[j]<dp[j-p[i].s]+p[i].pri)dp[j]=dp[j-p[i].s]+p[i].pri;
mx=max(mx,dp[j]);
}
}
cout<<ans+mx<<endl;
}
return 0;
}
题目C:简单矩阵处理!!
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=22;
int t,n,m,cas=1,map[maxn][maxn],res[maxn][maxn];
int dx[]={
1,-1,0,0
};
int dy[]={0,0,1,-1};
int inmap(int x,int y)
{
if(x<0||y<0||x>=n||y>=m)return 0;
return 1;
}
int main()
{
//freopen("D://input.txt","r",stdin);
cin>>t;
while(t--)
{
cin>>n>>m;
int i,j,k;
for(i=0;i<n;++i)
for(j=0;j<m;j++)cin>>map[i][j],res[i][j]=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
for(k=0;k<4;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(inmap(x,y))
{
res[i][j]+=map[x][y]-map[i][j];
} else res[i][j]--;
}
}
}
int mx=-9999999,x=0,y=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mx<res[i][j])mx=res[i][j],x=i,y=j;
}
}
cout<<"Case "<<cas++<<": "<<mx<<" "<<x+1<<" "<<y+1<<endl;
}
return 0;
}
题目E:简单模拟处理!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10002;
double wp,hp,inner;
int t,cas=1;
string dan,nam,str;
int main()
{
//freopen("D://input.txt","r",stdin);
cin>>t;
int i,j,k;
getchar();
while(t--)
{
getline(cin,str);
inner=hp=wp=0.0;
int pos=str.find("inches");
pos--;
while(str[pos]==' ')pos--;
int ppos=pos;
while(str[ppos]!=' ')ppos--;
for(i=ppos+1;i<=pos;i++)
{
if(str[i]=='.')break;
inner=inner*10+(str[i]-'0');
}while(str[ppos]==' ')ppos--;
double tmp=0.1;
for(i++;i<=pos;i++)
{
inner=inner+tmp*(str[i]-'0');
tmp*=0.1;
}
int pos1=str.find("*");
int pos2=pos1;
while(str[pos1]!=' ')pos1--;
for(i=pos1+1;i<pos2;i++)wp=wp*10+str[i]-'0';
while(str[pos1]==' ')pos1--;
pos2++;
while(str[pos2]!=' ')hp=hp*10+str[pos2]-'0',pos2++;
while(str[pos2]==' ')pos2++;
printf("Case %d: The ",cas++);
while(pos2<str.size())
{
if(str[pos2]>='A'&&str[pos2]<='Z')cout<<char(str[pos2]-'A'+'a');
else cout<<char(str[pos2]);
pos2++;
}cout<<" of ";
for(i=0;i<=ppos;i++)cout<<str[i];cout<<"'s PPI is ";
if(inner==0.0)printf("0.00.\n");
else
{
double dp=sqrt(hp*hp+wp*wp)/inner;
printf("%.2lf.\n",dp);
}
}
return 0;
}
题目G:简单字符串处理!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=2222;
int t,cas=1;
char str[maxn];
string s1="n5!wpuea^o7";
string s2="usimdnaevol";
int main()
{
//freopen("D://input.txt","r",stdin);
scanf("%d",&t);getchar();
while(t--)
{
gets(str);
int i,j,len=strlen(str);
cout<<"Case "<<cas++<<": ";
for(i=len-1;i>=0;i--)
{
if(str[i]==' ')
cout<<str[i];
else {
for(j=0;j<s1.size();j++)
{
if(str[i]==s1[j])
{
cout<<s2[j];break;
}
}
}
}cout<<endl;
}
return 0;
}
题目H:大型网络流----费用流!!
/*
分析下题目,假设相邻的 hill 是 a1, a2,插入的那个是 x,这个 x 要么 > max(a1, a2) 要
么 < min(a1, a2),不然的话高度不增加。需要让增加的最大。从 m 个取 k 个对应给 n 个
中去,要求增加高度最大,最优匹配吧,有个限制只能取 k 个,用最小费用流,在 m 个点
前在加个容量为 k 的边限制下就可以。所以构图就是增加点 s, lim, t。s -> lim 容量为 k,
表示最多选 k 个,lim 到 m 个点容量为 1 表示每个 hill 只有一个,然后 m 个点对应 n - 1
个点,容量为 inf,cost 为负的增加值(要求最大,所以取负),最后 n - 1 个点对应 t,容
量为 1,表示两个之间最多加一个。求最小费用最大流就可以了,最后再加上原来需要走的。
然后貌似点有点多,优化下,发现 0 <= Y <= 30,这样通过将 lim 连 m 个点的相同 Y 的
点合并,将容量设为相同 Y 点的个数,就可以减少点为 1030,边为 1000 * 30。
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int M=2202;
const int MAXN=400002;
const int INF=100000;
const int inf=1<<29;
int pre[M]; // pre[v] = k:在增广路上,到达点v的边的编号为k
int dis[M]; // dis[u] = d:从起点s到点u的路径长为d
int vis[M]; // inq[u]:点u是否在队列中
int path[M];
int head[M],cnt[M],x[M];
int n,m,NE,sink,tot,ans,max_flow,map[M][M];
struct node
{
int u,v,cap,cost,next;
} Edge[MAXN<<2];
void addEdge(int u,int v,int cap,int cost)
{
Edge[NE].u=u;
Edge[NE].v=v;
Edge[NE].cap=cap;
Edge[NE].cost=cost;
Edge[NE].next=head[u];
head[u]=NE++;
Edge[NE].v=u;
Edge[NE].u=v;
Edge[NE].cap=0;
Edge[NE].cost=-cost;
Edge[NE].next=head[v];
head[v]=NE++;
}
bool SPFA(int s,int t) // 源点为s,汇点为sink。
{
int i;
for(i=0; i<=n+1; i++) dis[i]=inf;
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
dis[s] = 0;
queue<int>q;
q.push(s);
vis[s] =1;
while(!q.empty()) // 这里最好用队列,有广搜的意思,堆栈像深搜。
{
int u =q.front();
q.pop();
for(i = head[u]; i != -1; i = Edge[i].next)
{
int v = Edge[i].v;
if(Edge[i].cap >0&& dis[v] > dis[u] + Edge[i].cost)
{
dis[v] = dis[u] + Edge[i].cost;
pre[v] = u;
path[v]=i;
if(!vis[v])
{
vis[v] =1;
q.push(v);
}
}
}
vis[u] =0;
}
if(pre[t]==-1)
return false;
return true;
}
void end(int s,int t)
{
int u, p,sum = inf;
for(u=t; u!=s; u=pre[u])
{
sum = min(sum,Edge[path[u]].cap);
}
max_flow+=sum;
for(u = t; u != s; u=pre[u])
{
Edge[path[u]].cap -= sum;
Edge[path[u]^1].cap += sum;
ans += sum*Edge[path[u]].cost; // cost记录的为单位流量费用,必须得乘以流量。
}
}
int main() {
// freopen("D://input.txt","r",stdin);
int Test,N,M,K;
int cas = 1;
scanf("%d", &Test);
while (Test--) {
memset(head,-1,sizeof(head));
NE=ans=max_flow=0;
printf("Case %d: ", cas++);
memset(cnt, 0, sizeof(cnt));
scanf("%d%d%d", &N, &M, &K);
int tot = N - 1 + 31;
int supersource = tot, source = tot + 1, sink = tot + 2;
n=sink;
int sum = 0;
for (int i = 0; i < N; i++) {
scanf("%d", &x[i]);
if (i) sum += abs(x[i] - x[i - 1]);
if (i < N - 1)
addEdge(source, i, 1, 0);
}
for (int i = 0; i < M; i++) {
int y;
scanf("%d", &y);
cnt[y]++;
}
for (int i = 0; i <= 30; i++) {
addEdge(N - 1 + i, sink, cnt[i], 0);
}
addEdge(supersource, source, K, 0);
for (int j = 0; j <= 30; j++)
if (cnt[j]) {
for (int i = 1; i < N; i++) {
int tmp = abs(x[i] - j) + abs(x[i - 1] - j) - abs(x[i] - x[i - 1]);
addEdge(i - 1, N - 1 + j, 1, -tmp);
}
}
while(SPFA(supersource,sink))
{
end(supersource,sink);
}
printf("%d\n", sum-ans);
}
return 0;
}