NC83
链接:牛客网NC83
题意、输入、输出:
分析:先来先装卸,所以记录在装卸的工厂完成时间和没有装卸的工厂的到达时间,一旦有工厂装卸完毕,就将最早来的还没装卸的工厂开始装卸,根据两者完成时间和到达的时间计算完成时间,不断直到所有工厂装卸完成。
思路:开始对所有工厂按到达时间排序,使用小顶堆维护正在装卸的工厂完成时间,开始k个工厂入堆,后面的比较堆顶和最小到达时间,后者大不停出栈,最后将这最小到达时间的判断计算完成时间加入堆。
代码:
import java.util.*;
class shop implements Comparable{
long a;
long b;
@Override
public int compareTo(Object o) {
shop s=(shop)o;
return (int)(a-s.a);
}
}
public class Solution {
/**
*
* @param n int整型
* @param k int整型
* @param a int整型一维数组
* @param b int整型一维数组
* @return long长整型
*/
public long solve (int n, int k, int[] a, int[] b) {
// write code here
PriorityQueue<Long> que=new PriorityQueue<>();
shop[] hh=new shop[n];
for(int i=0;i<n;i++){
hh[i]=new shop();
hh[i].a=a[i];
hh[i].b=b[i];
}
Arrays.sort(hh);
int cnt=0;
while(cnt<n){
if(cnt<k)que.add(hh[cnt].a+hh[cnt++].b);
else {
long tem=que.poll();
if(tem<hh[cnt].a)tem=hh[cnt].a;
que.add(tem+hh[cnt++].b);
}
}
long maxx=0;
while(que.size()>0){
maxx=que.poll();
}
return maxx;
}
}
NC54
链接:
牛客网NC54
题意、输入、输出:
分析:dp,注意先从行转移到列
思路:dp
代码:
import java.util.*;
public class Solution {
/**
* 简单变相
* @param n int整型
* @param m int整型
* @param x int整型一维数组
* @param y int整型一维数组
* @return int整型
*/
static int MOD=(int)1e9+7;
public int solve (int n, int m, int[] x, int[] y) {
// write code here
boolean[][] vis=new boolean[3][n];
for(int i=0;i<m;i++){
vis[x[i]-1][y[i]-1]=true;
}
long[][] dp=new long[3][n];
dp[0][0]=1;
for(int j=0;j<n-1;j++){
for(int i=0;i<3;i++){
if(!vis[i][j+1]){
dp[i][j+1]=(dp[i][j+1]+dp[i][j])%MOD;
}
if(i-1>=0&&!vis[i-1][j+1]){
dp[i-1][j+1]=(dp[i-1][j+1]+dp[i][j])%MOD;
}
if(i+1<3&&!vis[i+1][j+1]){
dp[i+1][j+1]=(dp[i+1][j+1]+dp[i][j])%MOD;
}
}
}
return (int)dp[2][n-1];
}
}