UPC 2020年混合个人训练第90~92场部分题目

//upc NO.90 问题 F: Heartlessly的三角形
/* 找规律 +推公式(等差等比数列错位相减) +n过大用快速幂 +前缀和的思想 */
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int mod = 1e9+7;

ll qpow(ll a,ll b) {
    
    
    ll ans = 1;
    while(b) {
    
    
        if(b&1)    ans=(ans*a)%mod;  
        a = (a*a)%mod;
        b /= 2;
    }
    return ans;
}
//3*(2^n-1) - 2*n.
int main()
{
    
    
    ll n,m,a,b;
	scanf("%lld %lld",&n,&m);

    while(m--)
	{
    
    
        scanf("%lld %lld",&a,&b);
        a--; 
        ll ans1=((3*(qpow(2,b)-1)%mod)%mod+mod-(2*b)%mod)%mod;
        ll ans2=((3*(qpow(2,a)-1)%mod)%mod+mod-(2*a)%mod)%mod;
        printf("%lld\n",(ans1-ans2+mod)%mod);
    }
    return 0;
}
//upc NO.91 问题 J: 直角三角形
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int N = 1e6+10;
struct node{
    
    int x,y;}a[N];
ll n,p[N],q[N];

int main()
{
    
    
	scanf("%lld",&n);
	for(int i=1; i<=n; i++)	{
    
    
		cin >> a[i].x >> a[i].y;
		p[a[i].x]++; //记录横坐标相同的点 
		q[a[i].y]++; //记录纵坐标相同的点 
	}
	
	/*以这个点为直角点的三角形数量, 
	就是横坐标与这个点相同的点的数量
	乘以纵坐标与这个点相同的点的数量*/
	ll ans = 0;
	for(int i=1; i<=n; i++)
	    ans += 1ll*(p[a[i].x]-1)*(q[a[i].y]-1); //尚未理解 
	cout << ans << endl;
	return 0;
} 
//upc NO.91 问题 I: 约会序列
/*
模拟队列的思想,把先输出的存到数组里 。然后就考虑分类讨论
在可操作的情况下,如果首尾两端不同就把字典序小的先存进去然后移动首尾指针中的一个。
否则要跳过去考虑,保证下一个首尾两端不同时有最优解。 
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10; 
char a[maxn],b[maxn];
int n;

int main()
{
    
    
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];
	int l=1 , r=n , cnt = 0;
	while(r>=1) {
    
    
		// 首尾不同的时候 
		if(a[l]<a[r]) {
    
    
			b[++cnt] = a[l]; l++;
		}
		else if(a[l]>a[r]) {
    
    
			b[++cnt] = a[r]; r--;
		}
		
		//首尾相同的时候 
		else {
    
    
			int flag = 0;
			int ll = l+1 , rr=r-1;
			while(rr>=ll) {
    
    
				if(a[ll]<a[rr]) {
    
    
					flag = 1; break;
				} 
				if(a[ll]>a[rr]) {
    
    
					flag = 0; break;
				}
				ll++;
				rr--;
			}
			if(flag==0) {
    
    
				b[++cnt] = a[r]; r--;
			}
			else {
    
    
				b[++cnt] = a[l]; l++;
			}
		}
	} 
	for(int i=1; i<=n; i++) cout<<b[i]<<" ";
	return 0;
} 
//upc NO.91 问题 B:营救(save)
/* BFS计数 (板子题)*/
#include <bits/stdc++.h>
#include <queue>
#define ll long long 
using namespace std;
const int maxn = 1e3+10;
int vis[maxn][maxn];
char a[maxn][maxn];

struct node {
    
    int x,y,st;};

int dx[4] = {
    
    0,0,1,-1};
int dy[4] = {
    
    1,-1,0,0};
int n,sx,sy,ex,ey;

int bfs(int x,int y)
{
    
    
	queue<node> q;
	node no,nx;
	no.x = x;
	no.y = y;
	no.st = 0;
	q.push(no);
	while(!q.empty())
	{
    
    
		no = q.front();
		q.pop();
		for(int i=0; i<4; i++)
		{
    
    
			nx.x = no.x + dx[i];
			nx.y = no.y + dy[i];
			nx.st = no.st+1;
		    if(nx.x<=0 || nx.y<=0 || nx.x>n || nx.y>n || a[nx.x][nx.y]=='1' || vis[nx.x][nx.y]==1) continue;
            vis[nx.x][nx.y] = 1;
			if(nx.x==ex && nx.y==ey)
			    return nx.st;
			q.push(nx);		
		}
	}
}

int main()
{
    
    
	cin >> n;
	for(int i=1; i<=n; i++) 
	    scanf("%s",a[i]+1);
	cin >> sx >> sy >> ex >> ey;
	int t = bfs(sx,sy);
	printf("%d\n",t);  
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Luoxiaobaia/article/details/110023265
UPC