Codeforces645div2

Codeforces #645div2

A

链接

思路:规律题,一个路灯照两个格子,用最少的路灯覆盖所有格子

ACcodes:

#include 
using namespace std;
const int N = 2e5+10;
int test = 1;
int n,m,a[N];
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		cin >> n >> m;
		if(n==1 && m==1)
		{
			cout << 1 << endl;
			continue;
		}
		if(n%2)
		{
			cout << n/2*m + (m+1)/2 << endl;
		}
		else
			cout << n/2*m << endl;
	}
	return 0;
}

B

链接

思路:模拟,先排序,再扫描,如果当前人数不够吸引老太太,那么先让他们等着,继续扫描,把这个老太太加入到等待行列

ACcodes:

#include 
using namespace std;
const int N = 2e5+10;
int test = 1;
int n,a[N];
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		cin >> n;
		int vis[N] = {0};
		for(int i=1;i<=n;i++)
			cin >> a[i],vis[a[i]]++;
		sort(a+1,a+1+n);
		int res = 1, lef=0;
		
		for(int i=1;i<=n;i++)
		{
			if(res+vis[a[i]]+lef-1>=a[i])
			{
				res+=vis[a[i]]+lef;
				lef=0;
			}
			else
				lef+=vis[a[i]];
			i+=vis[a[i]]-1;
		}
		cout << res << endl;
	}
	return 0;
}

C

链接

思路:找规律

ACcodes:

#include 
using namespace std;
const int N = 2e5+10;
int test = 1;
int n,a[N];	
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		long long x1,x2,y1,y2;
		cin >> x1 >> y1 >> x2 >> y2;
		long long res = (x2-x1)*(y2-y1)+1;
		cout << res << endl;

	}
	return 0;
}

D

链接

思路:前缀和,肯定是以最大月份的月末结尾,注意long long,就是因为这个WA了。。。

AC codes:

#include 
using namespace std;
#define int long long 
const int N = 4e5+10;
int test = 1;
signed main() {
  int n, len;
  cin >> n >> len;
  vector<int> A(2 * n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    A[n + i] = A[i];
  }
  n *= 2;
  
  vector<int> B = {0}, C = {0};
  for (int i = 0; i < n; i++) 
    B.push_back(B.back() + A[i]);
  for (int i = 0; i < n; i++) 
    C.push_back(C.back() + (A[i] * (A[i] + 1)) / 2);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (B[i + 1] >= len) {
      int z = upper_bound(B.begin(), B.end(), B[i + 1] - len) - B.begin();
      int cnt = C[i + 1] - C[z];
      int days = B[i + 1] - B[z];
      int too = len - days;
      cnt += ((A[z - 1] * (A[z - 1] + 1)) / 2);
      cnt -= (((A[z - 1] - too) * (A[z - 1] - too + 1)) / 2);
      ans = max(ans, cnt);
    }
  }
  cout << ans;
}

你可能感兴趣的:(Codeforces)