zoukankan      html  css  js  c++  java
  • CF1574D.The Strongest Build (bfs)

    linkkkk
    题意:
    给出有(n)行的矩阵,每行有(a_i)个元素。保证按照升序排列。
    每行都选择一个元素使得获得的价值最大,其中有(m)种不可以选的方案。
    思路:
    (n<=10)是个很重要的条件。
    由于每行都选最后的元素是最优的,考虑怎么躲避开不可选的方案。贪心的考虑,如果一个方案不可选,那么将其中的一个元素变成选前面的一位元素是比较优的,直接(O(nm))枚举就好了
    代码:

    // Problem: D. The Strongest Build
    // Contest: Codeforces - Educational Codeforces Round 114 (Rated for Div. 2)
    // URL: https://codeforces.com/contest/1574/problem/D
    // Memory Limit: 256 MB
    // Time Limit: 3000 ms
    // 
    // Powered by CP Editor (https://cpeditor.org)
    
    #include<bits/stdc++.h>
    using namespace std;
    
    const int maxn=2e5+10;
    
    vector<int>ans,a[15],b[maxn];
    map<vector<int>,int>mp;
    
    int main(){
    	int n;cin>>n;
    	for(int i=0;i<n;i++){
    		int m;cin>>m;
    		ans.push_back(m-1);
    		for(int j=0;j<m;j++){
    			int x;cin>>x;
    			a[i].push_back(x);
    		}
    	}
    	int m;cin>>m;
    	for(int i=0;i<m;i++){
    		for(int j=0;j<n;j++){
    			int x;cin>>x;
    			x--;
    			b[i].push_back(x);
    		}
    		mp[b[i]]=1;
    	}
    	if(mp.find(ans)==mp.end()){
    		for(auto it:ans) cout<<it+1<<" ";
    		puts("");
    		return 0;
    	}
    	int res=0;
    	for(int i=0;i<m;i++){
    		int sum=0;
    		for(int j=0;j<n;j++) sum+=a[j][b[i][j]];
    		for(int j=0;j<n;j++){
    			sum-=a[j][b[i][j]];
    			if(b[i][j]==0) continue;
    			b[i][j]--;
    			sum+=a[j][b[i][j]];
    			if(mp.find(b[i])==mp.end()){
    				if(sum>res) ans=b[i],res=sum;
    			}
    			sum-=a[j][b[i][j]];
    			b[i][j]++;
    			sum+=a[j][b[i][j]];
    		}
    	}
    	for(auto it:ans){
    		cout<<it+1<<" ";
    	}
    	puts("");
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	return 0;
    }
    
  • 相关阅读:
    vs整合MySQL和QT
    VS2019中QT连接及使用
    JavaScript 进阶入门
    解决MySQL workbench的resultgird不出现的问题
    JavaScript入门
    CSS学习
    Linux下如何查看tomcat是否启动、查看tomcat启动日志
    oracle常见的函数
    java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter
    位运算(&、|、^、~、>>、<<)
  • 原文地址:https://www.cnblogs.com/OvOq/p/15323873.html
Copyright © 2011-2022 走看看