门禁系统
实现
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
int a[N],ans[N];
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
ans[a[i]]++;
printf("%d ",ans[a[i]]);
}
}
Z字形扫描
实现
#include<bits/stdc++.h>
using namespace std;
const int N =505;
int n;
int a[N][N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int k=1;k<2*n;k++)
{
if(k<=n)
{
if(k%2)
{
int j = 1,i = k;
for(;j<=k;j++,i--) printf("%d ",a[i][j]);
}
else
{
int i = 1,j = k;
for(;i<=k;i++,j--) printf("%d ",a[i][j]);
}
}
else
{
int num = 2*n-k;
if(k%2==0)
{
int j = n,i = n-num+1;
for(;i<=n;j--,i++) printf("%d ",a[i][j]);
}
else
{
int i = n,j = n-num+1;
for(;j<=n;i--,j++) printf("%d ",a[i][j]);
}
}
}
}
集合竞价
实现
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
struct node
{
int type,s;
double p;
}nodes[N];
int main()
{
string op;
int cnt = 0;
int t;
while(1)
{
if(!(cin>>op)) break;
// cout<<op<<endl;
if(op=="buy") cin>>nodes[++cnt].p>>nodes[cnt].s,nodes[cnt].type = 1;
else if(op=="sell") cin>>nodes[++cnt].p>>nodes[cnt].s,nodes[cnt].type = 2;
else scanf("%d\n",&t),nodes[t].type = 0,cnt++;
}
double ans_p = 0;
long long ans_s = -1;
for(int i=1;i<=cnt;i++)
{
if(!nodes[i].type) continue;
double p1 = nodes[i].p;
long long buys = 0,sells = 0;
// cout<<"p1:"<<p1<<endl;
for(auto x:nodes)
{
if(!x.type) continue;
else if(x.type==1 && x.p>=p1)
{
buys+=x.s;
// cout<<"buy"<<x.p<<" "<<x.s<<endl;
}
else if(x.type==2 && x.p<=p1)
{
sells+=x.s;
//cout<<"sell"<<x.p<<" "<<x.s<<endl;
}
}
// cout<<"ans:"<<min(buys,sells)<<endl;
// cout<<endl;
if(min(buys,sells)>ans_s || (min(buys,sells)==ans_s && ans_p<p1))
{
ans_p = p1;
ans_s = min(buys,sells);
}
}
printf("%.2lf %ld",ans_p,ans_s);
}
最优灌溉
思路
最小生成树prim
实现
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1001;
int g[N][N];
int dist[N];
int st[N];
int n,m;
int prim(){
int ans = 0;
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
for(int i=0; i<n; i++){
int t = -1;
for(int j=1; j<=n; j++)
if(!st[j] && (t==-1 || dist[j]<dist[t])) t = j;
for(int j=1; j<=n; j++)
if (!st[j])dist[j] = min(dist[j], g[t][j]);
st[t] = 1;
ans += dist[t];
}
return ans;
}
int main(){
ios::sync_with_stdio(0);
memset(g, 0x3f, sizeof(g));
cin>>n>>m;
for(int i=0; i<m; i++){
int a,b,w;
cin>>a>>b>>w;
g[a][b] = w;
g[b][a] = w;
}
cout<<prim();
return 0;
}
货物调度
不会网络流= =
欢迎指正