3894: 文理分科

题目链接

题目大意:给出一个表格,每个人要选择文科或者理科,每个人选择文科有一个满意度,选择理科有一个满意度,以一个人为中心的五个人全选择一科也有一个满意度。问最大的满意度是多少

题解:类似2127happiness
Orz题解

我的收获:割割割割割

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int dx[]={0,1,-1,0,0};
const int dy[]={0,0,0,1,-1};
const int M=30005;
#define INF 0x7fffffff
#define rep(x,y) for(int i=1;i<=x;i++) for(int j=1;j<=y;j++)

bool Exit;
int n,m,t,st,ed,cnt,sum,x;
int num[M],d[M],head[M],last[M]; 

struct edge{int to,nex,c;}e[280005];

void add(int i,int j,int w){e[t].to=j,e[t].c=w,e[t].nex=head[i],last[i]=head[i]=t++;}
void ins(int x,int y,int z){add(x,y,z),add(y,x,0);}

int stu(int x,int y){return (x-1)*m+y;}
int art(int x,int y){return (x-1)*m+y+n*m;}
int sci(int x,int y){return (x-1)*m+y+n*m*2;}

int dfs(int x,int in)
{
    if(x==ed) return in;
    int ans=0,t;
    for(int i=last[x];i!=-1;last[x]=i=e[i].nex)
    {
        int v=e[i].to;
        if(e[i].c&&d[v]==d[x]-1){
            t=dfs(v,min(in-ans,e[i].c));
            ans+=t;
            e[i].c-=t; e[i^1].c+=t;
            if(Exit||ans==in) return ans;
        }
    }
    if(--num[d[x]]==0) Exit=1;
    d[x]++,num[d[x]]++,last[x]=head[x];
    return ans;
}

int ISAP()
{
    Exit=0;int flow=0;
    while(!Exit) flow+=dfs(st,INF);
    return flow;
}

void work(){
    cout<<sum-ISAP()<<endl;
}

void build()
{
    rep(n,m){
        scanf("%d",&x),sum+=x;
        for(int k=0;k<=4;k++){
            int fx=i+dx[k],fy=j+dy[k];
            if(fx<1||fx>n||fy<1||fy>m) continue;
            ins(art(i,j),stu(fx,fy),INF);
        }
        ins(st,art(i,j),x);
    }
    rep(n,m){
        scanf("%d",&x),sum+=x;
        for(int k=0;k<=4;k++){
            int fx=i+dx[k],fy=j+dy[k];
            if(fx<1||fx>n||fy<1||fy>m) continue;
            ins(stu(fx,fy),sci(i,j),INF);
        }
        ins(sci(i,j),ed,x);
    }
}

void init()
{
    memset(head,-1,sizeof(head));
    memset(last,-1,sizeof(last));
    cin>>n>>m;
    st=0,ed=3*n*m+1;num[0]=ed+1;
    rep(n,m) scanf("%d",&x),sum+=x,ins(st,stu(i,j),x);
    rep(n,m) scanf("%d",&x),sum+=x,ins(stu(i,j),ed,x);
    build();
}

int main()
{
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.*; class Student { private int id; private String name; private int chinese; private int math; private int english; private int totalScore; public Student(int id, String name, int chinese, int math, int english) { this.id = id; this.name = name; this.chinese = chinese; this.math = math; this.english = english; this.totalScore = chinese + math + english; } public int getId() { return id; } public String getName() { return name; } public int getChinese() { return chinese; } public int getMath() { return math; } public int getEnglish() { return english; } public int getTotalScore() { return totalScore; } public String getCategory() { return ""; } @Override public String toString() { return id + "," + name + "," + chinese + "," + math + "," + english + "," + totalScore + "," + getCategory(); } } class ArtStudent extends Student { private int artScore; public ArtStudent(int id, String name, int chinese, int math, int english, int artScore) { super(id, name, chinese, math, english); this.artScore = artScore; this.totalScore += artScore; } public int getArtScore() { return artScore; } public String getCategory() { return "艺术生"; } @Override public String toString() { return super.toString() + "," + artScore; } } class SportsStudent extends Student { private int sportsScore; public SportsStudent(int id, String name, int chinese, int math, int english, int sportsScore) { super(id, name, chinese, math, english); this.sportsScore = sportsScore; this.totalScore += sportsScore; } public int getSportsScore() { return sportsScore; } public String getCategory() { return "体育生"; } @Override public String toString() { return super.toString() + "," + sportsScore; } } class ArtsStudent extends Student { public ArtsStudent(int id, String name, int chinese, int math, int english) { super(id, name, chinese, math, english); } public String getCategory() { return "文科艺术生"; } } class ScienceStudent extends Student { public ScienceStudent(int id, String name, int chinese, int math, int english) { super(id, name, chinese, math, english); } public String getCategory() { return "文科理科生"; } } class ArtScienceStudent extends Student { private int artScore; public ArtScienceStudent(int id, String name, int chinese, int math, int english, int artScore) { super(id, name, chinese, math, english); this.artScore = artScore; this.totalScore += artScore; } public int getArtScore() { return artScore; } public String getCategory() { return "理科艺术生"; } @Override public String toString() { return super.toString() + "," + artScore; } } class SportsScienceStudent extends Student { private int sportsScore; public SportsScienceStudent(int id, String name, int chinese, int math, int english, int sportsScore) { super(id, name, chinese, math, english); this.sportsScore = sportsScore; this.totalScore += sportsScore; } public int getSportsScore() { return sportsScore; } public String getCategory() { return "理科体育生"; } @Override public String toString() { return super.toString() + "," + sportsScore; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // 普通文理生人数 List<Student> students = new ArrayList<>(); // 输入普通文理生信息 for (int i = 0; i < n; i++) { int id = scanner.nextInt(); String name = scanner.next(); int chinese = scanner.nextInt(); int math = scanner.nextInt(); int english = scanner.nextInt(); int category = scanner.nextInt(); if (category == 1) { students.add(new ArtsStudent(id, name, chinese, math, english)); } else { students.add(new ScienceStudent(id, name, chinese, math, english)); } } int m = scanner.nextInt(); // 艺体生人数 // 输入艺体生信息 for (int i = 0; i < m; i++) { int id = scanner.nextInt(); String name = scanner.next(); int chinese = scanner.nextInt(); int math = scanner.nextInt(); int english = scanner.nextInt(); int category = scanner.nextInt(); int score = scanner.nextInt(); if (category == 1) { students.add(new ArtStudent(id, name, chinese, math, english, score)); } else { students.add(new SportsStudent(id, name, chinese, math, english, score)); } } // 按总分由高到低排序 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { if (s1.getTotalScore() != s2.getTotalScore()) { return s2.getTotalScore() - s1.getTotalScore(); } else { if (s1 instanceof ArtStudent && s2 instanceof ArtStudent) { if (((ArtStudent) s1).getArtScore() != ((ArtStudent) s2).getArtScore()) { return ((ArtStudent) s2).getArtScore() - ((ArtStudent) s1).getArtScore(); } else { return s1.getId() - s2.getId(); } } else if (s1 instanceof SportsStudent && s2 instanceof SportsStudent) { if (((SportsStudent) s1).getSportsScore() != ((SportsStudent) s2).getSportsScore()) { return ((SportsStudent) s2).getSportsScore() - ((SportsStudent) s1).getSportsScore(); } else { return s1.getId() - s2.getId(); } } else { return s1.getId() - s2.getId(); } } } }); // 输出考生信息 for (Student student : students) { System.out.println(student); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值