#分组背包,模拟# [luogu CF148E] Porcelain &[luogu CF135B] Rectangle and Square

Title

CF148E Porcelain

CF135B Rectangle and Square


Solution

权且当作放一下程序


Code(Porcelain)

#include
#include
#include
#define rep(i,x,y) for(register int i=x;i<=y;i++)
using namespace std; 
const int N=305; 
int f[N][N],a[N][N],g[N][10005],n,m,q[N][N],w[N]; 
int ask(int k,int x,int y){
	return q[k][x]+q[k][w[k]]-q[k][w[k]-y]; 
}
int main(){
	scanf("%d%d",&n,&m); 
	rep(i,1,n) {
		scanf("%d",&w[i]); 
		rep(j,1,w[i]) scanf("%d",&a[i][j]),q[i][j]=q[i][j-1]+a[i][j]; 
	}
	rep(k,1,n) rep(i,1,w[k]) rep(j,0,i)	f[k][i]=max(f[k][i],ask(k,j,(i-j))); 
	rep(k,1,n) rep(i,0,m) for(int j=0;j<=w[k]&&j<=i;j++) g[k][i]=max(g[k][i],g[k-1][i-j]+f[k][j]); 
	printf("%d",g[n][m]); 
	return 0; 
}

Code(Rectangle and Square)

#include
#include
#include
#include
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define db ((a[q].x-a[i].x)==(a[w].x-a[j].x))&&((a[i].y-a[j].y)==(a[q].y-a[w].y))
#define dbb (pow(a[i].y-a[j].y,2)+pow(a[j].x-a[i].x,2)==pow(a[q].y-a[i].y,2)+pow(a[q].x-a[i].x,2))
#define zj (pow(a[i].y-a[j].y,2)+pow(a[j].x-a[i].x,2)+pow(a[q].y-a[i].y,2)+pow(a[q].x-a[i].x,2)==pow(a[q].x-a[j].x,2)+pow(a[q].y-a[j].y,2))
#define gx (a[w].y!=a[j].y&&a[j].x!=a[i].x)
using namespace std; 
struct node{int x,y;}a[9];
int n=8; bool b=0,B[8]; 
vector<int>v,vv; 
int main(){
	rep(i,1,n) scanf("%d%d",&a[i].x,&a[i].y); 
	rep(i,1,n) rep(j,1,n) rep(q,1,n) rep(w,1,n) if (i!=j&&j!=q&&q!=w&&i!=q&&i!=w&&j!=w&&!b){
		if (db&&dbb&&zj&&gx) {B[i]=B[j]=B[q]=B[w]=1; v.push_back(i),v.push_back(j),v.push_back(q),v.push_back(w),b=1; break;}
	}
	if (!b) return 0&printf("NO"); else b=0;  
	rep(i,1,n) rep(j,1,n) rep(q,1,n) rep(w,1,n) if (i!=j&&j!=q&&q!=w&&i!=q&&i!=w&&j!=w&&!b){
		if (B[i]||B[j]||B[q]||B[w]) continue; 
		if (db&&zj&&gx) {vv.push_back(i),vv.push_back(j),vv.push_back(q),vv.push_back(w),b=1; break;}
	}
	if (!b) return 0&printf("NO"); else printf("YES\n"); 
	sort(v.begin(),v.end()),sort(vv.begin(),vv.end()); 
	rep(i,0,3) printf("%d ",v[i]); printf("\n"); 
	rep(i,0,3) printf("%d ",vv[i]); 
	return 0; 
}

你可能感兴趣的:(动态规划(/线性DP),暴力(/模拟/字符串处理),cf148e,cf135b,porcelain,rectangl,分组背包)