zoukankan      html  css  js  c++  java
  • CodeForces

    /*
      这题真是...检查到错误以后,才觉得十分惋惜...太惨了!
      主要是,之前choosemax函数,本意是要找容积最大的,而...按照我代码的语义,a数组已经变为记录当前杯子装了多少,b数组才是真正的记录容量的数组,但被我给搞忘记了...真是十分难过和悲伤...
      
      同时,也突然觉得,自己未免也太不小心了,自己改了a数组居然都不记得,不过以后,我觉得最好还是改b不改a,毕竟输入进的是a,这样应该能降低出错的可能(其实没什么大用,自己不小心,总还会有别的错的)
    */



    #include <bits/stdc++.h>
    using namespace std;
    const int N = 105;
    int a[N];
    int b[N];
    int full[N];
    int choosemax(int n)
    {
    	int max = 0, tp = 1, flag = 0;
    	for (int i = 1; i <= n; i++)
    	{
    		if (full[i])
    		continue;
    		if (b[i] > max)
    		{
    			tp = i; max = b[i]; flag = 1;
    		}
    	}
    	if (flag) return tp;
    	else return 0;//all the cups are full
    }
    void showa(int n)
    {
    	cout << a[1];
    	for (int i = 2; i <= n; i++) cout << " " << a[i];
    	cout << endl;
    }
    
    int main()
    {
    	int n, w;
    	while (cin >> n >> w)
    	{
    		memset(full, 0, sizeof(full));
    		int sum = 0, ans = -1;
    		for (int i = 1; i <= n; i++)
    		{
    			cin >> a[i], b[i] = a[i];
    			if (a[i] % 2) a[i] = a[i] / 2 + 1;
    			else a[i] /= 2;
    		 	sum += a[i];
    		}
    		
    		
    		if (sum > w)
    		{
    			cout << -1 << endl; continue;
    		}
    	//	cout << sum << endl;
    		w -= sum;
    	//	cout << w << endl;
    		
    	//	for (int i = 1; i <= n; i++) cout << endl << "test: " << a[i] << " ";
    		int max, t;
    		while (w > 0)
    		{
    			max = choosemax(n);		
    			t = b[max] - a[max];
    			if (w >= t)
    			{
    				a[max] = b[max];
    				full[max] = 1;
    				w -= t;
    			}
    			else
    			{
    				a[max] += w;
    				w = 0;
    			}
    		}
    
    		showa(n);
    		
    	}
    	return 0;
    }



    /*
    这是参考了同学的代码,他用到了struct,也用到了sort,思路十分清晰...
    突然想起,积分赛时,我还是想过要用sort的,但是最后发现,我自己的那种方法,似乎用不了sort,主要时下标、容量和已装体积,无法保证三者一一对应,而如果把三者放到一个struct里,事情就好解决多了
    
    所以,struct...一定要活用啊!~
    */



    #include <bits/stdc++.h>
    using namespace std;
    const int N = 105;
    struct cup
    {
    	int c, v, index;//分别依次为:容量、杯中茶的体积、排序前序号
    	bool operator < (const cup &b) const // 排序时以容积大小排序,容积大的排前面
    	{
    		return b.c < c;
    	} 
    }a[N];
    int b[N];
    int main()
    {
    	int n, w;
    	cin >> n >> w;
    	int sum = 0, tp; //tp == temp
    	memset(a, 0, sizeof(a));
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a[i].c;
    		a[i].index = i;		//每次输入容量以后,先在结构体的index处标好号,就来倒酒,先将每个杯子都倒入一半(奇数体积另外处理)
    		
    		if (a[i].c & 1) //用按位与符号判断奇偶
    			tp = (a[i].c + 1) >> 1;  
    		else
    			tp = a[i].c >> 1;
    		a[i].v += tp;
    		sum += tp;
    	}
    	sort(a, a+n);
    //	cout << "test: " << endl; for (int i = 0; i < n; i++) cout << a[i].c << " "; cout << endl;
    
    	if (sum > w)
    	{
    		cout << -1 << endl; return 0;
    	}
    	
    	for (int i = 0; i < n && sum < w; i++)
    	{
    		tp = min(w - sum, a[i].c - a[i].v);
    		sum += tp;
    		a[i].v += tp;
    	}
    	if (sum < w)
    	{
    		cout << -1 << endl; return 0;
    	}
    	for (int i = 0; i < n; i++)
    	{
    		b[a[i].index] = a[i].v;
    	}
    	for (int i = 0; i < n; i++)
    	cout << b[i] << " ";
    	cout << endl;
    	return 0;
    }


  • 相关阅读:
    P1352 没有上司的舞会
    P1879 [USACO06NOV]玉米田Corn Fields
    P1896 [SCOI2005]互不侵犯
    2019寒假纪中happy之旅
    JZOJ 4249. 【五校联考7day1】游戏
    JZOJ 4248. 【五校联考7day1】n染色
    JZOJ 4252. 【五校联考7day2】QYQ的图
    STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'
    STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'
    重踏学习Java路上_Day02(java 基础上)
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789509.html
Copyright © 2011-2022 走看看