贪心

P1199 [NOIP2010 普及组] 三国游戏

最开始写暴力,然后TLE了,发现当我们选定第一个人的时候,第一大肯定被对面抢了,所以我们只能选择第二大,因为第一大谁都拿不到,所以我们最后肯定能比他大。

#include<bits/stdc++.h>
using namespace std;
int a[502][502];
int n ;
int v[502];
int ans1 , ans2;  
int cnt1 , cnt2;
int st1[502] , st2[502];
int f;
int ans;
/*void find(){
		while(cnt1 + cnt2 != n){
        int maxx = 0;
        int id = 0 ;
		for(int j = 1; j <= cnt1 ; j ++){
           for(int k = 1 ; k <= n ; k ++){
           	   if(v[k])continue;
           	  if(a[st1[j]][k] > maxx){
           	    maxx = a[st1[j]][k];
			   id = k;  	
			 }
		   }
		 }	
		 if(cnt1 > cnt2){
		 st2[++cnt2] = id ;
		 v[id] = 1;
	}
		 else {
		 st1[++cnt1] = id; 
		  v[id] = 1 ;
		  
     	  }  
	   }
    for(int k = 1 ; k <= cnt1 ; k ++){
    	for(int j = k; j <= cnt1 ;j ++){
           ans1 = max(ans1 , a[st1[k]][st1[j]]);		
		}
	  }
	for(int k = 1 ; k <= cnt2 ; k ++){
    	for(int j = k; j <= cnt2 ; j ++){
           ans2 = max(ans2 , a[st2[k]][st2[j]]);		
		}
	  }
	  if(ans1 > ans2){
	  if(!f)cout << 1 << endl;
	    ans = max(ans1 , ans);
	  	f = 1;
	  }
} */
int k[10020];
int main () {
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++){
    	for(int j = i + 1 ; j <= n ;j ++){
    		cin >> a[i][j];
    		a[j][i] = a[i][j];	
		}
	} 	
	for(int i = 1 ; i <= n  ;i ++){
		sort(a[i] + 1 ,  a[i] + 1 + n);
		ans = max(ans , a[i][n - 1]);
	}
	cout << "1" << endl;
	cout << ans ;
    /*for(int i = 1 ; i <= n ; i ++){
    	memset(v , 0 , sizeof v);
       cnt1 = cnt2 = 0 ;
		ans1 = ans2 = 0;
  	    v[i] = 1;
  	    st1[++cnt1] = i;
     	find();
   	 }   
	  if(f == 0){
		cout << 0 << endl;	
	}else cout << ans;*/
}

P1007 独木桥

对于每一个战士,可以选择向左或者向右走,那么
就很显然我们只需算出他向左走的时间p,或者向右的时间L - p + 1取最大和最小即可

#include<bits/stdc++.h>
using namespace std;
int n , l , p ;
int ans1 = 0 , ans2 = 0;
int main()
{
    cin >> l >> n ; 
    for(int i = 1;i <= n;i ++)
    {
        cin >>  p ;
		ans2 = max(ans2 , max(l - p + 1 , p));
        ans1 = max(ans1 , min(l - p + 1 , p));
    }
    printf("%d %d" , ans1 , ans2);
    return 0;
}

P1223 排队接水

排序不等式

#include<bits/stdc++.h>
using namespace std;
int n ;
pair<int ,int> a[10002];
int main () {
	cin >> n ;
	for(int i = 0 ; i < n ; i ++){
		cin >> a[i].first;
		a[i].second = i;
	}
	sort(a  , a + n);
	double ans = 0;
	for(int i = 0 ; i < n  ; i ++){
		cout << a[i].second + 1<< " ";
		ans += a[i].first * (n - i - 1);
	}
	cout << endl;
	printf("%.2lf" , ans / n);
}

P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G

priority_queue是大根堆,所以要把负数存进去

#include<bits/stdc++.h>
using namespace std;
int n ;
int x;
int ans;
priority_queue<int>q;
int main () {
	cin >> n;
	for(int i = 1; i <= n ;i  ++){
		cin >> x;
		q.push(-x);
	}
	for(int i = 1; i < n ; i ++){
	int  a = q.top();
	q.pop();
	int c = q.top();
	q.pop();
	ans += a + c;
	q.push(a+c);
  }
   cout << -ans << endl;
}
posted @ 2022-12-01 16:38  wmjlzw1314  阅读(35)  评论(0)    收藏  举报